blob: 91e52a6c9edcca7fb636a0bde7f657c3d8b086e7 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga448bc42007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/CallingConv.h"
26#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000030#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/ADT/DenseMap.h"
32#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/SmallPtrSet.h"
34#include <map>
35using namespace llvm;
36
37#ifndef NDEBUG
38static cl::opt<bool>
39ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
40 cl::desc("Pop up a window to show dags before legalize"));
41#else
42static const bool ViewLegalizeDAGs = 0;
43#endif
44
45//===----------------------------------------------------------------------===//
46/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
47/// hacks on it until the target machine can handle it. This involves
48/// eliminating value sizes the machine cannot handle (promoting small sizes to
49/// large sizes or splitting up large values into small values) as well as
50/// eliminating operations the machine cannot handle.
51///
52/// This code also does a small amount of optimization and recognition of idioms
53/// as part of its processing. For example, if a target does not support a
54/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
55/// will attempt merge setcc and brc instructions into brcc's.
56///
57namespace {
58class VISIBILITY_HIDDEN SelectionDAGLegalize {
59 TargetLowering &TLI;
60 SelectionDAG &DAG;
61
62 // Libcall insertion helpers.
63
64 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
65 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
67 SDOperand LastCALLSEQ_END;
68
69 /// IsLegalizingCall - This member is used *only* for purposes of providing
70 /// helpful assertions that a libcall isn't created while another call is
71 /// being legalized (which could lead to non-serialized call sequences).
72 bool IsLegalizingCall;
73
74 enum LegalizeAction {
75 Legal, // The target natively supports this operation.
76 Promote, // This operation should be executed in a larger type.
77 Expand // Try to expand this to other ops, otherwise use a libcall.
78 };
79
80 /// ValueTypeActions - This is a bitvector that contains two bits for each
81 /// value type, where the two bits correspond to the LegalizeAction enum.
82 /// This can be queried with "getTypeAction(VT)".
83 TargetLowering::ValueTypeActionImpl ValueTypeActions;
84
85 /// LegalizedNodes - For nodes that are of legal width, and that have more
86 /// than one use, this map indicates what regularized operand to use. This
87 /// allows us to avoid legalizing the same thing more than once.
88 DenseMap<SDOperand, SDOperand> LegalizedNodes;
89
90 /// PromotedNodes - For nodes that are below legal width, and that have more
91 /// than one use, this map indicates what promoted value to use. This allows
92 /// us to avoid promoting the same thing more than once.
93 DenseMap<SDOperand, SDOperand> PromotedNodes;
94
95 /// ExpandedNodes - For nodes that need to be expanded this map indicates
96 /// which which operands are the expanded version of the input. This allows
97 /// us to avoid expanding the same node more than once.
98 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
99
100 /// SplitNodes - For vector nodes that need to be split, this map indicates
101 /// which which operands are the split version of the input. This allows us
102 /// to avoid splitting the same node more than once.
103 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
104
105 /// ScalarizedNodes - For nodes that need to be converted from vector types to
106 /// scalar types, this contains the mapping of ones we have already
107 /// processed to the result.
108 std::map<SDOperand, SDOperand> ScalarizedNodes;
109
110 void AddLegalizedOperand(SDOperand From, SDOperand To) {
111 LegalizedNodes.insert(std::make_pair(From, To));
112 // If someone requests legalization of the new node, return itself.
113 if (From != To)
114 LegalizedNodes.insert(std::make_pair(To, To));
115 }
116 void AddPromotedOperand(SDOperand From, SDOperand To) {
117 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
118 assert(isNew && "Got into the map somehow?");
119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
121 }
122
123public:
124
125 SelectionDAGLegalize(SelectionDAG &DAG);
126
127 /// getTypeAction - Return how we should legalize values of this type, either
128 /// it is already legal or we need to expand it into multiple registers of
129 /// smaller integer type, or we need to promote it to a larger type.
130 LegalizeAction getTypeAction(MVT::ValueType VT) const {
131 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
132 }
133
134 /// isTypeLegal - Return true if this type is legal on this target.
135 ///
136 bool isTypeLegal(MVT::ValueType VT) const {
137 return getTypeAction(VT) == Legal;
138 }
139
140 void LegalizeDAG();
141
142private:
143 /// HandleOp - Legalize, Promote, or Expand the specified operand as
144 /// appropriate for its type.
145 void HandleOp(SDOperand Op);
146
147 /// LegalizeOp - We know that the specified value has a legal type.
148 /// Recursively ensure that the operands have legal types, then return the
149 /// result.
150 SDOperand LegalizeOp(SDOperand O);
151
Dan Gohman6d05cac2007-10-11 23:57:53 +0000152 /// UnrollVectorOp - We know that the given vector has a legal type, however
153 /// the operation it performs is not legal and is an operation that we have
154 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
155 /// operating on each element individually.
156 SDOperand UnrollVectorOp(SDOperand O);
157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 /// PromoteOp - Given an operation that produces a value in an invalid type,
159 /// promote it to compute the value into a larger type. The produced value
160 /// will have the correct bits for the low portion of the register, but no
161 /// guarantee is made about the top bits: it may be zero, sign-extended, or
162 /// garbage.
163 SDOperand PromoteOp(SDOperand O);
164
165 /// ExpandOp - Expand the specified SDOperand into its two component pieces
166 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
167 /// the LegalizeNodes map is filled in for any results that are not expanded,
168 /// the ExpandedNodes map is filled in for any results that are expanded, and
169 /// the Lo/Hi values are returned. This applies to integer types and Vector
170 /// types.
171 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
172
173 /// SplitVectorOp - Given an operand of vector type, break it down into
174 /// two smaller values.
175 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
176
177 /// ScalarizeVectorOp - Given an operand of single-element vector type
178 /// (e.g. v1f32), convert it into the equivalent operation that returns a
179 /// scalar (e.g. f32) value.
180 SDOperand ScalarizeVectorOp(SDOperand O);
181
182 /// isShuffleLegal - Return true if a vector shuffle is legal with the
183 /// specified mask and type. Targets can specify exactly which masks they
184 /// support and the code generator is tasked with not creating illegal masks.
185 ///
186 /// Note that this will also return true for shuffles that are promoted to a
187 /// different type.
188 ///
189 /// If this is a legal shuffle, this method returns the (possibly promoted)
190 /// build_vector Mask. If it's not a legal shuffle, it returns null.
191 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
192
193 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
194 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
195
196 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
197
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
199 SDOperand &Hi);
200 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
201 SDOperand Source);
202
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +0000203 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
204 MVT::ValueType DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
206 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
207 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
208 SDOperand LegalOp,
209 MVT::ValueType DestVT);
210 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
212 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
213 bool isSigned);
214
215 SDOperand ExpandBSWAP(SDOperand Op);
216 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
217 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
219 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
220 SDOperand &Lo, SDOperand &Hi);
221
222 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
223 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224};
225}
226
227/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
228/// specified mask and type. Targets can specify exactly which masks they
229/// support and the code generator is tasked with not creating illegal masks.
230///
231/// Note that this will also return true for shuffles that are promoted to a
232/// different type.
233SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
234 SDOperand Mask) const {
235 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
236 default: return 0;
237 case TargetLowering::Legal:
238 case TargetLowering::Custom:
239 break;
240 case TargetLowering::Promote: {
241 // If this is promoted to a different type, convert the shuffle mask and
242 // ask if it is legal in the promoted type!
243 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
244
245 // If we changed # elements, change the shuffle mask.
246 unsigned NumEltsGrowth =
247 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
248 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
249 if (NumEltsGrowth > 1) {
250 // Renumber the elements.
251 SmallVector<SDOperand, 8> Ops;
252 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
253 SDOperand InOp = Mask.getOperand(i);
254 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
255 if (InOp.getOpcode() == ISD::UNDEF)
256 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
257 else {
258 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
259 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
260 }
261 }
262 }
263 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
264 }
265 VT = NVT;
266 break;
267 }
268 }
269 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
270}
271
272SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
273 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
274 ValueTypeActions(TLI.getValueTypeActions()) {
275 assert(MVT::LAST_VALUETYPE <= 32 &&
276 "Too many value types for ValueTypeActions to hold!");
277}
278
279/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
280/// contains all of a nodes operands before it contains the node.
281static void ComputeTopDownOrdering(SelectionDAG &DAG,
282 SmallVector<SDNode*, 64> &Order) {
283
284 DenseMap<SDNode*, unsigned> Visited;
285 std::vector<SDNode*> Worklist;
286 Worklist.reserve(128);
287
288 // Compute ordering from all of the leaves in the graphs, those (like the
289 // entry node) that have no operands.
290 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
291 E = DAG.allnodes_end(); I != E; ++I) {
292 if (I->getNumOperands() == 0) {
293 Visited[I] = 0 - 1U;
294 Worklist.push_back(I);
295 }
296 }
297
298 while (!Worklist.empty()) {
299 SDNode *N = Worklist.back();
300 Worklist.pop_back();
301
302 if (++Visited[N] != N->getNumOperands())
303 continue; // Haven't visited all operands yet
304
305 Order.push_back(N);
306
307 // Now that we have N in, add anything that uses it if all of their operands
308 // are now done.
309 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
310 UI != E; ++UI)
311 Worklist.push_back(*UI);
312 }
313
314 assert(Order.size() == Visited.size() &&
315 Order.size() ==
316 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
317 "Error: DAG is cyclic!");
318}
319
320
321void SelectionDAGLegalize::LegalizeDAG() {
322 LastCALLSEQ_END = DAG.getEntryNode();
323 IsLegalizingCall = false;
324
325 // The legalize process is inherently a bottom-up recursive process (users
326 // legalize their uses before themselves). Given infinite stack space, we
327 // could just start legalizing on the root and traverse the whole graph. In
328 // practice however, this causes us to run out of stack space on large basic
329 // blocks. To avoid this problem, compute an ordering of the nodes where each
330 // node is only legalized after all of its operands are legalized.
331 SmallVector<SDNode*, 64> Order;
332 ComputeTopDownOrdering(DAG, Order);
333
334 for (unsigned i = 0, e = Order.size(); i != e; ++i)
335 HandleOp(SDOperand(Order[i], 0));
336
337 // Finally, it's possible the root changed. Get the new root.
338 SDOperand OldRoot = DAG.getRoot();
339 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
340 DAG.setRoot(LegalizedNodes[OldRoot]);
341
342 ExpandedNodes.clear();
343 LegalizedNodes.clear();
344 PromotedNodes.clear();
345 SplitNodes.clear();
346 ScalarizedNodes.clear();
347
348 // Remove dead nodes now.
349 DAG.RemoveDeadNodes();
350}
351
352
353/// FindCallEndFromCallStart - Given a chained node that is part of a call
354/// sequence, find the CALLSEQ_END node that terminates the call sequence.
355static SDNode *FindCallEndFromCallStart(SDNode *Node) {
356 if (Node->getOpcode() == ISD::CALLSEQ_END)
357 return Node;
358 if (Node->use_empty())
359 return 0; // No CallSeqEnd
360
361 // The chain is usually at the end.
362 SDOperand TheChain(Node, Node->getNumValues()-1);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Sometimes it's at the beginning.
365 TheChain = SDOperand(Node, 0);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Otherwise, hunt for it.
368 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
369 if (Node->getValueType(i) == MVT::Other) {
370 TheChain = SDOperand(Node, i);
371 break;
372 }
373
374 // Otherwise, we walked into a node without a chain.
375 if (TheChain.getValueType() != MVT::Other)
376 return 0;
377 }
378 }
379
380 for (SDNode::use_iterator UI = Node->use_begin(),
381 E = Node->use_end(); UI != E; ++UI) {
382
383 // Make sure to only follow users of our token chain.
384 SDNode *User = *UI;
385 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
386 if (User->getOperand(i) == TheChain)
387 if (SDNode *Result = FindCallEndFromCallStart(User))
388 return Result;
389 }
390 return 0;
391}
392
393/// FindCallStartFromCallEnd - Given a chained node that is part of a call
394/// sequence, find the CALLSEQ_START node that initiates the call sequence.
395static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
396 assert(Node && "Didn't find callseq_start for a call??");
397 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
398
399 assert(Node->getOperand(0).getValueType() == MVT::Other &&
400 "Node doesn't have a token chain argument!");
401 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
402}
403
404/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
405/// see if any uses can reach Dest. If no dest operands can get to dest,
406/// legalize them, legalize ourself, and return false, otherwise, return true.
407///
408/// Keep track of the nodes we fine that actually do lead to Dest in
409/// NodesLeadingTo. This avoids retraversing them exponential number of times.
410///
411bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
412 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
413 if (N == Dest) return true; // N certainly leads to Dest :)
414
415 // If we've already processed this node and it does lead to Dest, there is no
416 // need to reprocess it.
417 if (NodesLeadingTo.count(N)) return true;
418
419 // If the first result of this node has been already legalized, then it cannot
420 // reach N.
421 switch (getTypeAction(N->getValueType(0))) {
422 case Legal:
423 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Promote:
426 if (PromotedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Expand:
429 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 }
432
433 // Okay, this node has not already been legalized. Check and legalize all
434 // operands. If none lead to Dest, then we can legalize this node.
435 bool OperandsLeadToDest = false;
436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
437 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
438 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
439
440 if (OperandsLeadToDest) {
441 NodesLeadingTo.insert(N);
442 return true;
443 }
444
445 // Okay, this node looks safe, legalize it and return false.
446 HandleOp(SDOperand(N, 0));
447 return false;
448}
449
450/// HandleOp - Legalize, Promote, or Expand the specified operand as
451/// appropriate for its type.
452void SelectionDAGLegalize::HandleOp(SDOperand Op) {
453 MVT::ValueType VT = Op.getValueType();
454 switch (getTypeAction(VT)) {
455 default: assert(0 && "Bad type action!");
456 case Legal: (void)LegalizeOp(Op); break;
457 case Promote: (void)PromoteOp(Op); break;
458 case Expand:
459 if (!MVT::isVector(VT)) {
460 // If this is an illegal scalar, expand it into its two component
461 // pieces.
462 SDOperand X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000463 if (Op.getOpcode() == ISD::TargetConstant)
464 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 ExpandOp(Op, X, Y);
466 } else if (MVT::getVectorNumElements(VT) == 1) {
467 // If this is an illegal single element vector, convert it to a
468 // scalar operation.
469 (void)ScalarizeVectorOp(Op);
470 } else {
471 // Otherwise, this is an illegal multiple element vector.
472 // Split it in half and legalize both parts.
473 SDOperand X, Y;
474 SplitVectorOp(Op, X, Y);
475 }
476 break;
477 }
478}
479
480/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
481/// a load from the constant pool.
482static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
483 SelectionDAG &DAG, TargetLowering &TLI) {
484 bool Extend = false;
485
486 // If a FP immediate is precise when represented as a float and if the
487 // target can do an extending load from float to double, we put it into
488 // the constant pool as a float, even if it's is statically typed as a
489 // double.
490 MVT::ValueType VT = CFP->getValueType(0);
491 bool isDouble = VT == MVT::f64;
Dale Johannesenb17a7a22007-09-16 16:51:49 +0000492 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen2fc20782007-09-14 22:26:36 +0000493 CFP->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000495 if (VT!=MVT::f64 && VT!=MVT::f32)
496 assert(0 && "Invalid type expansion");
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000497 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
498 isDouble ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 }
500
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000501 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen2fc20782007-09-14 22:26:36 +0000503 // Do not try to be clever about long doubles (so far)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
505 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
506 VT = MVT::f32;
507 Extend = true;
508 }
509
510 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
511 if (Extend) {
512 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000513 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman12a9c082008-02-06 22:27:42 +0000514 0, MVT::f32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +0000516 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +0000517 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 }
519}
520
521
522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
527 MVT::ValueType VT = Node->getValueType(0);
528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
532
533 // First get the sign bit of second operand.
534 SDOperand Mask1 = (SrcVT == MVT::f64)
535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000572 if (MVT::isFloatingPoint(ST->getMemoryVT())) {
Dale Johannesen08275382007-09-08 19:29:23 +0000573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000587 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen08275382007-09-08 19:29:23 +0000588 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000589 // Get the half-size VT
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000590 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000606 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000607 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
608 ST->getSrcValue(), SVOffset + IncrementSize,
609 NewStoredVT, ST->isVolatile(), Alignment);
610
611 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
612}
613
614/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
615static
616SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
617 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000618 int SVOffset = LD->getSrcValueOffset();
619 SDOperand Chain = LD->getChain();
620 SDOperand Ptr = LD->getBasePtr();
621 MVT::ValueType VT = LD->getValueType(0);
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000622 MVT::ValueType LoadedVT = LD->getMemoryVT();
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000623 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen08275382007-09-08 19:29:23 +0000624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000627 if (LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000628 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000629 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000630 intVT = MVT::i32;
631 else
632 assert(0 && "Unaligned load of unsupported floating point type");
633
634 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
635 SVOffset, LD->isVolatile(),
636 LD->getAlignment());
637 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
638 if (LoadedVT != VT)
639 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
640
641 SDOperand Ops[] = { Result, Chain };
642 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
643 Ops, 2);
644 }
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000645 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
646 "Unaligned load of unsupported type.");
647
648 // Compute the new VT that is half the size of the old one. We either have an
649 // integer MVT or we have a vector MVT.
650 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
651 MVT::ValueType NewLoadedVT;
652 if (!MVT::isVector(LoadedVT)) {
653 NewLoadedVT = MVT::getIntegerType(NumBits/2);
654 } else {
655 // FIXME: This is not right for <1 x anything> it is also not right for
656 // non-power-of-two vectors.
657 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
658 MVT::getVectorNumElements(LoadedVT)/2);
659 }
660 NumBits >>= 1;
661
662 unsigned Alignment = LD->getAlignment();
663 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000664 ISD::LoadExtType HiExtType = LD->getExtensionType();
665
666 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
667 if (HiExtType == ISD::NON_EXTLOAD)
668 HiExtType = ISD::ZEXTLOAD;
669
670 // Load the value in two parts
671 SDOperand Lo, Hi;
672 if (TLI.isLittleEndian()) {
673 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
674 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
675 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
676 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
677 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
678 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000679 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000680 } else {
681 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
682 NewLoadedVT,LD->isVolatile(), Alignment);
683 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
684 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
685 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
686 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000687 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000688 }
689
690 // aggregate the two parts
691 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
692 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
693 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
694
695 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
696 Hi.getValue(1));
697
698 SDOperand Ops[] = { Result, TF };
699 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
700}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701
Dan Gohman6d05cac2007-10-11 23:57:53 +0000702/// UnrollVectorOp - We know that the given vector has a legal type, however
703/// the operation it performs is not legal and is an operation that we have
704/// no way of lowering. "Unroll" the vector, splitting out the scalars and
705/// operating on each element individually.
706SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
707 MVT::ValueType VT = Op.getValueType();
708 assert(isTypeLegal(VT) &&
709 "Caller should expand or promote operands that are not legal!");
710 assert(Op.Val->getNumValues() == 1 &&
711 "Can't unroll a vector with multiple results!");
712 unsigned NE = MVT::getVectorNumElements(VT);
713 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
714
715 SmallVector<SDOperand, 8> Scalars;
716 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
717 for (unsigned i = 0; i != NE; ++i) {
718 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
719 SDOperand Operand = Op.getOperand(j);
720 MVT::ValueType OperandVT = Operand.getValueType();
721 if (MVT::isVector(OperandVT)) {
722 // A vector operand; extract a single element.
723 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
724 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
725 OperandEltVT,
726 Operand,
727 DAG.getConstant(i, MVT::i32));
728 } else {
729 // A scalar operand; just use it as is.
730 Operands[j] = Operand;
731 }
732 }
733 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
734 &Operands[0], Operands.size()));
735 }
736
737 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
738}
739
Duncan Sands37a3f472008-01-10 10:28:30 +0000740/// GetFPLibCall - Return the right libcall for the given floating point type.
741static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
742 RTLIB::Libcall Call_F32,
743 RTLIB::Libcall Call_F64,
744 RTLIB::Libcall Call_F80,
745 RTLIB::Libcall Call_PPCF128) {
746 return
747 VT == MVT::f32 ? Call_F32 :
748 VT == MVT::f64 ? Call_F64 :
749 VT == MVT::f80 ? Call_F80 :
750 VT == MVT::ppcf128 ? Call_PPCF128 :
751 RTLIB::UNKNOWN_LIBCALL;
752}
753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754/// LegalizeOp - We know that the specified value has a legal type, and
755/// that its operands are legal. Now ensure that the operation itself
756/// is legal, recursively ensuring that the operands' operations remain
757/// legal.
758SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000759 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
760 return Op;
761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 assert(isTypeLegal(Op.getValueType()) &&
763 "Caller should expand or promote operands that are not legal!");
764 SDNode *Node = Op.Val;
765
766 // If this operation defines any values that cannot be represented in a
767 // register on this target, make sure to expand or promote them.
768 if (Node->getNumValues() > 1) {
769 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
770 if (getTypeAction(Node->getValueType(i)) != Legal) {
771 HandleOp(Op.getValue(i));
772 assert(LegalizedNodes.count(Op) &&
773 "Handling didn't add legal operands!");
774 return LegalizedNodes[Op];
775 }
776 }
777
778 // Note that LegalizeOp may be reentered even from single-use nodes, which
779 // means that we always must cache transformed nodes.
780 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
781 if (I != LegalizedNodes.end()) return I->second;
782
783 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
784 SDOperand Result = Op;
785 bool isCustom = false;
786
787 switch (Node->getOpcode()) {
788 case ISD::FrameIndex:
789 case ISD::EntryToken:
790 case ISD::Register:
791 case ISD::BasicBlock:
792 case ISD::TargetFrameIndex:
793 case ISD::TargetJumpTable:
794 case ISD::TargetConstant:
795 case ISD::TargetConstantFP:
796 case ISD::TargetConstantPool:
797 case ISD::TargetGlobalAddress:
798 case ISD::TargetGlobalTLSAddress:
799 case ISD::TargetExternalSymbol:
800 case ISD::VALUETYPE:
801 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000802 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 case ISD::STRING:
804 case ISD::CONDCODE:
805 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000806 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 "This must be legal!");
808 break;
809 default:
810 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
811 // If this is a target node, legalize it by legalizing the operands then
812 // passing it through.
813 SmallVector<SDOperand, 8> Ops;
814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
816
817 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
818
819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
820 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
821 return Result.getValue(Op.ResNo);
822 }
823 // Otherwise this is an unhandled builtin node. splat.
824#ifndef NDEBUG
825 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
826#endif
827 assert(0 && "Do not know how to legalize this operator!");
828 abort();
829 case ISD::GLOBAL_OFFSET_TABLE:
830 case ISD::GlobalAddress:
831 case ISD::GlobalTLSAddress:
832 case ISD::ExternalSymbol:
833 case ISD::ConstantPool:
834 case ISD::JumpTable: // Nothing to do.
835 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
836 default: assert(0 && "This action is not supported yet!");
837 case TargetLowering::Custom:
838 Tmp1 = TLI.LowerOperation(Op, DAG);
839 if (Tmp1.Val) Result = Tmp1;
840 // FALLTHROUGH if the target doesn't want to lower this op after all.
841 case TargetLowering::Legal:
842 break;
843 }
844 break;
845 case ISD::FRAMEADDR:
846 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 // The only option for these nodes is to custom lower them. If the target
848 // does not custom lower them, then return zero.
849 Tmp1 = TLI.LowerOperation(Op, DAG);
850 if (Tmp1.Val)
851 Result = Tmp1;
852 else
853 Result = DAG.getConstant(0, TLI.getPointerTy());
854 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000855 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000856 MVT::ValueType VT = Node->getValueType(0);
857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Custom:
860 Result = TLI.LowerOperation(Op, DAG);
861 if (Result.Val) break;
862 // Fall Thru
863 case TargetLowering::Legal:
864 Result = DAG.getConstant(0, VT);
865 break;
866 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000867 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000868 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 case ISD::EXCEPTIONADDR: {
870 Tmp1 = LegalizeOp(Node->getOperand(0));
871 MVT::ValueType VT = Node->getValueType(0);
872 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
873 default: assert(0 && "This action is not supported yet!");
874 case TargetLowering::Expand: {
875 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000876 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 }
878 break;
879 case TargetLowering::Custom:
880 Result = TLI.LowerOperation(Op, DAG);
881 if (Result.Val) break;
882 // Fall Thru
883 case TargetLowering::Legal: {
884 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
885 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000886 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 break;
888 }
889 }
890 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000891 if (Result.Val->getNumValues() == 1) break;
892
893 assert(Result.Val->getNumValues() == 2 &&
894 "Cannot return more than two values!");
895
896 // Since we produced two values, make sure to remember that we
897 // legalized both of them.
898 Tmp1 = LegalizeOp(Result);
899 Tmp2 = LegalizeOp(Result.getValue(1));
900 AddLegalizedOperand(Op.getValue(0), Tmp1);
901 AddLegalizedOperand(Op.getValue(1), Tmp2);
902 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 case ISD::EHSELECTION: {
904 Tmp1 = LegalizeOp(Node->getOperand(0));
905 Tmp2 = LegalizeOp(Node->getOperand(1));
906 MVT::ValueType VT = Node->getValueType(0);
907 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
908 default: assert(0 && "This action is not supported yet!");
909 case TargetLowering::Expand: {
910 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000911 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 }
913 break;
914 case TargetLowering::Custom:
915 Result = TLI.LowerOperation(Op, DAG);
916 if (Result.Val) break;
917 // Fall Thru
918 case TargetLowering::Legal: {
919 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
920 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000921 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 break;
923 }
924 }
925 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000926 if (Result.Val->getNumValues() == 1) break;
927
928 assert(Result.Val->getNumValues() == 2 &&
929 "Cannot return more than two values!");
930
931 // Since we produced two values, make sure to remember that we
932 // legalized both of them.
933 Tmp1 = LegalizeOp(Result);
934 Tmp2 = LegalizeOp(Result.getValue(1));
935 AddLegalizedOperand(Op.getValue(0), Tmp1);
936 AddLegalizedOperand(Op.getValue(1), Tmp2);
937 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 case ISD::EH_RETURN: {
939 MVT::ValueType VT = Node->getValueType(0);
940 // The only "good" option for this node is to custom lower it.
941 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
942 default: assert(0 && "This action is not supported at all!");
943 case TargetLowering::Custom:
944 Result = TLI.LowerOperation(Op, DAG);
945 if (Result.Val) break;
946 // Fall Thru
947 case TargetLowering::Legal:
948 // Target does not know, how to lower this, lower to noop
949 Result = LegalizeOp(Node->getOperand(0));
950 break;
951 }
952 }
953 break;
954 case ISD::AssertSext:
955 case ISD::AssertZext:
956 Tmp1 = LegalizeOp(Node->getOperand(0));
957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
958 break;
959 case ISD::MERGE_VALUES:
960 // Legalize eliminates MERGE_VALUES nodes.
961 Result = Node->getOperand(Op.ResNo);
962 break;
963 case ISD::CopyFromReg:
964 Tmp1 = LegalizeOp(Node->getOperand(0));
965 Result = Op.getValue(0);
966 if (Node->getNumValues() == 2) {
967 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
968 } else {
969 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
970 if (Node->getNumOperands() == 3) {
971 Tmp2 = LegalizeOp(Node->getOperand(2));
972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
973 } else {
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
975 }
976 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
977 }
978 // Since CopyFromReg produces two values, make sure to remember that we
979 // legalized both of them.
980 AddLegalizedOperand(Op.getValue(0), Result);
981 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
982 return Result.getValue(Op.ResNo);
983 case ISD::UNDEF: {
984 MVT::ValueType VT = Op.getValueType();
985 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
986 default: assert(0 && "This action is not supported yet!");
987 case TargetLowering::Expand:
988 if (MVT::isInteger(VT))
989 Result = DAG.getConstant(0, VT);
990 else if (MVT::isFloatingPoint(VT))
Dale Johannesen20b76352007-09-26 17:26:49 +0000991 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
992 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 else
994 assert(0 && "Unknown value type!");
995 break;
996 case TargetLowering::Legal:
997 break;
998 }
999 break;
1000 }
1001
1002 case ISD::INTRINSIC_W_CHAIN:
1003 case ISD::INTRINSIC_WO_CHAIN:
1004 case ISD::INTRINSIC_VOID: {
1005 SmallVector<SDOperand, 8> Ops;
1006 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1007 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1008 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1009
1010 // Allow the target to custom lower its intrinsics if it wants to.
1011 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1012 TargetLowering::Custom) {
1013 Tmp3 = TLI.LowerOperation(Result, DAG);
1014 if (Tmp3.Val) Result = Tmp3;
1015 }
1016
1017 if (Result.Val->getNumValues() == 1) break;
1018
1019 // Must have return value and chain result.
1020 assert(Result.Val->getNumValues() == 2 &&
1021 "Cannot return more than two values!");
1022
1023 // Since loads produce two values, make sure to remember that we
1024 // legalized both of them.
1025 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1026 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1027 return Result.getValue(Op.ResNo);
1028 }
1029
1030 case ISD::LOCATION:
1031 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1033
1034 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1035 case TargetLowering::Promote:
1036 default: assert(0 && "This action is not supported yet!");
1037 case TargetLowering::Expand: {
1038 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1039 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
1040 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
1041
1042 if (MMI && (useDEBUG_LOC || useLABEL)) {
1043 const std::string &FName =
1044 cast<StringSDNode>(Node->getOperand(3))->getValue();
1045 const std::string &DirName =
1046 cast<StringSDNode>(Node->getOperand(4))->getValue();
1047 unsigned SrcFile = MMI->RecordSource(DirName, FName);
1048
1049 SmallVector<SDOperand, 8> Ops;
1050 Ops.push_back(Tmp1); // chain
1051 SDOperand LineOp = Node->getOperand(1);
1052 SDOperand ColOp = Node->getOperand(2);
1053
1054 if (useDEBUG_LOC) {
1055 Ops.push_back(LineOp); // line #
1056 Ops.push_back(ColOp); // col #
1057 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
1058 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
1059 } else {
1060 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1061 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng69eda822008-02-01 02:05:57 +00001062 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng13d1c292008-01-31 09:59:15 +00001064 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1065 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 }
1067 } else {
1068 Result = Tmp1; // chain
1069 }
1070 break;
1071 }
1072 case TargetLowering::Legal:
1073 if (Tmp1 != Node->getOperand(0) ||
1074 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
1075 SmallVector<SDOperand, 8> Ops;
1076 Ops.push_back(Tmp1);
1077 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1078 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1079 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1080 } else {
1081 // Otherwise promote them.
1082 Ops.push_back(PromoteOp(Node->getOperand(1)));
1083 Ops.push_back(PromoteOp(Node->getOperand(2)));
1084 }
1085 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1086 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1087 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1088 }
1089 break;
1090 }
1091 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001092
1093 case ISD::DECLARE:
1094 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1095 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1096 default: assert(0 && "This action is not supported yet!");
1097 case TargetLowering::Legal:
1098 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1099 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1100 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1102 break;
1103 }
1104 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105
1106 case ISD::DEBUG_LOC:
1107 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1108 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1113 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1114 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1116 break;
1117 }
1118 break;
1119
1120 case ISD::LABEL:
Evan Cheng13d1c292008-01-31 09:59:15 +00001121 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1123 default: assert(0 && "This action is not supported yet!");
1124 case TargetLowering::Legal:
1125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1126 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng13d1c292008-01-31 09:59:15 +00001127 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 break;
1130 case TargetLowering::Expand:
1131 Result = LegalizeOp(Node->getOperand(0));
1132 break;
1133 }
1134 break;
1135
Andrew Lenharth785610d2008-02-16 01:24:58 +00001136 case ISD::MEMBARRIER: {
1137 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001138 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1139 default: assert(0 && "This action is not supported yet!");
1140 case TargetLowering::Legal: {
1141 SDOperand Ops[6];
1142 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1143 for (int x = 1; x < 6; ++x)
1144 Ops[x] = PromoteOp(Node->getOperand(x));
1145 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1146 break;
1147 }
1148 case TargetLowering::Expand:
1149 //There is no libgcc call for this op
1150 Result = Node->getOperand(0); // Noop
1151 break;
1152 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001153 break;
1154 }
1155
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001156 case ISD::ATOMIC_LCS:
1157 case ISD::ATOMIC_LAS:
1158 case ISD::ATOMIC_SWAP: {
1159 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1160 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1161 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
1162 "Invalid MemBarrier node!");
1163 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
1164 MVT::ValueType VT = Node->getValueType(0);
1165 switch (TLI.getOperationAction(ISD::ATOMIC_LCS, VT)) {
1166 default: assert(0 && "This action is not supported yet!");
1167 case TargetLowering::Legal: {
1168 SDOperand Ops[4];
1169 for (int x = 0; x < num; ++x)
1170 Ops[x] = LegalizeOp(Node->getOperand(x));
1171 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1172 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1173 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1174 return Result.getValue(Op.ResNo);
1175 break;
1176 }
1177 }
1178 break;
1179 }
1180
Scott Michelf2e2b702007-08-08 23:23:31 +00001181 case ISD::Constant: {
1182 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1183 unsigned opAction =
1184 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1185
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 // We know we don't need to expand constants here, constants only have one
1187 // value and we check that it is fine above.
1188
Scott Michelf2e2b702007-08-08 23:23:31 +00001189 if (opAction == TargetLowering::Custom) {
1190 Tmp1 = TLI.LowerOperation(Result, DAG);
1191 if (Tmp1.Val)
1192 Result = Tmp1;
1193 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001195 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 case ISD::ConstantFP: {
1197 // Spill FP immediates to the constant pool if the target cannot directly
1198 // codegen them. Targets often have some immediate values that can be
1199 // efficiently generated into an FP register without a load. We explicitly
1200 // leave these constants as ConstantFP nodes for the target to deal with.
1201 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1204 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001205 case TargetLowering::Legal:
1206 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001207 case TargetLowering::Custom:
1208 Tmp3 = TLI.LowerOperation(Result, DAG);
1209 if (Tmp3.Val) {
1210 Result = Tmp3;
1211 break;
1212 }
1213 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001214 case TargetLowering::Expand: {
1215 // Check to see if this FP immediate is already legal.
1216 bool isLegal = false;
1217 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1218 E = TLI.legal_fpimm_end(); I != E; ++I) {
1219 if (CFP->isExactlyValue(*I)) {
1220 isLegal = true;
1221 break;
1222 }
1223 }
1224 // If this is a legal constant, turn it into a TargetConstantFP node.
1225 if (isLegal)
1226 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001227 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1228 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001229 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001230 break;
1231 }
1232 case ISD::TokenFactor:
1233 if (Node->getNumOperands() == 2) {
1234 Tmp1 = LegalizeOp(Node->getOperand(0));
1235 Tmp2 = LegalizeOp(Node->getOperand(1));
1236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1237 } else if (Node->getNumOperands() == 3) {
1238 Tmp1 = LegalizeOp(Node->getOperand(0));
1239 Tmp2 = LegalizeOp(Node->getOperand(1));
1240 Tmp3 = LegalizeOp(Node->getOperand(2));
1241 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1242 } else {
1243 SmallVector<SDOperand, 8> Ops;
1244 // Legalize the operands.
1245 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1246 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1247 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1248 }
1249 break;
1250
1251 case ISD::FORMAL_ARGUMENTS:
1252 case ISD::CALL:
1253 // The only option for this is to custom lower it.
1254 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1255 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001256
1257 // The number of incoming and outgoing values should match; unless the final
1258 // outgoing value is a flag.
1259 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1260 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1261 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1262 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 "Lowering call/formal_arguments produced unexpected # results!");
1264
1265 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1266 // remember that we legalized all of them, so it doesn't get relegalized.
1267 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001268 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1269 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1271 if (Op.ResNo == i)
1272 Tmp2 = Tmp1;
1273 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1274 }
1275 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001276 case ISD::EXTRACT_SUBREG: {
1277 Tmp1 = LegalizeOp(Node->getOperand(0));
1278 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1279 assert(idx && "Operand must be a constant");
1280 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1281 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1282 }
1283 break;
1284 case ISD::INSERT_SUBREG: {
1285 Tmp1 = LegalizeOp(Node->getOperand(0));
1286 Tmp2 = LegalizeOp(Node->getOperand(1));
1287 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1288 assert(idx && "Operand must be a constant");
1289 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1291 }
1292 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 case ISD::BUILD_VECTOR:
1294 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1295 default: assert(0 && "This action is not supported yet!");
1296 case TargetLowering::Custom:
1297 Tmp3 = TLI.LowerOperation(Result, DAG);
1298 if (Tmp3.Val) {
1299 Result = Tmp3;
1300 break;
1301 }
1302 // FALLTHROUGH
1303 case TargetLowering::Expand:
1304 Result = ExpandBUILD_VECTOR(Result.Val);
1305 break;
1306 }
1307 break;
1308 case ISD::INSERT_VECTOR_ELT:
1309 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001311
1312 // The type of the value to insert may not be legal, even though the vector
1313 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1314 // here.
1315 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1316 default: assert(0 && "Cannot expand insert element operand");
1317 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1318 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1319 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1321
1322 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1323 Node->getValueType(0))) {
1324 default: assert(0 && "This action is not supported yet!");
1325 case TargetLowering::Legal:
1326 break;
1327 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001328 Tmp4 = TLI.LowerOperation(Result, DAG);
1329 if (Tmp4.Val) {
1330 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 break;
1332 }
1333 // FALLTHROUGH
1334 case TargetLowering::Expand: {
1335 // If the insert index is a constant, codegen this as a scalar_to_vector,
1336 // then a shuffle that inserts it into the right position in the vector.
1337 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001338 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1339 // match the element type of the vector being created.
1340 if (Tmp2.getValueType() ==
1341 MVT::getVectorElementType(Op.getValueType())) {
1342 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1343 Tmp1.getValueType(), Tmp2);
1344
1345 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1346 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1347 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1348
1349 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1350 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1351 // elt 0 of the RHS.
1352 SmallVector<SDOperand, 8> ShufOps;
1353 for (unsigned i = 0; i != NumElts; ++i) {
1354 if (i != InsertPos->getValue())
1355 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1356 else
1357 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1358 }
1359 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1360 &ShufOps[0], ShufOps.size());
1361
1362 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1363 Tmp1, ScVec, ShufMask);
1364 Result = LegalizeOp(Result);
1365 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 }
1368
1369 // If the target doesn't support this, we have to spill the input vector
1370 // to a temporary stack slot, update the element, then reload it. This is
1371 // badness. We could also load the value into a vector register (either
1372 // with a "move to register" or "extload into register" instruction, then
1373 // permute it into place, if the idx is a constant and if the idx is
1374 // supported by the target.
1375 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001376 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 MVT::ValueType IdxVT = Tmp3.getValueType();
1378 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001379 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman12a9c082008-02-06 22:27:42 +00001380
Dan Gohman20e37962008-02-11 18:58:42 +00001381 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman12a9c082008-02-06 22:27:42 +00001382 int SPFI = StackPtrFI->getIndex();
1383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 // Store the vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001385 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001386 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001387 SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388
1389 // Truncate or zero extend offset to target pointer type.
1390 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1391 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1392 // Add the offset to the index.
1393 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1394 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1395 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1396 // Store the scalar value.
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001397 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1398 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399 // Load the updated vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001400 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001401 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402 break;
1403 }
1404 }
1405 break;
1406 case ISD::SCALAR_TO_VECTOR:
1407 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1408 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1409 break;
1410 }
1411
1412 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1413 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1414 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1415 Node->getValueType(0))) {
1416 default: assert(0 && "This action is not supported yet!");
1417 case TargetLowering::Legal:
1418 break;
1419 case TargetLowering::Custom:
1420 Tmp3 = TLI.LowerOperation(Result, DAG);
1421 if (Tmp3.Val) {
1422 Result = Tmp3;
1423 break;
1424 }
1425 // FALLTHROUGH
1426 case TargetLowering::Expand:
1427 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1428 break;
1429 }
1430 break;
1431 case ISD::VECTOR_SHUFFLE:
1432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1433 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1435
1436 // Allow targets to custom lower the SHUFFLEs they support.
1437 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1438 default: assert(0 && "Unknown operation action!");
1439 case TargetLowering::Legal:
1440 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1441 "vector shuffle should not be created if not legal!");
1442 break;
1443 case TargetLowering::Custom:
1444 Tmp3 = TLI.LowerOperation(Result, DAG);
1445 if (Tmp3.Val) {
1446 Result = Tmp3;
1447 break;
1448 }
1449 // FALLTHROUGH
1450 case TargetLowering::Expand: {
1451 MVT::ValueType VT = Node->getValueType(0);
1452 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1453 MVT::ValueType PtrVT = TLI.getPointerTy();
1454 SDOperand Mask = Node->getOperand(2);
1455 unsigned NumElems = Mask.getNumOperands();
1456 SmallVector<SDOperand,8> Ops;
1457 for (unsigned i = 0; i != NumElems; ++i) {
1458 SDOperand Arg = Mask.getOperand(i);
1459 if (Arg.getOpcode() == ISD::UNDEF) {
1460 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1461 } else {
1462 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1463 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1464 if (Idx < NumElems)
1465 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1466 DAG.getConstant(Idx, PtrVT)));
1467 else
1468 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1469 DAG.getConstant(Idx - NumElems, PtrVT)));
1470 }
1471 }
1472 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1473 break;
1474 }
1475 case TargetLowering::Promote: {
1476 // Change base type to a different vector type.
1477 MVT::ValueType OVT = Node->getValueType(0);
1478 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1479
1480 // Cast the two input vectors.
1481 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1482 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1483
1484 // Convert the shuffle mask to the right # elements.
1485 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1486 assert(Tmp3.Val && "Shuffle not legal?");
1487 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1488 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1489 break;
1490 }
1491 }
1492 break;
1493
1494 case ISD::EXTRACT_VECTOR_ELT:
1495 Tmp1 = Node->getOperand(0);
1496 Tmp2 = LegalizeOp(Node->getOperand(1));
1497 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1498 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1499 break;
1500
1501 case ISD::EXTRACT_SUBVECTOR:
1502 Tmp1 = Node->getOperand(0);
1503 Tmp2 = LegalizeOp(Node->getOperand(1));
1504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1505 Result = ExpandEXTRACT_SUBVECTOR(Result);
1506 break;
1507
1508 case ISD::CALLSEQ_START: {
1509 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1510
1511 // Recursively Legalize all of the inputs of the call end that do not lead
1512 // to this call start. This ensures that any libcalls that need be inserted
1513 // are inserted *before* the CALLSEQ_START.
1514 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1515 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1516 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1517 NodesLeadingTo);
1518 }
1519
1520 // Now that we legalized all of the inputs (which may have inserted
1521 // libcalls) create the new CALLSEQ_START node.
1522 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1523
1524 // Merge in the last call, to ensure that this call start after the last
1525 // call ended.
1526 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1527 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1528 Tmp1 = LegalizeOp(Tmp1);
1529 }
1530
1531 // Do not try to legalize the target-specific arguments (#1+).
1532 if (Tmp1 != Node->getOperand(0)) {
1533 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1534 Ops[0] = Tmp1;
1535 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1536 }
1537
1538 // Remember that the CALLSEQ_START is legalized.
1539 AddLegalizedOperand(Op.getValue(0), Result);
1540 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1541 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1542
1543 // Now that the callseq_start and all of the non-call nodes above this call
1544 // sequence have been legalized, legalize the call itself. During this
1545 // process, no libcalls can/will be inserted, guaranteeing that no calls
1546 // can overlap.
1547 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1548 SDOperand InCallSEQ = LastCALLSEQ_END;
1549 // Note that we are selecting this call!
1550 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1551 IsLegalizingCall = true;
1552
1553 // Legalize the call, starting from the CALLSEQ_END.
1554 LegalizeOp(LastCALLSEQ_END);
1555 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1556 return Result;
1557 }
1558 case ISD::CALLSEQ_END:
1559 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1560 // will cause this node to be legalized as well as handling libcalls right.
1561 if (LastCALLSEQ_END.Val != Node) {
1562 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1563 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1564 assert(I != LegalizedNodes.end() &&
1565 "Legalizing the call start should have legalized this node!");
1566 return I->second;
1567 }
1568
1569 // Otherwise, the call start has been legalized and everything is going
1570 // according to plan. Just legalize ourselves normally here.
1571 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1572 // Do not try to legalize the target-specific arguments (#1+), except for
1573 // an optional flag input.
1574 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1575 if (Tmp1 != Node->getOperand(0)) {
1576 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1577 Ops[0] = Tmp1;
1578 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1579 }
1580 } else {
1581 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1582 if (Tmp1 != Node->getOperand(0) ||
1583 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1584 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1585 Ops[0] = Tmp1;
1586 Ops.back() = Tmp2;
1587 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1588 }
1589 }
1590 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1591 // This finishes up call legalization.
1592 IsLegalizingCall = false;
1593
1594 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1595 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1596 if (Node->getNumValues() == 2)
1597 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1598 return Result.getValue(Op.ResNo);
1599 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001600 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1602 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1603 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1604 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1605
1606 Tmp1 = Result.getValue(0);
1607 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001608 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609 default: assert(0 && "This action is not supported yet!");
1610 case TargetLowering::Expand: {
1611 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1612 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1613 " not tell us which reg is the stack pointer!");
1614 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001615
1616 // Chain the dynamic stack allocation so that it doesn't modify the stack
1617 // pointer when other instructions are using the stack.
1618 Chain = DAG.getCALLSEQ_START(Chain,
1619 DAG.getConstant(0, TLI.getPointerTy()));
1620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001621 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001622 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1623 Chain = SP.getValue(1);
1624 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1625 unsigned StackAlign =
1626 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1627 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001628 SP = DAG.getNode(ISD::AND, VT, SP,
1629 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001630 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001631 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1632
1633 Tmp2 =
1634 DAG.getCALLSEQ_END(Chain,
1635 DAG.getConstant(0, TLI.getPointerTy()),
1636 DAG.getConstant(0, TLI.getPointerTy()),
1637 SDOperand());
1638
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 Tmp1 = LegalizeOp(Tmp1);
1640 Tmp2 = LegalizeOp(Tmp2);
1641 break;
1642 }
1643 case TargetLowering::Custom:
1644 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1645 if (Tmp3.Val) {
1646 Tmp1 = LegalizeOp(Tmp3);
1647 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1648 }
1649 break;
1650 case TargetLowering::Legal:
1651 break;
1652 }
1653 // Since this op produce two values, make sure to remember that we
1654 // legalized both of them.
1655 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1656 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1657 return Op.ResNo ? Tmp2 : Tmp1;
1658 }
1659 case ISD::INLINEASM: {
1660 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1661 bool Changed = false;
1662 // Legalize all of the operands of the inline asm, in case they are nodes
1663 // that need to be expanded or something. Note we skip the asm string and
1664 // all of the TargetConstant flags.
1665 SDOperand Op = LegalizeOp(Ops[0]);
1666 Changed = Op != Ops[0];
1667 Ops[0] = Op;
1668
1669 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1670 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1671 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1672 for (++i; NumVals; ++i, --NumVals) {
1673 SDOperand Op = LegalizeOp(Ops[i]);
1674 if (Op != Ops[i]) {
1675 Changed = true;
1676 Ops[i] = Op;
1677 }
1678 }
1679 }
1680
1681 if (HasInFlag) {
1682 Op = LegalizeOp(Ops.back());
1683 Changed |= Op != Ops.back();
1684 Ops.back() = Op;
1685 }
1686
1687 if (Changed)
1688 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1689
1690 // INLINE asm returns a chain and flag, make sure to add both to the map.
1691 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1692 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1693 return Result.getValue(Op.ResNo);
1694 }
1695 case ISD::BR:
1696 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1697 // Ensure that libcalls are emitted before a branch.
1698 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1699 Tmp1 = LegalizeOp(Tmp1);
1700 LastCALLSEQ_END = DAG.getEntryNode();
1701
1702 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1703 break;
1704 case ISD::BRIND:
1705 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1706 // Ensure that libcalls are emitted before a branch.
1707 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1708 Tmp1 = LegalizeOp(Tmp1);
1709 LastCALLSEQ_END = DAG.getEntryNode();
1710
1711 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1712 default: assert(0 && "Indirect target must be legal type (pointer)!");
1713 case Legal:
1714 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1715 break;
1716 }
1717 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1718 break;
1719 case ISD::BR_JT:
1720 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1721 // Ensure that libcalls are emitted before a branch.
1722 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1723 Tmp1 = LegalizeOp(Tmp1);
1724 LastCALLSEQ_END = DAG.getEntryNode();
1725
1726 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1727 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1728
1729 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1730 default: assert(0 && "This action is not supported yet!");
1731 case TargetLowering::Legal: break;
1732 case TargetLowering::Custom:
1733 Tmp1 = TLI.LowerOperation(Result, DAG);
1734 if (Tmp1.Val) Result = Tmp1;
1735 break;
1736 case TargetLowering::Expand: {
1737 SDOperand Chain = Result.getOperand(0);
1738 SDOperand Table = Result.getOperand(1);
1739 SDOperand Index = Result.getOperand(2);
1740
1741 MVT::ValueType PTy = TLI.getPointerTy();
1742 MachineFunction &MF = DAG.getMachineFunction();
1743 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1744 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1745 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1746
1747 SDOperand LD;
1748 switch (EntrySize) {
1749 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001750 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001751 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001752 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001753 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 }
1755
Evan Cheng6fb06762007-11-09 01:32:10 +00001756 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1758 // For PIC, the sequence is:
1759 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001760 // RelocBase can be JumpTable, GOT or some sort of global base.
1761 if (PTy != MVT::i32)
1762 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1763 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1764 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001765 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001766 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 }
1768 }
1769 break;
1770 case ISD::BRCOND:
1771 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1772 // Ensure that libcalls are emitted before a return.
1773 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1774 Tmp1 = LegalizeOp(Tmp1);
1775 LastCALLSEQ_END = DAG.getEntryNode();
1776
1777 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1778 case Expand: assert(0 && "It's impossible to expand bools");
1779 case Legal:
1780 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1781 break;
1782 case Promote:
1783 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1784
1785 // The top bits of the promoted condition are not necessarily zero, ensure
1786 // that the value is properly zero extended.
1787 if (!DAG.MaskedValueIsZero(Tmp2,
1788 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1789 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1790 break;
1791 }
1792
1793 // Basic block destination (Op#2) is always legal.
1794 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1795
1796 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1797 default: assert(0 && "This action is not supported yet!");
1798 case TargetLowering::Legal: break;
1799 case TargetLowering::Custom:
1800 Tmp1 = TLI.LowerOperation(Result, DAG);
1801 if (Tmp1.Val) Result = Tmp1;
1802 break;
1803 case TargetLowering::Expand:
1804 // Expand brcond's setcc into its constituent parts and create a BR_CC
1805 // Node.
1806 if (Tmp2.getOpcode() == ISD::SETCC) {
1807 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1808 Tmp2.getOperand(0), Tmp2.getOperand(1),
1809 Node->getOperand(2));
1810 } else {
1811 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1812 DAG.getCondCode(ISD::SETNE), Tmp2,
1813 DAG.getConstant(0, Tmp2.getValueType()),
1814 Node->getOperand(2));
1815 }
1816 break;
1817 }
1818 break;
1819 case ISD::BR_CC:
1820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1821 // Ensure that libcalls are emitted before a branch.
1822 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1823 Tmp1 = LegalizeOp(Tmp1);
1824 Tmp2 = Node->getOperand(2); // LHS
1825 Tmp3 = Node->getOperand(3); // RHS
1826 Tmp4 = Node->getOperand(1); // CC
1827
1828 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1829 LastCALLSEQ_END = DAG.getEntryNode();
1830
1831 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1832 // the LHS is a legal SETCC itself. In this case, we need to compare
1833 // the result against zero to select between true and false values.
1834 if (Tmp3.Val == 0) {
1835 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1836 Tmp4 = DAG.getCondCode(ISD::SETNE);
1837 }
1838
1839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1840 Node->getOperand(4));
1841
1842 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1843 default: assert(0 && "Unexpected action for BR_CC!");
1844 case TargetLowering::Legal: break;
1845 case TargetLowering::Custom:
1846 Tmp4 = TLI.LowerOperation(Result, DAG);
1847 if (Tmp4.Val) Result = Tmp4;
1848 break;
1849 }
1850 break;
1851 case ISD::LOAD: {
1852 LoadSDNode *LD = cast<LoadSDNode>(Node);
1853 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1854 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1855
1856 ISD::LoadExtType ExtType = LD->getExtensionType();
1857 if (ExtType == ISD::NON_EXTLOAD) {
1858 MVT::ValueType VT = Node->getValueType(0);
1859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1860 Tmp3 = Result.getValue(0);
1861 Tmp4 = Result.getValue(1);
1862
1863 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1864 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001865 case TargetLowering::Legal:
1866 // If this is an unaligned load and the target doesn't support it,
1867 // expand it.
1868 if (!TLI.allowsUnalignedMemoryAccesses()) {
1869 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001870 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001871 if (LD->getAlignment() < ABIAlignment){
1872 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1873 TLI);
1874 Tmp3 = Result.getOperand(0);
1875 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001876 Tmp3 = LegalizeOp(Tmp3);
1877 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001878 }
1879 }
1880 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 case TargetLowering::Custom:
1882 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1883 if (Tmp1.Val) {
1884 Tmp3 = LegalizeOp(Tmp1);
1885 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1886 }
1887 break;
1888 case TargetLowering::Promote: {
1889 // Only promote a load of vector type to another.
1890 assert(MVT::isVector(VT) && "Cannot promote this load!");
1891 // Change base type to a different vector type.
1892 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1893
1894 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1895 LD->getSrcValueOffset(),
1896 LD->isVolatile(), LD->getAlignment());
1897 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1898 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1899 break;
1900 }
1901 }
1902 // Since loads produce two values, make sure to remember that we
1903 // legalized both of them.
1904 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1905 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1906 return Op.ResNo ? Tmp4 : Tmp3;
1907 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001908 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands082524c2008-01-23 20:39:46 +00001909 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1910 int SVOffset = LD->getSrcValueOffset();
1911 unsigned Alignment = LD->getAlignment();
1912 bool isVolatile = LD->isVolatile();
1913
1914 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1915 // Some targets pretend to have an i1 loading operation, and actually
1916 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1917 // bits are guaranteed to be zero; it helps the optimizers understand
1918 // that these bits are zero. It is also useful for EXTLOAD, since it
1919 // tells the optimizers that those bits are undefined. It would be
1920 // nice to have an effective generic way of getting these benefits...
1921 // Until such a way is found, don't insist on promoting i1 here.
1922 (SrcVT != MVT::i1 ||
1923 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1924 // Promote to a byte-sized load if not loading an integral number of
1925 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1926 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1927 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1928 SDOperand Ch;
1929
1930 // The extra bits are guaranteed to be zero, since we stored them that
1931 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1932
1933 ISD::LoadExtType NewExtType =
1934 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1935
1936 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1937 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1938 NVT, isVolatile, Alignment);
1939
1940 Ch = Result.getValue(1); // The chain.
1941
1942 if (ExtType == ISD::SEXTLOAD)
1943 // Having the top bits zero doesn't help when sign extending.
1944 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1945 Result, DAG.getValueType(SrcVT));
1946 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1947 // All the top bits are guaranteed to be zero - inform the optimizers.
1948 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1949 DAG.getValueType(SrcVT));
1950
1951 Tmp1 = LegalizeOp(Result);
1952 Tmp2 = LegalizeOp(Ch);
1953 } else if (SrcWidth & (SrcWidth - 1)) {
1954 // If not loading a power-of-2 number of bits, expand as two loads.
1955 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1956 "Unsupported extload!");
1957 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1958 assert(RoundWidth < SrcWidth);
1959 unsigned ExtraWidth = SrcWidth - RoundWidth;
1960 assert(ExtraWidth < RoundWidth);
1961 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1962 "Load size not an integral number of bytes!");
1963 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1964 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1965 SDOperand Lo, Hi, Ch;
1966 unsigned IncrementSize;
1967
1968 if (TLI.isLittleEndian()) {
1969 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1970 // Load the bottom RoundWidth bits.
1971 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1972 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1973 Alignment);
1974
1975 // Load the remaining ExtraWidth bits.
1976 IncrementSize = RoundWidth / 8;
1977 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1978 DAG.getIntPtrConstant(IncrementSize));
1979 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1980 LD->getSrcValue(), SVOffset + IncrementSize,
1981 ExtraVT, isVolatile,
1982 MinAlign(Alignment, IncrementSize));
1983
1984 // Build a factor node to remember that this load is independent of the
1985 // other one.
1986 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1987 Hi.getValue(1));
1988
1989 // Move the top bits to the right place.
1990 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1991 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1992
1993 // Join the hi and lo parts.
1994 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001995 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00001996 // Big endian - avoid unaligned loads.
1997 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1998 // Load the top RoundWidth bits.
1999 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2000 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2001 Alignment);
2002
2003 // Load the remaining ExtraWidth bits.
2004 IncrementSize = RoundWidth / 8;
2005 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2006 DAG.getIntPtrConstant(IncrementSize));
2007 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2008 LD->getSrcValue(), SVOffset + IncrementSize,
2009 ExtraVT, isVolatile,
2010 MinAlign(Alignment, IncrementSize));
2011
2012 // Build a factor node to remember that this load is independent of the
2013 // other one.
2014 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2015 Hi.getValue(1));
2016
2017 // Move the top bits to the right place.
2018 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2019 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2020
2021 // Join the hi and lo parts.
2022 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2023 }
2024
2025 Tmp1 = LegalizeOp(Result);
2026 Tmp2 = LegalizeOp(Ch);
2027 } else {
2028 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2029 default: assert(0 && "This action is not supported yet!");
2030 case TargetLowering::Custom:
2031 isCustom = true;
2032 // FALLTHROUGH
2033 case TargetLowering::Legal:
2034 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2035 Tmp1 = Result.getValue(0);
2036 Tmp2 = Result.getValue(1);
2037
2038 if (isCustom) {
2039 Tmp3 = TLI.LowerOperation(Result, DAG);
2040 if (Tmp3.Val) {
2041 Tmp1 = LegalizeOp(Tmp3);
2042 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2043 }
2044 } else {
2045 // If this is an unaligned load and the target doesn't support it,
2046 // expand it.
2047 if (!TLI.allowsUnalignedMemoryAccesses()) {
2048 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002049 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands082524c2008-01-23 20:39:46 +00002050 if (LD->getAlignment() < ABIAlignment){
2051 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2052 TLI);
2053 Tmp1 = Result.getOperand(0);
2054 Tmp2 = Result.getOperand(1);
2055 Tmp1 = LegalizeOp(Tmp1);
2056 Tmp2 = LegalizeOp(Tmp2);
2057 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002058 }
2059 }
Duncan Sands082524c2008-01-23 20:39:46 +00002060 break;
2061 case TargetLowering::Expand:
2062 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2063 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2064 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2065 LD->getSrcValueOffset(),
2066 LD->isVolatile(), LD->getAlignment());
2067 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2068 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2069 Tmp2 = LegalizeOp(Load.getValue(1));
2070 break;
2071 }
2072 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2073 // Turn the unsupported load into an EXTLOAD followed by an explicit
2074 // zero/sign extend inreg.
2075 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2076 Tmp1, Tmp2, LD->getSrcValue(),
2077 LD->getSrcValueOffset(), SrcVT,
2078 LD->isVolatile(), LD->getAlignment());
2079 SDOperand ValRes;
2080 if (ExtType == ISD::SEXTLOAD)
2081 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2082 Result, DAG.getValueType(SrcVT));
2083 else
2084 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2085 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2086 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002087 break;
2088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 }
Duncan Sands082524c2008-01-23 20:39:46 +00002090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 // Since loads produce two values, make sure to remember that we legalized
2092 // both of them.
2093 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2094 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2095 return Op.ResNo ? Tmp2 : Tmp1;
2096 }
2097 }
2098 case ISD::EXTRACT_ELEMENT: {
2099 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2100 switch (getTypeAction(OpTy)) {
2101 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2102 case Legal:
2103 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2104 // 1 -> Hi
2105 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2106 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2107 TLI.getShiftAmountTy()));
2108 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2109 } else {
2110 // 0 -> Lo
2111 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2112 Node->getOperand(0));
2113 }
2114 break;
2115 case Expand:
2116 // Get both the low and high parts.
2117 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2118 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2119 Result = Tmp2; // 1 -> Hi
2120 else
2121 Result = Tmp1; // 0 -> Lo
2122 break;
2123 }
2124 break;
2125 }
2126
2127 case ISD::CopyToReg:
2128 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2129
2130 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2131 "Register type must be legal!");
2132 // Legalize the incoming value (must be a legal type).
2133 Tmp2 = LegalizeOp(Node->getOperand(2));
2134 if (Node->getNumValues() == 1) {
2135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2136 } else {
2137 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2138 if (Node->getNumOperands() == 4) {
2139 Tmp3 = LegalizeOp(Node->getOperand(3));
2140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2141 Tmp3);
2142 } else {
2143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2144 }
2145
2146 // Since this produces two values, make sure to remember that we legalized
2147 // both of them.
2148 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2149 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2150 return Result;
2151 }
2152 break;
2153
2154 case ISD::RET:
2155 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2156
2157 // Ensure that libcalls are emitted before a return.
2158 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2159 Tmp1 = LegalizeOp(Tmp1);
2160 LastCALLSEQ_END = DAG.getEntryNode();
2161
2162 switch (Node->getNumOperands()) {
2163 case 3: // ret val
2164 Tmp2 = Node->getOperand(1);
2165 Tmp3 = Node->getOperand(2); // Signness
2166 switch (getTypeAction(Tmp2.getValueType())) {
2167 case Legal:
2168 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2169 break;
2170 case Expand:
2171 if (!MVT::isVector(Tmp2.getValueType())) {
2172 SDOperand Lo, Hi;
2173 ExpandOp(Tmp2, Lo, Hi);
2174
2175 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002176 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002177 std::swap(Lo, Hi);
2178
2179 if (Hi.Val)
2180 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2181 else
2182 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2183 Result = LegalizeOp(Result);
2184 } else {
2185 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002186 int InIx = Tmp2.ResNo;
2187 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2188 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189
2190 // Figure out if there is a simple type corresponding to this Vector
2191 // type. If so, convert to the vector type.
2192 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2193 if (TLI.isTypeLegal(TVT)) {
2194 // Turn this into a return of the vector type.
2195 Tmp2 = LegalizeOp(Tmp2);
2196 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2197 } else if (NumElems == 1) {
2198 // Turn this into a return of the scalar type.
2199 Tmp2 = ScalarizeVectorOp(Tmp2);
2200 Tmp2 = LegalizeOp(Tmp2);
2201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2202
2203 // FIXME: Returns of gcc generic vectors smaller than a legal type
2204 // should be returned in integer registers!
2205
2206 // The scalarized value type may not be legal, e.g. it might require
2207 // promotion or expansion. Relegalize the return.
2208 Result = LegalizeOp(Result);
2209 } else {
2210 // FIXME: Returns of gcc generic vectors larger than a legal vector
2211 // type should be returned by reference!
2212 SDOperand Lo, Hi;
2213 SplitVectorOp(Tmp2, Lo, Hi);
2214 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2215 Result = LegalizeOp(Result);
2216 }
2217 }
2218 break;
2219 case Promote:
2220 Tmp2 = PromoteOp(Node->getOperand(1));
2221 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2222 Result = LegalizeOp(Result);
2223 break;
2224 }
2225 break;
2226 case 1: // ret void
2227 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2228 break;
2229 default: { // ret <values>
2230 SmallVector<SDOperand, 8> NewValues;
2231 NewValues.push_back(Tmp1);
2232 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2233 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2234 case Legal:
2235 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2236 NewValues.push_back(Node->getOperand(i+1));
2237 break;
2238 case Expand: {
2239 SDOperand Lo, Hi;
2240 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
2241 "FIXME: TODO: implement returning non-legal vector types!");
2242 ExpandOp(Node->getOperand(i), Lo, Hi);
2243 NewValues.push_back(Lo);
2244 NewValues.push_back(Node->getOperand(i+1));
2245 if (Hi.Val) {
2246 NewValues.push_back(Hi);
2247 NewValues.push_back(Node->getOperand(i+1));
2248 }
2249 break;
2250 }
2251 case Promote:
2252 assert(0 && "Can't promote multiple return value yet!");
2253 }
2254
2255 if (NewValues.size() == Node->getNumOperands())
2256 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2257 else
2258 Result = DAG.getNode(ISD::RET, MVT::Other,
2259 &NewValues[0], NewValues.size());
2260 break;
2261 }
2262 }
2263
2264 if (Result.getOpcode() == ISD::RET) {
2265 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2266 default: assert(0 && "This action is not supported yet!");
2267 case TargetLowering::Legal: break;
2268 case TargetLowering::Custom:
2269 Tmp1 = TLI.LowerOperation(Result, DAG);
2270 if (Tmp1.Val) Result = Tmp1;
2271 break;
2272 }
2273 }
2274 break;
2275 case ISD::STORE: {
2276 StoreSDNode *ST = cast<StoreSDNode>(Node);
2277 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2278 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2279 int SVOffset = ST->getSrcValueOffset();
2280 unsigned Alignment = ST->getAlignment();
2281 bool isVolatile = ST->isVolatile();
2282
2283 if (!ST->isTruncatingStore()) {
2284 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2285 // FIXME: We shouldn't do this for TargetConstantFP's.
2286 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2287 // to phase ordering between legalized code and the dag combiner. This
2288 // probably means that we need to integrate dag combiner and legalizer
2289 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002290 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002291 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002292 if (CFP->getValueType(0) == MVT::f32 &&
2293 getTypeAction(MVT::i32) == Legal) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00002294 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2295 convertToAPInt().getZExtValue(),
Dale Johannesen1616e902007-09-11 18:32:33 +00002296 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002297 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2298 SVOffset, isVolatile, Alignment);
2299 break;
2300 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002301 // If this target supports 64-bit registers, do a single 64-bit store.
2302 if (getTypeAction(MVT::i64) == Legal) {
2303 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2304 getZExtValue(), MVT::i64);
2305 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2306 SVOffset, isVolatile, Alignment);
2307 break;
2308 } else if (getTypeAction(MVT::i32) == Legal) {
2309 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2310 // stores. If the target supports neither 32- nor 64-bits, this
2311 // xform is certainly not worth it.
2312 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2313 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2314 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002315 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002316
2317 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2318 SVOffset, isVolatile, Alignment);
2319 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002320 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002321 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002322 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002323
2324 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2325 break;
2326 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002327 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002328 }
2329
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002330 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002331 case Legal: {
2332 Tmp3 = LegalizeOp(ST->getValue());
2333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2334 ST->getOffset());
2335
2336 MVT::ValueType VT = Tmp3.getValueType();
2337 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2338 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002339 case TargetLowering::Legal:
2340 // If this is an unaligned store and the target doesn't support it,
2341 // expand it.
2342 if (!TLI.allowsUnalignedMemoryAccesses()) {
2343 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002344 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002345 if (ST->getAlignment() < ABIAlignment)
2346 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2347 TLI);
2348 }
2349 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002350 case TargetLowering::Custom:
2351 Tmp1 = TLI.LowerOperation(Result, DAG);
2352 if (Tmp1.Val) Result = Tmp1;
2353 break;
2354 case TargetLowering::Promote:
2355 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2356 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2357 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2358 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2359 ST->getSrcValue(), SVOffset, isVolatile,
2360 Alignment);
2361 break;
2362 }
2363 break;
2364 }
2365 case Promote:
2366 // Truncate the value and store the result.
2367 Tmp3 = PromoteOp(ST->getValue());
2368 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002369 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 isVolatile, Alignment);
2371 break;
2372
2373 case Expand:
2374 unsigned IncrementSize = 0;
2375 SDOperand Lo, Hi;
2376
2377 // If this is a vector type, then we have to calculate the increment as
2378 // the product of the element size in bytes, and the number of elements
2379 // in the high half of the vector.
2380 if (MVT::isVector(ST->getValue().getValueType())) {
2381 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002382 int InIx = ST->getValue().ResNo;
Chris Lattner5872a362008-01-17 07:00:52 +00002383 MVT::ValueType InVT = InVal->getValueType(InIx);
2384 unsigned NumElems = MVT::getVectorNumElements(InVT);
2385 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386
2387 // Figure out if there is a simple type corresponding to this Vector
2388 // type. If so, convert to the vector type.
2389 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2390 if (TLI.isTypeLegal(TVT)) {
2391 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002392 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2394 SVOffset, isVolatile, Alignment);
2395 Result = LegalizeOp(Result);
2396 break;
2397 } else if (NumElems == 1) {
2398 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002399 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2401 SVOffset, isVolatile, Alignment);
2402 // The scalarized value type may not be legal, e.g. it might require
2403 // promotion or expansion. Relegalize the scalar store.
2404 Result = LegalizeOp(Result);
2405 break;
2406 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002407 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00002408 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2409 MVT::getSizeInBits(EVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 }
2411 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002412 ExpandOp(ST->getValue(), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002413 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2414
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002415 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 std::swap(Lo, Hi);
2417 }
2418
2419 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2420 SVOffset, isVolatile, Alignment);
2421
2422 if (Hi.Val == NULL) {
2423 // Must be int <-> float one-to-one expansion.
2424 Result = Lo;
2425 break;
2426 }
2427
2428 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002429 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002430 assert(isTypeLegal(Tmp2.getValueType()) &&
2431 "Pointers must be legal!");
2432 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002433 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2435 SVOffset, isVolatile, Alignment);
2436 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2437 break;
2438 }
2439 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002440 switch (getTypeAction(ST->getValue().getValueType())) {
2441 case Legal:
2442 Tmp3 = LegalizeOp(ST->getValue());
2443 break;
2444 case Promote:
2445 // We can promote the value, the truncstore will still take care of it.
2446 Tmp3 = PromoteOp(ST->getValue());
2447 break;
2448 case Expand:
2449 // Just store the low part. This may become a non-trunc store, so make
2450 // sure to use getTruncStore, not UpdateNodeOperands below.
2451 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2452 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2453 SVOffset, MVT::i8, isVolatile, Alignment);
2454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002456 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands40676662008-01-22 07:17:34 +00002457 unsigned StWidth = MVT::getSizeInBits(StVT);
2458
2459 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2460 // Promote to a byte-sized store with upper bits zero if not
2461 // storing an integral number of bytes. For example, promote
2462 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2463 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2464 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2465 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2466 SVOffset, NVT, isVolatile, Alignment);
2467 } else if (StWidth & (StWidth - 1)) {
2468 // If not storing a power-of-2 number of bits, expand as two stores.
2469 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2470 "Unsupported truncstore!");
2471 unsigned RoundWidth = 1 << Log2_32(StWidth);
2472 assert(RoundWidth < StWidth);
2473 unsigned ExtraWidth = StWidth - RoundWidth;
2474 assert(ExtraWidth < RoundWidth);
2475 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2476 "Store size not an integral number of bytes!");
2477 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2478 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2479 SDOperand Lo, Hi;
2480 unsigned IncrementSize;
2481
2482 if (TLI.isLittleEndian()) {
2483 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2484 // Store the bottom RoundWidth bits.
2485 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2486 SVOffset, RoundVT,
2487 isVolatile, Alignment);
2488
2489 // Store the remaining ExtraWidth bits.
2490 IncrementSize = RoundWidth / 8;
2491 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2492 DAG.getIntPtrConstant(IncrementSize));
2493 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2494 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2495 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2496 SVOffset + IncrementSize, ExtraVT, isVolatile,
2497 MinAlign(Alignment, IncrementSize));
2498 } else {
2499 // Big endian - avoid unaligned stores.
2500 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2501 // Store the top RoundWidth bits.
2502 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2503 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2504 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2505 RoundVT, isVolatile, Alignment);
2506
2507 // Store the remaining ExtraWidth bits.
2508 IncrementSize = RoundWidth / 8;
2509 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2510 DAG.getIntPtrConstant(IncrementSize));
2511 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2512 SVOffset + IncrementSize, ExtraVT, isVolatile,
2513 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002514 }
Duncan Sands40676662008-01-22 07:17:34 +00002515
2516 // The order of the stores doesn't matter.
2517 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2518 } else {
2519 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2520 Tmp2 != ST->getBasePtr())
2521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2522 ST->getOffset());
2523
2524 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2525 default: assert(0 && "This action is not supported yet!");
2526 case TargetLowering::Legal:
2527 // If this is an unaligned store and the target doesn't support it,
2528 // expand it.
2529 if (!TLI.allowsUnalignedMemoryAccesses()) {
2530 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002531 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands40676662008-01-22 07:17:34 +00002532 if (ST->getAlignment() < ABIAlignment)
2533 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2534 TLI);
2535 }
2536 break;
2537 case TargetLowering::Custom:
2538 Result = TLI.LowerOperation(Result, DAG);
2539 break;
2540 case Expand:
2541 // TRUNCSTORE:i16 i32 -> STORE i16
2542 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2543 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2544 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2545 isVolatile, Alignment);
2546 break;
2547 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 }
2549 }
2550 break;
2551 }
2552 case ISD::PCMARKER:
2553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2554 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2555 break;
2556 case ISD::STACKSAVE:
2557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2558 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2559 Tmp1 = Result.getValue(0);
2560 Tmp2 = Result.getValue(1);
2561
2562 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2563 default: assert(0 && "This action is not supported yet!");
2564 case TargetLowering::Legal: break;
2565 case TargetLowering::Custom:
2566 Tmp3 = TLI.LowerOperation(Result, DAG);
2567 if (Tmp3.Val) {
2568 Tmp1 = LegalizeOp(Tmp3);
2569 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2570 }
2571 break;
2572 case TargetLowering::Expand:
2573 // Expand to CopyFromReg if the target set
2574 // StackPointerRegisterToSaveRestore.
2575 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2576 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2577 Node->getValueType(0));
2578 Tmp2 = Tmp1.getValue(1);
2579 } else {
2580 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2581 Tmp2 = Node->getOperand(0);
2582 }
2583 break;
2584 }
2585
2586 // Since stacksave produce two values, make sure to remember that we
2587 // legalized both of them.
2588 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2589 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2590 return Op.ResNo ? Tmp2 : Tmp1;
2591
2592 case ISD::STACKRESTORE:
2593 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2594 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2596
2597 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2598 default: assert(0 && "This action is not supported yet!");
2599 case TargetLowering::Legal: break;
2600 case TargetLowering::Custom:
2601 Tmp1 = TLI.LowerOperation(Result, DAG);
2602 if (Tmp1.Val) Result = Tmp1;
2603 break;
2604 case TargetLowering::Expand:
2605 // Expand to CopyToReg if the target set
2606 // StackPointerRegisterToSaveRestore.
2607 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2608 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2609 } else {
2610 Result = Tmp1;
2611 }
2612 break;
2613 }
2614 break;
2615
2616 case ISD::READCYCLECOUNTER:
2617 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2618 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2619 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2620 Node->getValueType(0))) {
2621 default: assert(0 && "This action is not supported yet!");
2622 case TargetLowering::Legal:
2623 Tmp1 = Result.getValue(0);
2624 Tmp2 = Result.getValue(1);
2625 break;
2626 case TargetLowering::Custom:
2627 Result = TLI.LowerOperation(Result, DAG);
2628 Tmp1 = LegalizeOp(Result.getValue(0));
2629 Tmp2 = LegalizeOp(Result.getValue(1));
2630 break;
2631 }
2632
2633 // Since rdcc produce two values, make sure to remember that we legalized
2634 // both of them.
2635 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2636 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2637 return Result;
2638
2639 case ISD::SELECT:
2640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2641 case Expand: assert(0 && "It's impossible to expand bools");
2642 case Legal:
2643 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2644 break;
2645 case Promote:
2646 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2647 // Make sure the condition is either zero or one.
2648 if (!DAG.MaskedValueIsZero(Tmp1,
2649 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2650 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2651 break;
2652 }
2653 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2654 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2655
2656 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2657
2658 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2659 default: assert(0 && "This action is not supported yet!");
2660 case TargetLowering::Legal: break;
2661 case TargetLowering::Custom: {
2662 Tmp1 = TLI.LowerOperation(Result, DAG);
2663 if (Tmp1.Val) Result = Tmp1;
2664 break;
2665 }
2666 case TargetLowering::Expand:
2667 if (Tmp1.getOpcode() == ISD::SETCC) {
2668 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2669 Tmp2, Tmp3,
2670 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2671 } else {
2672 Result = DAG.getSelectCC(Tmp1,
2673 DAG.getConstant(0, Tmp1.getValueType()),
2674 Tmp2, Tmp3, ISD::SETNE);
2675 }
2676 break;
2677 case TargetLowering::Promote: {
2678 MVT::ValueType NVT =
2679 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2680 unsigned ExtOp, TruncOp;
2681 if (MVT::isVector(Tmp2.getValueType())) {
2682 ExtOp = ISD::BIT_CONVERT;
2683 TruncOp = ISD::BIT_CONVERT;
2684 } else if (MVT::isInteger(Tmp2.getValueType())) {
2685 ExtOp = ISD::ANY_EXTEND;
2686 TruncOp = ISD::TRUNCATE;
2687 } else {
2688 ExtOp = ISD::FP_EXTEND;
2689 TruncOp = ISD::FP_ROUND;
2690 }
2691 // Promote each of the values to the new type.
2692 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2693 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2694 // Perform the larger operation, then round down.
2695 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002696 if (TruncOp != ISD::FP_ROUND)
2697 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2698 else
2699 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2700 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 break;
2702 }
2703 }
2704 break;
2705 case ISD::SELECT_CC: {
2706 Tmp1 = Node->getOperand(0); // LHS
2707 Tmp2 = Node->getOperand(1); // RHS
2708 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2709 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2710 SDOperand CC = Node->getOperand(4);
2711
2712 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2713
2714 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2715 // the LHS is a legal SETCC itself. In this case, we need to compare
2716 // the result against zero to select between true and false values.
2717 if (Tmp2.Val == 0) {
2718 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2719 CC = DAG.getCondCode(ISD::SETNE);
2720 }
2721 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2722
2723 // Everything is legal, see if we should expand this op or something.
2724 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2725 default: assert(0 && "This action is not supported yet!");
2726 case TargetLowering::Legal: break;
2727 case TargetLowering::Custom:
2728 Tmp1 = TLI.LowerOperation(Result, DAG);
2729 if (Tmp1.Val) Result = Tmp1;
2730 break;
2731 }
2732 break;
2733 }
2734 case ISD::SETCC:
2735 Tmp1 = Node->getOperand(0);
2736 Tmp2 = Node->getOperand(1);
2737 Tmp3 = Node->getOperand(2);
2738 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2739
2740 // If we had to Expand the SetCC operands into a SELECT node, then it may
2741 // not always be possible to return a true LHS & RHS. In this case, just
2742 // return the value we legalized, returned in the LHS
2743 if (Tmp2.Val == 0) {
2744 Result = Tmp1;
2745 break;
2746 }
2747
2748 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2749 default: assert(0 && "Cannot handle this action for SETCC yet!");
2750 case TargetLowering::Custom:
2751 isCustom = true;
2752 // FALLTHROUGH.
2753 case TargetLowering::Legal:
2754 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2755 if (isCustom) {
2756 Tmp4 = TLI.LowerOperation(Result, DAG);
2757 if (Tmp4.Val) Result = Tmp4;
2758 }
2759 break;
2760 case TargetLowering::Promote: {
2761 // First step, figure out the appropriate operation to use.
2762 // Allow SETCC to not be supported for all legal data types
2763 // Mostly this targets FP
2764 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2765 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2766
2767 // Scan for the appropriate larger type to use.
2768 while (1) {
2769 NewInTy = (MVT::ValueType)(NewInTy+1);
2770
2771 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2772 "Fell off of the edge of the integer world");
2773 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2774 "Fell off of the edge of the floating point world");
2775
2776 // If the target supports SETCC of this type, use it.
2777 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2778 break;
2779 }
2780 if (MVT::isInteger(NewInTy))
2781 assert(0 && "Cannot promote Legal Integer SETCC yet");
2782 else {
2783 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2784 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2785 }
2786 Tmp1 = LegalizeOp(Tmp1);
2787 Tmp2 = LegalizeOp(Tmp2);
2788 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2789 Result = LegalizeOp(Result);
2790 break;
2791 }
2792 case TargetLowering::Expand:
2793 // Expand a setcc node into a select_cc of the same condition, lhs, and
2794 // rhs that selects between const 1 (true) and const 0 (false).
2795 MVT::ValueType VT = Node->getValueType(0);
2796 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2797 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2798 Tmp3);
2799 break;
2800 }
2801 break;
2802 case ISD::MEMSET:
2803 case ISD::MEMCPY:
2804 case ISD::MEMMOVE: {
2805 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2806 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2807
2808 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2809 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2810 case Expand: assert(0 && "Cannot expand a byte!");
2811 case Legal:
2812 Tmp3 = LegalizeOp(Node->getOperand(2));
2813 break;
2814 case Promote:
2815 Tmp3 = PromoteOp(Node->getOperand(2));
2816 break;
2817 }
2818 } else {
2819 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2820 }
2821
2822 SDOperand Tmp4;
2823 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2824 case Expand: {
2825 // Length is too big, just take the lo-part of the length.
2826 SDOperand HiPart;
2827 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2828 break;
2829 }
2830 case Legal:
2831 Tmp4 = LegalizeOp(Node->getOperand(3));
2832 break;
2833 case Promote:
2834 Tmp4 = PromoteOp(Node->getOperand(3));
2835 break;
2836 }
2837
2838 SDOperand Tmp5;
2839 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2840 case Expand: assert(0 && "Cannot expand this yet!");
2841 case Legal:
2842 Tmp5 = LegalizeOp(Node->getOperand(4));
2843 break;
2844 case Promote:
2845 Tmp5 = PromoteOp(Node->getOperand(4));
2846 break;
2847 }
2848
Rafael Espindola80825902007-10-19 10:41:11 +00002849 SDOperand Tmp6;
2850 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2851 case Expand: assert(0 && "Cannot expand this yet!");
2852 case Legal:
2853 Tmp6 = LegalizeOp(Node->getOperand(5));
2854 break;
2855 case Promote:
2856 Tmp6 = PromoteOp(Node->getOperand(5));
2857 break;
2858 }
2859
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002860 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2861 default: assert(0 && "This action not implemented for this operation!");
2862 case TargetLowering::Custom:
2863 isCustom = true;
2864 // FALLTHROUGH
Rafael Espindola80825902007-10-19 10:41:11 +00002865 case TargetLowering::Legal: {
2866 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2867 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 if (isCustom) {
2869 Tmp1 = TLI.LowerOperation(Result, DAG);
2870 if (Tmp1.Val) Result = Tmp1;
2871 }
2872 break;
Rafael Espindola80825902007-10-19 10:41:11 +00002873 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874 case TargetLowering::Expand: {
2875 // Otherwise, the target does not support this operation. Lower the
2876 // operation to an explicit libcall as appropriate.
2877 MVT::ValueType IntPtr = TLI.getPointerTy();
2878 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2879 TargetLowering::ArgListTy Args;
2880 TargetLowering::ArgListEntry Entry;
2881
2882 const char *FnName = 0;
2883 if (Node->getOpcode() == ISD::MEMSET) {
2884 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2885 Args.push_back(Entry);
2886 // Extend the (previously legalized) ubyte argument to be an int value
2887 // for the call.
2888 if (Tmp3.getValueType() > MVT::i32)
2889 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2890 else
2891 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2892 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2893 Args.push_back(Entry);
2894 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2895 Args.push_back(Entry);
2896
2897 FnName = "memset";
2898 } else if (Node->getOpcode() == ISD::MEMCPY ||
2899 Node->getOpcode() == ISD::MEMMOVE) {
2900 Entry.Ty = IntPtrTy;
2901 Entry.Node = Tmp2; Args.push_back(Entry);
2902 Entry.Node = Tmp3; Args.push_back(Entry);
2903 Entry.Node = Tmp4; Args.push_back(Entry);
2904 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2905 } else {
2906 assert(0 && "Unknown op!");
2907 }
2908
2909 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00002910 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2911 false, false, false, CallingConv::C, false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2913 Result = CallResult.second;
2914 break;
2915 }
2916 }
2917 break;
2918 }
2919
2920 case ISD::SHL_PARTS:
2921 case ISD::SRA_PARTS:
2922 case ISD::SRL_PARTS: {
2923 SmallVector<SDOperand, 8> Ops;
2924 bool Changed = false;
2925 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2926 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2927 Changed |= Ops.back() != Node->getOperand(i);
2928 }
2929 if (Changed)
2930 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2931
2932 switch (TLI.getOperationAction(Node->getOpcode(),
2933 Node->getValueType(0))) {
2934 default: assert(0 && "This action is not supported yet!");
2935 case TargetLowering::Legal: break;
2936 case TargetLowering::Custom:
2937 Tmp1 = TLI.LowerOperation(Result, DAG);
2938 if (Tmp1.Val) {
2939 SDOperand Tmp2, RetVal(0, 0);
2940 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2941 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2942 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2943 if (i == Op.ResNo)
2944 RetVal = Tmp2;
2945 }
2946 assert(RetVal.Val && "Illegal result number");
2947 return RetVal;
2948 }
2949 break;
2950 }
2951
2952 // Since these produce multiple values, make sure to remember that we
2953 // legalized all of them.
2954 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2955 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2956 return Result.getValue(Op.ResNo);
2957 }
2958
2959 // Binary operators
2960 case ISD::ADD:
2961 case ISD::SUB:
2962 case ISD::MUL:
2963 case ISD::MULHS:
2964 case ISD::MULHU:
2965 case ISD::UDIV:
2966 case ISD::SDIV:
2967 case ISD::AND:
2968 case ISD::OR:
2969 case ISD::XOR:
2970 case ISD::SHL:
2971 case ISD::SRL:
2972 case ISD::SRA:
2973 case ISD::FADD:
2974 case ISD::FSUB:
2975 case ISD::FMUL:
2976 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002977 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2979 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2980 case Expand: assert(0 && "Not possible");
2981 case Legal:
2982 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2983 break;
2984 case Promote:
2985 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2986 break;
2987 }
2988
2989 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2990
2991 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2992 default: assert(0 && "BinOp legalize operation not supported");
2993 case TargetLowering::Legal: break;
2994 case TargetLowering::Custom:
2995 Tmp1 = TLI.LowerOperation(Result, DAG);
2996 if (Tmp1.Val) Result = Tmp1;
2997 break;
2998 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00002999 MVT::ValueType VT = Op.getValueType();
3000
3001 // See if multiply or divide can be lowered using two-result operations.
3002 SDVTList VTs = DAG.getVTList(VT, VT);
3003 if (Node->getOpcode() == ISD::MUL) {
3004 // We just need the low half of the multiply; try both the signed
3005 // and unsigned forms. If the target supports both SMUL_LOHI and
3006 // UMUL_LOHI, form a preference by checking which forms of plain
3007 // MULH it supports.
3008 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3009 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3010 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3011 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3012 unsigned OpToUse = 0;
3013 if (HasSMUL_LOHI && !HasMULHS) {
3014 OpToUse = ISD::SMUL_LOHI;
3015 } else if (HasUMUL_LOHI && !HasMULHU) {
3016 OpToUse = ISD::UMUL_LOHI;
3017 } else if (HasSMUL_LOHI) {
3018 OpToUse = ISD::SMUL_LOHI;
3019 } else if (HasUMUL_LOHI) {
3020 OpToUse = ISD::UMUL_LOHI;
3021 }
3022 if (OpToUse) {
3023 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3024 break;
3025 }
3026 }
3027 if (Node->getOpcode() == ISD::MULHS &&
3028 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3029 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3030 break;
3031 }
3032 if (Node->getOpcode() == ISD::MULHU &&
3033 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3034 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3035 break;
3036 }
3037 if (Node->getOpcode() == ISD::SDIV &&
3038 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3039 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3040 break;
3041 }
3042 if (Node->getOpcode() == ISD::UDIV &&
3043 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3044 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3045 break;
3046 }
3047
Dan Gohman6d05cac2007-10-11 23:57:53 +00003048 // Check to see if we have a libcall for this operator.
3049 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3050 bool isSigned = false;
3051 switch (Node->getOpcode()) {
3052 case ISD::UDIV:
3053 case ISD::SDIV:
3054 if (VT == MVT::i32) {
3055 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003057 isSigned = Node->getOpcode() == ISD::SDIV;
3058 }
3059 break;
3060 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003061 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3062 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003063 break;
3064 default: break;
3065 }
3066 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3067 SDOperand Dummy;
3068 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003069 break;
3070 }
3071
3072 assert(MVT::isVector(Node->getValueType(0)) &&
3073 "Cannot expand this binary operator!");
3074 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003075 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003076 break;
3077 }
3078 case TargetLowering::Promote: {
3079 switch (Node->getOpcode()) {
3080 default: assert(0 && "Do not know how to promote this BinOp!");
3081 case ISD::AND:
3082 case ISD::OR:
3083 case ISD::XOR: {
3084 MVT::ValueType OVT = Node->getValueType(0);
3085 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3086 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3087 // Bit convert each of the values to the new type.
3088 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3089 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3090 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3091 // Bit convert the result back the original type.
3092 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3093 break;
3094 }
3095 }
3096 }
3097 }
3098 break;
3099
Dan Gohman475cd732007-10-05 14:17:22 +00003100 case ISD::SMUL_LOHI:
3101 case ISD::UMUL_LOHI:
3102 case ISD::SDIVREM:
3103 case ISD::UDIVREM:
3104 // These nodes will only be produced by target-specific lowering, so
3105 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003106 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003107 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003108
3109 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3110 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003112 break;
3113
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3115 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3116 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3117 case Expand: assert(0 && "Not possible");
3118 case Legal:
3119 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3120 break;
3121 case Promote:
3122 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3123 break;
3124 }
3125
3126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3127
3128 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3129 default: assert(0 && "Operation not supported");
3130 case TargetLowering::Custom:
3131 Tmp1 = TLI.LowerOperation(Result, DAG);
3132 if (Tmp1.Val) Result = Tmp1;
3133 break;
3134 case TargetLowering::Legal: break;
3135 case TargetLowering::Expand: {
3136 // If this target supports fabs/fneg natively and select is cheap,
3137 // do this efficiently.
3138 if (!TLI.isSelectExpensive() &&
3139 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3140 TargetLowering::Legal &&
3141 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3142 TargetLowering::Legal) {
3143 // Get the sign bit of the RHS.
3144 MVT::ValueType IVT =
3145 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3146 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3147 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3148 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3149 // Get the absolute value of the result.
3150 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3151 // Select between the nabs and abs value based on the sign bit of
3152 // the input.
3153 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3154 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3155 AbsVal),
3156 AbsVal);
3157 Result = LegalizeOp(Result);
3158 break;
3159 }
3160
3161 // Otherwise, do bitwise ops!
3162 MVT::ValueType NVT =
3163 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3164 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3165 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3166 Result = LegalizeOp(Result);
3167 break;
3168 }
3169 }
3170 break;
3171
3172 case ISD::ADDC:
3173 case ISD::SUBC:
3174 Tmp1 = LegalizeOp(Node->getOperand(0));
3175 Tmp2 = LegalizeOp(Node->getOperand(1));
3176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3177 // Since this produces two values, make sure to remember that we legalized
3178 // both of them.
3179 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3180 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3181 return Result;
3182
3183 case ISD::ADDE:
3184 case ISD::SUBE:
3185 Tmp1 = LegalizeOp(Node->getOperand(0));
3186 Tmp2 = LegalizeOp(Node->getOperand(1));
3187 Tmp3 = LegalizeOp(Node->getOperand(2));
3188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3189 // Since this produces two values, make sure to remember that we legalized
3190 // both of them.
3191 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3192 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3193 return Result;
3194
3195 case ISD::BUILD_PAIR: {
3196 MVT::ValueType PairTy = Node->getValueType(0);
3197 // TODO: handle the case where the Lo and Hi operands are not of legal type
3198 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3199 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3200 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3201 case TargetLowering::Promote:
3202 case TargetLowering::Custom:
3203 assert(0 && "Cannot promote/custom this yet!");
3204 case TargetLowering::Legal:
3205 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3206 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3207 break;
3208 case TargetLowering::Expand:
3209 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3210 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3211 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3212 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3213 TLI.getShiftAmountTy()));
3214 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3215 break;
3216 }
3217 break;
3218 }
3219
3220 case ISD::UREM:
3221 case ISD::SREM:
3222 case ISD::FREM:
3223 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3224 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3225
3226 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3227 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3228 case TargetLowering::Custom:
3229 isCustom = true;
3230 // FALLTHROUGH
3231 case TargetLowering::Legal:
3232 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3233 if (isCustom) {
3234 Tmp1 = TLI.LowerOperation(Result, DAG);
3235 if (Tmp1.Val) Result = Tmp1;
3236 }
3237 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003238 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3240 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00003241 MVT::ValueType VT = Node->getValueType(0);
3242
3243 // See if remainder can be lowered using two-result operations.
3244 SDVTList VTs = DAG.getVTList(VT, VT);
3245 if (Node->getOpcode() == ISD::SREM &&
3246 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3247 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3248 break;
3249 }
3250 if (Node->getOpcode() == ISD::UREM &&
3251 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3252 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3253 break;
3254 }
3255
3256 if (MVT::isInteger(VT)) {
3257 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 TargetLowering::Legal) {
3259 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3261 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3262 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003263 } else if (MVT::isVector(VT)) {
3264 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003265 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003266 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267 "Cannot expand this binary operator!");
3268 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3269 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3270 SDOperand Dummy;
3271 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
3272 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003273 } else {
3274 assert(MVT::isFloatingPoint(VT) &&
3275 "remainder op must have integer or floating-point type");
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003276 if (MVT::isVector(VT)) {
3277 Result = LegalizeOp(UnrollVectorOp(Op));
3278 } else {
3279 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003280 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3281 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003282 SDOperand Dummy;
3283 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3284 false/*sign irrelevant*/, Dummy);
3285 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 }
3287 break;
3288 }
Dan Gohman5a199552007-10-08 18:33:35 +00003289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 break;
3291 case ISD::VAARG: {
3292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3294
3295 MVT::ValueType VT = Node->getValueType(0);
3296 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3297 default: assert(0 && "This action is not supported yet!");
3298 case TargetLowering::Custom:
3299 isCustom = true;
3300 // FALLTHROUGH
3301 case TargetLowering::Legal:
3302 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3303 Result = Result.getValue(0);
3304 Tmp1 = Result.getValue(1);
3305
3306 if (isCustom) {
3307 Tmp2 = TLI.LowerOperation(Result, DAG);
3308 if (Tmp2.Val) {
3309 Result = LegalizeOp(Tmp2);
3310 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3311 }
3312 }
3313 break;
3314 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003315 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3316 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 // Increment the pointer, VAList, to the next vaarg
3318 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3319 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3320 TLI.getPointerTy()));
3321 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003322 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003323 // Load the actual argument out of the pointer VAList
3324 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3325 Tmp1 = LegalizeOp(Result.getValue(1));
3326 Result = LegalizeOp(Result);
3327 break;
3328 }
3329 }
3330 // Since VAARG produces two values, make sure to remember that we
3331 // legalized both of them.
3332 AddLegalizedOperand(SDOperand(Node, 0), Result);
3333 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3334 return Op.ResNo ? Tmp1 : Result;
3335 }
3336
3337 case ISD::VACOPY:
3338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3339 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3340 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3341
3342 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3343 default: assert(0 && "This action is not supported yet!");
3344 case TargetLowering::Custom:
3345 isCustom = true;
3346 // FALLTHROUGH
3347 case TargetLowering::Legal:
3348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3349 Node->getOperand(3), Node->getOperand(4));
3350 if (isCustom) {
3351 Tmp1 = TLI.LowerOperation(Result, DAG);
3352 if (Tmp1.Val) Result = Tmp1;
3353 }
3354 break;
3355 case TargetLowering::Expand:
3356 // This defaults to loading a pointer from the input and storing it to the
3357 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003358 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3359 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3360 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3361 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 break;
3363 }
3364 break;
3365
3366 case ISD::VAEND:
3367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3368 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3369
3370 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3371 default: assert(0 && "This action is not supported yet!");
3372 case TargetLowering::Custom:
3373 isCustom = true;
3374 // FALLTHROUGH
3375 case TargetLowering::Legal:
3376 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3377 if (isCustom) {
3378 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3379 if (Tmp1.Val) Result = Tmp1;
3380 }
3381 break;
3382 case TargetLowering::Expand:
3383 Result = Tmp1; // Default to a no-op, return the chain
3384 break;
3385 }
3386 break;
3387
3388 case ISD::VASTART:
3389 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3390 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3391
3392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3393
3394 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3395 default: assert(0 && "This action is not supported yet!");
3396 case TargetLowering::Legal: break;
3397 case TargetLowering::Custom:
3398 Tmp1 = TLI.LowerOperation(Result, DAG);
3399 if (Tmp1.Val) Result = Tmp1;
3400 break;
3401 }
3402 break;
3403
3404 case ISD::ROTL:
3405 case ISD::ROTR:
3406 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3407 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3408 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3409 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3410 default:
3411 assert(0 && "ROTL/ROTR legalize operation not supported");
3412 break;
3413 case TargetLowering::Legal:
3414 break;
3415 case TargetLowering::Custom:
3416 Tmp1 = TLI.LowerOperation(Result, DAG);
3417 if (Tmp1.Val) Result = Tmp1;
3418 break;
3419 case TargetLowering::Promote:
3420 assert(0 && "Do not know how to promote ROTL/ROTR");
3421 break;
3422 case TargetLowering::Expand:
3423 assert(0 && "Do not know how to expand ROTL/ROTR");
3424 break;
3425 }
3426 break;
3427
3428 case ISD::BSWAP:
3429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3430 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3431 case TargetLowering::Custom:
3432 assert(0 && "Cannot custom legalize this yet!");
3433 case TargetLowering::Legal:
3434 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3435 break;
3436 case TargetLowering::Promote: {
3437 MVT::ValueType OVT = Tmp1.getValueType();
3438 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3439 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3440
3441 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3442 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3443 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3444 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3445 break;
3446 }
3447 case TargetLowering::Expand:
3448 Result = ExpandBSWAP(Tmp1);
3449 break;
3450 }
3451 break;
3452
3453 case ISD::CTPOP:
3454 case ISD::CTTZ:
3455 case ISD::CTLZ:
3456 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3457 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003458 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 case TargetLowering::Legal:
3460 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003461 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003462 TargetLowering::Custom) {
3463 Tmp1 = TLI.LowerOperation(Result, DAG);
3464 if (Tmp1.Val) {
3465 Result = Tmp1;
3466 }
Scott Michel48b63e62007-07-30 21:00:31 +00003467 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 break;
3469 case TargetLowering::Promote: {
3470 MVT::ValueType OVT = Tmp1.getValueType();
3471 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3472
3473 // Zero extend the argument.
3474 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3475 // Perform the larger operation, then subtract if needed.
3476 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3477 switch (Node->getOpcode()) {
3478 case ISD::CTPOP:
3479 Result = Tmp1;
3480 break;
3481 case ISD::CTTZ:
3482 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3483 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3484 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3485 ISD::SETEQ);
3486 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003487 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003488 break;
3489 case ISD::CTLZ:
3490 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3491 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3492 DAG.getConstant(MVT::getSizeInBits(NVT) -
3493 MVT::getSizeInBits(OVT), NVT));
3494 break;
3495 }
3496 break;
3497 }
3498 case TargetLowering::Expand:
3499 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3500 break;
3501 }
3502 break;
3503
3504 // Unary operators
3505 case ISD::FABS:
3506 case ISD::FNEG:
3507 case ISD::FSQRT:
3508 case ISD::FSIN:
3509 case ISD::FCOS:
3510 Tmp1 = LegalizeOp(Node->getOperand(0));
3511 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3512 case TargetLowering::Promote:
3513 case TargetLowering::Custom:
3514 isCustom = true;
3515 // FALLTHROUGH
3516 case TargetLowering::Legal:
3517 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3518 if (isCustom) {
3519 Tmp1 = TLI.LowerOperation(Result, DAG);
3520 if (Tmp1.Val) Result = Tmp1;
3521 }
3522 break;
3523 case TargetLowering::Expand:
3524 switch (Node->getOpcode()) {
3525 default: assert(0 && "Unreachable!");
3526 case ISD::FNEG:
3527 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3528 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3529 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3530 break;
3531 case ISD::FABS: {
3532 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3533 MVT::ValueType VT = Node->getValueType(0);
3534 Tmp2 = DAG.getConstantFP(0.0, VT);
3535 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
3536 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3537 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3538 break;
3539 }
3540 case ISD::FSQRT:
3541 case ISD::FSIN:
3542 case ISD::FCOS: {
3543 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003544
3545 // Expand unsupported unary vector operators by unrolling them.
3546 if (MVT::isVector(VT)) {
3547 Result = LegalizeOp(UnrollVectorOp(Op));
3548 break;
3549 }
3550
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003551 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3552 switch(Node->getOpcode()) {
3553 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003554 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3555 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 break;
3557 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003558 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3559 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003560 break;
3561 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003562 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3563 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003564 break;
3565 default: assert(0 && "Unreachable!");
3566 }
3567 SDOperand Dummy;
3568 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3569 false/*sign irrelevant*/, Dummy);
3570 break;
3571 }
3572 }
3573 break;
3574 }
3575 break;
3576 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003577 MVT::ValueType VT = Node->getValueType(0);
3578
3579 // Expand unsupported unary vector operators by unrolling them.
3580 if (MVT::isVector(VT)) {
3581 Result = LegalizeOp(UnrollVectorOp(Op));
3582 break;
3583 }
3584
3585 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003586 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3587 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003588 SDOperand Dummy;
3589 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3590 false/*sign irrelevant*/, Dummy);
3591 break;
3592 }
3593 case ISD::BIT_CONVERT:
3594 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003595 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3596 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3598 // The input has to be a vector type, we have to either scalarize it, pack
3599 // it, or convert it based on whether the input vector type is legal.
3600 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003601 int InIx = Node->getOperand(0).ResNo;
3602 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3603 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003604
3605 // Figure out if there is a simple type corresponding to this Vector
3606 // type. If so, convert to the vector type.
3607 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3608 if (TLI.isTypeLegal(TVT)) {
3609 // Turn this into a bit convert of the vector input.
3610 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3611 LegalizeOp(Node->getOperand(0)));
3612 break;
3613 } else if (NumElems == 1) {
3614 // Turn this into a bit convert of the scalar input.
3615 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3616 ScalarizeVectorOp(Node->getOperand(0)));
3617 break;
3618 } else {
3619 // FIXME: UNIMP! Store then reload
3620 assert(0 && "Cast from unsupported vector type not implemented yet!");
3621 }
3622 } else {
3623 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3624 Node->getOperand(0).getValueType())) {
3625 default: assert(0 && "Unknown operation action!");
3626 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003627 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3628 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003629 break;
3630 case TargetLowering::Legal:
3631 Tmp1 = LegalizeOp(Node->getOperand(0));
3632 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3633 break;
3634 }
3635 }
3636 break;
3637
3638 // Conversion operators. The source and destination have different types.
3639 case ISD::SINT_TO_FP:
3640 case ISD::UINT_TO_FP: {
3641 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3642 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3643 case Legal:
3644 switch (TLI.getOperationAction(Node->getOpcode(),
3645 Node->getOperand(0).getValueType())) {
3646 default: assert(0 && "Unknown operation action!");
3647 case TargetLowering::Custom:
3648 isCustom = true;
3649 // FALLTHROUGH
3650 case TargetLowering::Legal:
3651 Tmp1 = LegalizeOp(Node->getOperand(0));
3652 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3653 if (isCustom) {
3654 Tmp1 = TLI.LowerOperation(Result, DAG);
3655 if (Tmp1.Val) Result = Tmp1;
3656 }
3657 break;
3658 case TargetLowering::Expand:
3659 Result = ExpandLegalINT_TO_FP(isSigned,
3660 LegalizeOp(Node->getOperand(0)),
3661 Node->getValueType(0));
3662 break;
3663 case TargetLowering::Promote:
3664 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3665 Node->getValueType(0),
3666 isSigned);
3667 break;
3668 }
3669 break;
3670 case Expand:
3671 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3672 Node->getValueType(0), Node->getOperand(0));
3673 break;
3674 case Promote:
3675 Tmp1 = PromoteOp(Node->getOperand(0));
3676 if (isSigned) {
3677 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3678 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3679 } else {
3680 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3681 Node->getOperand(0).getValueType());
3682 }
3683 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3684 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3685 break;
3686 }
3687 break;
3688 }
3689 case ISD::TRUNCATE:
3690 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3691 case Legal:
3692 Tmp1 = LegalizeOp(Node->getOperand(0));
3693 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3694 break;
3695 case Expand:
3696 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3697
3698 // Since the result is legal, we should just be able to truncate the low
3699 // part of the source.
3700 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3701 break;
3702 case Promote:
3703 Result = PromoteOp(Node->getOperand(0));
3704 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3705 break;
3706 }
3707 break;
3708
3709 case ISD::FP_TO_SINT:
3710 case ISD::FP_TO_UINT:
3711 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3712 case Legal:
3713 Tmp1 = LegalizeOp(Node->getOperand(0));
3714
3715 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3716 default: assert(0 && "Unknown operation action!");
3717 case TargetLowering::Custom:
3718 isCustom = true;
3719 // FALLTHROUGH
3720 case TargetLowering::Legal:
3721 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3722 if (isCustom) {
3723 Tmp1 = TLI.LowerOperation(Result, DAG);
3724 if (Tmp1.Val) Result = Tmp1;
3725 }
3726 break;
3727 case TargetLowering::Promote:
3728 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3729 Node->getOpcode() == ISD::FP_TO_SINT);
3730 break;
3731 case TargetLowering::Expand:
3732 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3733 SDOperand True, False;
3734 MVT::ValueType VT = Node->getOperand(0).getValueType();
3735 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen280620d2007-09-19 17:53:26 +00003736 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003737 const uint64_t zero[] = {0, 0};
3738 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3739 uint64_t x = 1ULL << ShiftAmt;
Neil Booth4bdd45a2007-10-07 11:45:55 +00003740 (void)apf.convertFromZeroExtendedInteger
3741 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003742 Tmp2 = DAG.getConstantFP(apf, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3744 Node->getOperand(0), Tmp2, ISD::SETLT);
3745 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3746 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3747 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3748 Tmp2));
3749 False = DAG.getNode(ISD::XOR, NVT, False,
3750 DAG.getConstant(1ULL << ShiftAmt, NVT));
3751 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3752 break;
3753 } else {
3754 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3755 }
3756 break;
3757 }
3758 break;
3759 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003760 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003761 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003762 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003763 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003764 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3765 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3766 Node->getOperand(0), DAG.getValueType(MVT::f64));
3767 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3768 DAG.getIntPtrConstant(1));
3769 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3770 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003771 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3772 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3773 Tmp2 = DAG.getConstantFP(apf, OVT);
3774 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3775 // FIXME: generated code sucks.
3776 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3777 DAG.getNode(ISD::ADD, MVT::i32,
3778 DAG.getNode(ISD::FP_TO_SINT, VT,
3779 DAG.getNode(ISD::FSUB, OVT,
3780 Node->getOperand(0), Tmp2)),
3781 DAG.getConstant(0x80000000, MVT::i32)),
3782 DAG.getNode(ISD::FP_TO_SINT, VT,
3783 Node->getOperand(0)),
3784 DAG.getCondCode(ISD::SETGE));
3785 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003786 break;
3787 }
Dale Johannesend3b6af32007-10-11 23:32:15 +00003788 // Convert f32 / f64 to i32 / i64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3790 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003791 case ISD::FP_TO_SINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003792 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003793 LC = (VT == MVT::i32)
3794 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003795 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003796 LC = (VT == MVT::i32)
3797 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003798 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003799 assert(VT == MVT::i64);
Dale Johannesenac77b272007-10-05 20:04:43 +00003800 LC = RTLIB::FPTOSINT_F80_I64;
3801 }
3802 else if (OVT == MVT::ppcf128) {
3803 assert(VT == MVT::i64);
3804 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003807 }
3808 case ISD::FP_TO_UINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003809 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 LC = (VT == MVT::i32)
3811 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003812 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813 LC = (VT == MVT::i32)
3814 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003815 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003816 LC = (VT == MVT::i32)
Dale Johannesenac77b272007-10-05 20:04:43 +00003817 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3818 }
3819 else if (OVT == MVT::ppcf128) {
3820 assert(VT == MVT::i64);
3821 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003822 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003823 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003824 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 default: assert(0 && "Unreachable!");
3826 }
3827 SDOperand Dummy;
3828 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3829 false/*sign irrelevant*/, Dummy);
3830 break;
3831 }
3832 case Promote:
3833 Tmp1 = PromoteOp(Node->getOperand(0));
3834 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3835 Result = LegalizeOp(Result);
3836 break;
3837 }
3838 break;
3839
Chris Lattner56ecde32008-01-16 06:57:07 +00003840 case ISD::FP_EXTEND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003841 MVT::ValueType DstVT = Op.getValueType();
3842 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3843 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3844 // The only other way we can lower this is to turn it into a STORE,
3845 // LOAD pair, targetting a temporary location (a stack slot).
3846 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3847 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003848 }
3849 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3850 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3851 case Legal:
3852 Tmp1 = LegalizeOp(Node->getOperand(0));
3853 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3854 break;
3855 case Promote:
3856 Tmp1 = PromoteOp(Node->getOperand(0));
3857 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3858 break;
3859 }
3860 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003861 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003862 case ISD::FP_ROUND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003863 MVT::ValueType DstVT = Op.getValueType();
3864 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3865 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3866 if (SrcVT == MVT::ppcf128) {
Dale Johannesena0d36082008-01-20 01:18:38 +00003867 SDOperand Lo;
3868 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003869 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003870 if (DstVT!=MVT::f64)
3871 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003872 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003873 }
Chris Lattner5872a362008-01-17 07:00:52 +00003874 // The only other way we can lower this is to turn it into a STORE,
3875 // LOAD pair, targetting a temporary location (a stack slot).
3876 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3877 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003878 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003879 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3880 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3881 case Legal:
3882 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003884 break;
3885 case Promote:
3886 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003887 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3888 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003889 break;
3890 }
3891 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003892 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 case ISD::ANY_EXTEND:
3894 case ISD::ZERO_EXTEND:
3895 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003896 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3897 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3898 case Legal:
3899 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac7091c2008-02-15 23:05:48 +00003900 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3901 TargetLowering::Custom) {
3902 Tmp2 = TLI.LowerOperation(Result, DAG);
3903 if (Tmp2.Val) {
3904 Tmp1 = Tmp2;
3905 }
3906 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3908 break;
3909 case Promote:
3910 switch (Node->getOpcode()) {
3911 case ISD::ANY_EXTEND:
3912 Tmp1 = PromoteOp(Node->getOperand(0));
3913 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3914 break;
3915 case ISD::ZERO_EXTEND:
3916 Result = PromoteOp(Node->getOperand(0));
3917 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3918 Result = DAG.getZeroExtendInReg(Result,
3919 Node->getOperand(0).getValueType());
3920 break;
3921 case ISD::SIGN_EXTEND:
3922 Result = PromoteOp(Node->getOperand(0));
3923 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3924 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3925 Result,
3926 DAG.getValueType(Node->getOperand(0).getValueType()));
3927 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003928 }
3929 }
3930 break;
3931 case ISD::FP_ROUND_INREG:
3932 case ISD::SIGN_EXTEND_INREG: {
3933 Tmp1 = LegalizeOp(Node->getOperand(0));
3934 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3935
3936 // If this operation is not supported, convert it to a shl/shr or load/store
3937 // pair.
3938 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3939 default: assert(0 && "This action not supported for this op yet!");
3940 case TargetLowering::Legal:
3941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3942 break;
3943 case TargetLowering::Expand:
3944 // If this is an integer extend and shifts are supported, do that.
3945 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3946 // NOTE: we could fall back on load/store here too for targets without
3947 // SAR. However, it is doubtful that any exist.
3948 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3949 MVT::getSizeInBits(ExtraVT);
3950 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3951 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3952 Node->getOperand(0), ShiftCst);
3953 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3954 Result, ShiftCst);
3955 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3956 // The only way we can lower this is to turn it into a TRUNCSTORE,
3957 // EXTLOAD pair, targetting a temporary location (a stack slot).
3958
3959 // NOTE: there is a choice here between constantly creating new stack
3960 // slots and always reusing the same one. We currently always create
3961 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003962 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3963 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 } else {
3965 assert(0 && "Unknown op");
3966 }
3967 break;
3968 }
3969 break;
3970 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003971 case ISD::TRAMPOLINE: {
3972 SDOperand Ops[6];
3973 for (unsigned i = 0; i != 6; ++i)
3974 Ops[i] = LegalizeOp(Node->getOperand(i));
3975 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3976 // The only option for this node is to custom lower it.
3977 Result = TLI.LowerOperation(Result, DAG);
3978 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003979
3980 // Since trampoline produces two values, make sure to remember that we
3981 // legalized both of them.
3982 Tmp1 = LegalizeOp(Result.getValue(1));
3983 Result = LegalizeOp(Result);
3984 AddLegalizedOperand(SDOperand(Node, 0), Result);
3985 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3986 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003987 }
Dan Gohman819574c2008-01-31 00:41:03 +00003988 case ISD::FLT_ROUNDS_: {
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003989 MVT::ValueType VT = Node->getValueType(0);
3990 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3991 default: assert(0 && "This action not supported for this op yet!");
3992 case TargetLowering::Custom:
3993 Result = TLI.LowerOperation(Op, DAG);
3994 if (Result.Val) break;
3995 // Fall Thru
3996 case TargetLowering::Legal:
3997 // If this operation is not supported, lower it to constant 1
3998 Result = DAG.getConstant(1, VT);
3999 break;
4000 }
4001 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004002 case ISD::TRAP: {
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004003 MVT::ValueType VT = Node->getValueType(0);
4004 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4005 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004006 case TargetLowering::Legal:
4007 Tmp1 = LegalizeOp(Node->getOperand(0));
4008 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4009 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004010 case TargetLowering::Custom:
4011 Result = TLI.LowerOperation(Op, DAG);
4012 if (Result.Val) break;
4013 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004014 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004015 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004016 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004017 TargetLowering::ArgListTy Args;
4018 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004019 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4020 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00004021 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4022 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004023 Result = CallResult.second;
4024 break;
4025 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004026 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004027 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 }
4029
4030 assert(Result.getValueType() == Op.getValueType() &&
4031 "Bad legalization!");
4032
4033 // Make sure that the generated code is itself legal.
4034 if (Result != Op)
4035 Result = LegalizeOp(Result);
4036
4037 // Note that LegalizeOp may be reentered even from single-use nodes, which
4038 // means that we always must cache transformed nodes.
4039 AddLegalizedOperand(Op, Result);
4040 return Result;
4041}
4042
4043/// PromoteOp - Given an operation that produces a value in an invalid type,
4044/// promote it to compute the value into a larger type. The produced value will
4045/// have the correct bits for the low portion of the register, but no guarantee
4046/// is made about the top bits: it may be zero, sign-extended, or garbage.
4047SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4048 MVT::ValueType VT = Op.getValueType();
4049 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4050 assert(getTypeAction(VT) == Promote &&
4051 "Caller should expand or legalize operands that are not promotable!");
4052 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4053 "Cannot promote to smaller type!");
4054
4055 SDOperand Tmp1, Tmp2, Tmp3;
4056 SDOperand Result;
4057 SDNode *Node = Op.Val;
4058
4059 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
4060 if (I != PromotedNodes.end()) return I->second;
4061
4062 switch (Node->getOpcode()) {
4063 case ISD::CopyFromReg:
4064 assert(0 && "CopyFromReg must be legal!");
4065 default:
4066#ifndef NDEBUG
4067 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4068#endif
4069 assert(0 && "Do not know how to promote this operator!");
4070 abort();
4071 case ISD::UNDEF:
4072 Result = DAG.getNode(ISD::UNDEF, NVT);
4073 break;
4074 case ISD::Constant:
4075 if (VT != MVT::i1)
4076 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4077 else
4078 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4079 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4080 break;
4081 case ISD::ConstantFP:
4082 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4083 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4084 break;
4085
4086 case ISD::SETCC:
4087 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
4088 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4089 Node->getOperand(1), Node->getOperand(2));
4090 break;
4091
4092 case ISD::TRUNCATE:
4093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4094 case Legal:
4095 Result = LegalizeOp(Node->getOperand(0));
4096 assert(Result.getValueType() >= NVT &&
4097 "This truncation doesn't make sense!");
4098 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4099 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4100 break;
4101 case Promote:
4102 // The truncation is not required, because we don't guarantee anything
4103 // about high bits anyway.
4104 Result = PromoteOp(Node->getOperand(0));
4105 break;
4106 case Expand:
4107 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4108 // Truncate the low part of the expanded value to the result type
4109 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4110 }
4111 break;
4112 case ISD::SIGN_EXTEND:
4113 case ISD::ZERO_EXTEND:
4114 case ISD::ANY_EXTEND:
4115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4116 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4117 case Legal:
4118 // Input is legal? Just do extend all the way to the larger type.
4119 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4120 break;
4121 case Promote:
4122 // Promote the reg if it's smaller.
4123 Result = PromoteOp(Node->getOperand(0));
4124 // The high bits are not guaranteed to be anything. Insert an extend.
4125 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4126 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4127 DAG.getValueType(Node->getOperand(0).getValueType()));
4128 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4129 Result = DAG.getZeroExtendInReg(Result,
4130 Node->getOperand(0).getValueType());
4131 break;
4132 }
4133 break;
4134 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004135 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4136 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137 Result = PromoteOp(Result);
4138 break;
4139
4140 case ISD::FP_EXTEND:
4141 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4142 case ISD::FP_ROUND:
4143 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4144 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4145 case Promote: assert(0 && "Unreachable with 2 FP types!");
4146 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004147 if (Node->getConstantOperandVal(1) == 0) {
4148 // Input is legal? Do an FP_ROUND_INREG.
4149 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4150 DAG.getValueType(VT));
4151 } else {
4152 // Just remove the truncate, it isn't affecting the value.
4153 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4154 Node->getOperand(1));
4155 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004156 break;
4157 }
4158 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 case ISD::SINT_TO_FP:
4160 case ISD::UINT_TO_FP:
4161 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4162 case Legal:
4163 // No extra round required here.
4164 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4165 break;
4166
4167 case Promote:
4168 Result = PromoteOp(Node->getOperand(0));
4169 if (Node->getOpcode() == ISD::SINT_TO_FP)
4170 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4171 Result,
4172 DAG.getValueType(Node->getOperand(0).getValueType()));
4173 else
4174 Result = DAG.getZeroExtendInReg(Result,
4175 Node->getOperand(0).getValueType());
4176 // No extra round required here.
4177 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4178 break;
4179 case Expand:
4180 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4181 Node->getOperand(0));
4182 // Round if we cannot tolerate excess precision.
4183 if (NoExcessFPPrecision)
4184 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4185 DAG.getValueType(VT));
4186 break;
4187 }
4188 break;
4189
4190 case ISD::SIGN_EXTEND_INREG:
4191 Result = PromoteOp(Node->getOperand(0));
4192 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4193 Node->getOperand(1));
4194 break;
4195 case ISD::FP_TO_SINT:
4196 case ISD::FP_TO_UINT:
4197 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4198 case Legal:
4199 case Expand:
4200 Tmp1 = Node->getOperand(0);
4201 break;
4202 case Promote:
4203 // The input result is prerounded, so we don't have to do anything
4204 // special.
4205 Tmp1 = PromoteOp(Node->getOperand(0));
4206 break;
4207 }
4208 // If we're promoting a UINT to a larger size, check to see if the new node
4209 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4210 // we can use that instead. This allows us to generate better code for
4211 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4212 // legal, such as PowerPC.
4213 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4214 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4215 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4216 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4217 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4218 } else {
4219 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4220 }
4221 break;
4222
4223 case ISD::FABS:
4224 case ISD::FNEG:
4225 Tmp1 = PromoteOp(Node->getOperand(0));
4226 assert(Tmp1.getValueType() == NVT);
4227 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4228 // NOTE: we do not have to do any extra rounding here for
4229 // NoExcessFPPrecision, because we know the input will have the appropriate
4230 // precision, and these operations don't modify precision at all.
4231 break;
4232
4233 case ISD::FSQRT:
4234 case ISD::FSIN:
4235 case ISD::FCOS:
4236 Tmp1 = PromoteOp(Node->getOperand(0));
4237 assert(Tmp1.getValueType() == NVT);
4238 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4239 if (NoExcessFPPrecision)
4240 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4241 DAG.getValueType(VT));
4242 break;
4243
4244 case ISD::FPOWI: {
4245 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4246 // directly as well, which may be better.
4247 Tmp1 = PromoteOp(Node->getOperand(0));
4248 assert(Tmp1.getValueType() == NVT);
4249 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4250 if (NoExcessFPPrecision)
4251 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4252 DAG.getValueType(VT));
4253 break;
4254 }
4255
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004256 case ISD::ATOMIC_LCS: {
4257 Tmp2 = PromoteOp(Node->getOperand(2));
4258 Tmp3 = PromoteOp(Node->getOperand(3));
4259 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4260 Node->getOperand(1), Tmp2, Tmp3,
4261 cast<AtomicSDNode>(Node)->getVT());
4262 // Remember that we legalized the chain.
4263 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4264 break;
4265 }
4266 case ISD::ATOMIC_LAS:
4267 case ISD::ATOMIC_SWAP: {
4268 Tmp2 = PromoteOp(Node->getOperand(2));
4269 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4270 Node->getOperand(1), Tmp2,
4271 cast<AtomicSDNode>(Node)->getVT());
4272 // Remember that we legalized the chain.
4273 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4274 break;
4275 }
4276
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277 case ISD::AND:
4278 case ISD::OR:
4279 case ISD::XOR:
4280 case ISD::ADD:
4281 case ISD::SUB:
4282 case ISD::MUL:
4283 // The input may have strange things in the top bits of the registers, but
4284 // these operations don't care. They may have weird bits going out, but
4285 // that too is okay if they are integer operations.
4286 Tmp1 = PromoteOp(Node->getOperand(0));
4287 Tmp2 = PromoteOp(Node->getOperand(1));
4288 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4289 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4290 break;
4291 case ISD::FADD:
4292 case ISD::FSUB:
4293 case ISD::FMUL:
4294 Tmp1 = PromoteOp(Node->getOperand(0));
4295 Tmp2 = PromoteOp(Node->getOperand(1));
4296 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4297 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4298
4299 // Floating point operations will give excess precision that we may not be
4300 // able to tolerate. If we DO allow excess precision, just leave it,
4301 // otherwise excise it.
4302 // FIXME: Why would we need to round FP ops more than integer ones?
4303 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4304 if (NoExcessFPPrecision)
4305 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4306 DAG.getValueType(VT));
4307 break;
4308
4309 case ISD::SDIV:
4310 case ISD::SREM:
4311 // These operators require that their input be sign extended.
4312 Tmp1 = PromoteOp(Node->getOperand(0));
4313 Tmp2 = PromoteOp(Node->getOperand(1));
4314 if (MVT::isInteger(NVT)) {
4315 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4316 DAG.getValueType(VT));
4317 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4318 DAG.getValueType(VT));
4319 }
4320 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4321
4322 // Perform FP_ROUND: this is probably overly pessimistic.
4323 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
4324 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4325 DAG.getValueType(VT));
4326 break;
4327 case ISD::FDIV:
4328 case ISD::FREM:
4329 case ISD::FCOPYSIGN:
4330 // These operators require that their input be fp extended.
4331 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004332 case Expand: assert(0 && "not implemented");
4333 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4334 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335 }
4336 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004337 case Expand: assert(0 && "not implemented");
4338 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4339 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340 }
4341 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4342
4343 // Perform FP_ROUND: this is probably overly pessimistic.
4344 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4345 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4346 DAG.getValueType(VT));
4347 break;
4348
4349 case ISD::UDIV:
4350 case ISD::UREM:
4351 // These operators require that their input be zero extended.
4352 Tmp1 = PromoteOp(Node->getOperand(0));
4353 Tmp2 = PromoteOp(Node->getOperand(1));
4354 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
4355 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4356 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4357 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4358 break;
4359
4360 case ISD::SHL:
4361 Tmp1 = PromoteOp(Node->getOperand(0));
4362 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4363 break;
4364 case ISD::SRA:
4365 // The input value must be properly sign extended.
4366 Tmp1 = PromoteOp(Node->getOperand(0));
4367 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4368 DAG.getValueType(VT));
4369 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4370 break;
4371 case ISD::SRL:
4372 // The input value must be properly zero extended.
4373 Tmp1 = PromoteOp(Node->getOperand(0));
4374 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4375 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4376 break;
4377
4378 case ISD::VAARG:
4379 Tmp1 = Node->getOperand(0); // Get the chain.
4380 Tmp2 = Node->getOperand(1); // Get the pointer.
4381 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4382 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4383 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4384 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004385 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4386 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387 // Increment the pointer, VAList, to the next vaarg
4388 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4389 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4390 TLI.getPointerTy()));
4391 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004392 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393 // Load the actual argument out of the pointer VAList
4394 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4395 }
4396 // Remember that we legalized the chain.
4397 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4398 break;
4399
4400 case ISD::LOAD: {
4401 LoadSDNode *LD = cast<LoadSDNode>(Node);
4402 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4403 ? ISD::EXTLOAD : LD->getExtensionType();
4404 Result = DAG.getExtLoad(ExtType, NVT,
4405 LD->getChain(), LD->getBasePtr(),
4406 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004407 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 LD->isVolatile(),
4409 LD->getAlignment());
4410 // Remember that we legalized the chain.
4411 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4412 break;
4413 }
4414 case ISD::SELECT:
4415 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4416 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4417 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4418 break;
4419 case ISD::SELECT_CC:
4420 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4421 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4422 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4423 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4424 break;
4425 case ISD::BSWAP:
4426 Tmp1 = Node->getOperand(0);
4427 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4428 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4429 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4430 DAG.getConstant(MVT::getSizeInBits(NVT) -
4431 MVT::getSizeInBits(VT),
4432 TLI.getShiftAmountTy()));
4433 break;
4434 case ISD::CTPOP:
4435 case ISD::CTTZ:
4436 case ISD::CTLZ:
4437 // Zero extend the argument
4438 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4439 // Perform the larger operation, then subtract if needed.
4440 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4441 switch(Node->getOpcode()) {
4442 case ISD::CTPOP:
4443 Result = Tmp1;
4444 break;
4445 case ISD::CTTZ:
4446 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4447 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
4448 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4449 ISD::SETEQ);
4450 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4451 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4452 break;
4453 case ISD::CTLZ:
4454 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4455 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4456 DAG.getConstant(MVT::getSizeInBits(NVT) -
4457 MVT::getSizeInBits(VT), NVT));
4458 break;
4459 }
4460 break;
4461 case ISD::EXTRACT_SUBVECTOR:
4462 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4463 break;
4464 case ISD::EXTRACT_VECTOR_ELT:
4465 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4466 break;
4467 }
4468
4469 assert(Result.Val && "Didn't set a result!");
4470
4471 // Make sure the result is itself legal.
4472 Result = LegalizeOp(Result);
4473
4474 // Remember that we promoted this!
4475 AddPromotedOperand(Op, Result);
4476 return Result;
4477}
4478
4479/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4480/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4481/// based on the vector type. The return type of this matches the element type
4482/// of the vector, which may not be legal for the target.
4483SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4484 // We know that operand #0 is the Vec vector. If the index is a constant
4485 // or if the invec is a supported hardware type, we can use it. Otherwise,
4486 // lower to a store then an indexed load.
4487 SDOperand Vec = Op.getOperand(0);
4488 SDOperand Idx = Op.getOperand(1);
4489
Dan Gohmana0763d92007-09-24 15:54:53 +00004490 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 unsigned NumElems = MVT::getVectorNumElements(TVT);
4492
4493 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4494 default: assert(0 && "This action is not supported yet!");
4495 case TargetLowering::Custom: {
4496 Vec = LegalizeOp(Vec);
4497 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4498 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4499 if (Tmp3.Val)
4500 return Tmp3;
4501 break;
4502 }
4503 case TargetLowering::Legal:
4504 if (isTypeLegal(TVT)) {
4505 Vec = LegalizeOp(Vec);
4506 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004507 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 }
4509 break;
4510 case TargetLowering::Expand:
4511 break;
4512 }
4513
4514 if (NumElems == 1) {
4515 // This must be an access of the only element. Return it.
4516 Op = ScalarizeVectorOp(Vec);
4517 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004518 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4520 SDOperand Lo, Hi;
4521 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004522 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523 Vec = Lo;
4524 } else {
4525 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004526 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527 Idx.getValueType());
4528 }
4529
4530 // It's now an extract from the appropriate high or low part. Recurse.
4531 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4532 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4533 } else {
4534 // Store the value to a temporary stack slot, then LOAD the scalar
4535 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004536 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4538
4539 // Add the offset to the index.
4540 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4541 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4542 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004543
4544 if (MVT::getSizeInBits(Idx.getValueType()) >
4545 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004546 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004547 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004548 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004549
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4551
4552 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4553 }
4554 return Op;
4555}
4556
4557/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4558/// we assume the operation can be split if it is not already legal.
4559SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4560 // We know that operand #0 is the Vec vector. For now we assume the index
4561 // is a constant and that the extracted result is a supported hardware type.
4562 SDOperand Vec = Op.getOperand(0);
4563 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4564
4565 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4566
4567 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4568 // This must be an access of the desired vector length. Return it.
4569 return Vec;
4570 }
4571
4572 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4573 SDOperand Lo, Hi;
4574 SplitVectorOp(Vec, Lo, Hi);
4575 if (CIdx->getValue() < NumElems/2) {
4576 Vec = Lo;
4577 } else {
4578 Vec = Hi;
4579 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4580 }
4581
4582 // It's now an extract from the appropriate high or low part. Recurse.
4583 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4584 return ExpandEXTRACT_SUBVECTOR(Op);
4585}
4586
4587/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4588/// with condition CC on the current target. This usually involves legalizing
4589/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4590/// there may be no choice but to create a new SetCC node to represent the
4591/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4592/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4593void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4594 SDOperand &RHS,
4595 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004596 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004597
4598 switch (getTypeAction(LHS.getValueType())) {
4599 case Legal:
4600 Tmp1 = LegalizeOp(LHS); // LHS
4601 Tmp2 = LegalizeOp(RHS); // RHS
4602 break;
4603 case Promote:
4604 Tmp1 = PromoteOp(LHS); // LHS
4605 Tmp2 = PromoteOp(RHS); // RHS
4606
4607 // If this is an FP compare, the operands have already been extended.
4608 if (MVT::isInteger(LHS.getValueType())) {
4609 MVT::ValueType VT = LHS.getValueType();
4610 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4611
4612 // Otherwise, we have to insert explicit sign or zero extends. Note
4613 // that we could insert sign extends for ALL conditions, but zero extend
4614 // is cheaper on many machines (an AND instead of two shifts), so prefer
4615 // it.
4616 switch (cast<CondCodeSDNode>(CC)->get()) {
4617 default: assert(0 && "Unknown integer comparison!");
4618 case ISD::SETEQ:
4619 case ISD::SETNE:
4620 case ISD::SETUGE:
4621 case ISD::SETUGT:
4622 case ISD::SETULE:
4623 case ISD::SETULT:
4624 // ALL of these operations will work if we either sign or zero extend
4625 // the operands (including the unsigned comparisons!). Zero extend is
4626 // usually a simpler/cheaper operation, so prefer it.
4627 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4628 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4629 break;
4630 case ISD::SETGE:
4631 case ISD::SETGT:
4632 case ISD::SETLT:
4633 case ISD::SETLE:
4634 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4635 DAG.getValueType(VT));
4636 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4637 DAG.getValueType(VT));
4638 break;
4639 }
4640 }
4641 break;
4642 case Expand: {
4643 MVT::ValueType VT = LHS.getValueType();
4644 if (VT == MVT::f32 || VT == MVT::f64) {
4645 // Expand into one or more soft-fp libcall(s).
4646 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4647 switch (cast<CondCodeSDNode>(CC)->get()) {
4648 case ISD::SETEQ:
4649 case ISD::SETOEQ:
4650 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4651 break;
4652 case ISD::SETNE:
4653 case ISD::SETUNE:
4654 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4655 break;
4656 case ISD::SETGE:
4657 case ISD::SETOGE:
4658 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4659 break;
4660 case ISD::SETLT:
4661 case ISD::SETOLT:
4662 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4663 break;
4664 case ISD::SETLE:
4665 case ISD::SETOLE:
4666 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4667 break;
4668 case ISD::SETGT:
4669 case ISD::SETOGT:
4670 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4671 break;
4672 case ISD::SETUO:
4673 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4674 break;
4675 case ISD::SETO:
4676 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4677 break;
4678 default:
4679 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4680 switch (cast<CondCodeSDNode>(CC)->get()) {
4681 case ISD::SETONE:
4682 // SETONE = SETOLT | SETOGT
4683 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4684 // Fallthrough
4685 case ISD::SETUGT:
4686 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4687 break;
4688 case ISD::SETUGE:
4689 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4690 break;
4691 case ISD::SETULT:
4692 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4693 break;
4694 case ISD::SETULE:
4695 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4696 break;
4697 case ISD::SETUEQ:
4698 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4699 break;
4700 default: assert(0 && "Unsupported FP setcc!");
4701 }
4702 }
4703
4704 SDOperand Dummy;
4705 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
4706 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4707 false /*sign irrelevant*/, Dummy);
4708 Tmp2 = DAG.getConstant(0, MVT::i32);
4709 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4710 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
4711 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
4712 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
4713 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4714 false /*sign irrelevant*/, Dummy);
4715 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4716 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4717 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4718 Tmp2 = SDOperand();
4719 }
4720 LHS = Tmp1;
4721 RHS = Tmp2;
4722 return;
4723 }
4724
4725 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4726 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004727 ExpandOp(RHS, RHSLo, RHSHi);
4728 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4729
4730 if (VT==MVT::ppcf128) {
4731 // FIXME: This generated code sucks. We want to generate
4732 // FCMP crN, hi1, hi2
4733 // BNE crN, L:
4734 // FCMP crN, lo1, lo2
4735 // The following can be improved, but not that much.
4736 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4737 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4738 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4739 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4740 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4741 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4742 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4743 Tmp2 = SDOperand();
4744 break;
4745 }
4746
4747 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 case ISD::SETEQ:
4749 case ISD::SETNE:
4750 if (RHSLo == RHSHi)
4751 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4752 if (RHSCST->isAllOnesValue()) {
4753 // Comparison to -1.
4754 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4755 Tmp2 = RHSLo;
4756 break;
4757 }
4758
4759 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4760 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4761 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4762 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4763 break;
4764 default:
4765 // If this is a comparison of the sign bit, just look at the top part.
4766 // X > -1, x < 0
4767 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4768 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4769 CST->getValue() == 0) || // X < 0
4770 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4771 CST->isAllOnesValue())) { // X > -1
4772 Tmp1 = LHSHi;
4773 Tmp2 = RHSHi;
4774 break;
4775 }
4776
4777 // FIXME: This generated code sucks.
4778 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779 switch (CCCode) {
4780 default: assert(0 && "Unknown integer setcc!");
4781 case ISD::SETLT:
4782 case ISD::SETULT: LowCC = ISD::SETULT; break;
4783 case ISD::SETGT:
4784 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4785 case ISD::SETLE:
4786 case ISD::SETULE: LowCC = ISD::SETULE; break;
4787 case ISD::SETGE:
4788 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4789 }
4790
4791 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4792 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4793 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4794
4795 // NOTE: on targets without efficient SELECT of bools, we can always use
4796 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4797 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4798 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4799 false, DagCombineInfo);
4800 if (!Tmp1.Val)
4801 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4802 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4803 CCCode, false, DagCombineInfo);
4804 if (!Tmp2.Val)
Chris Lattner6fb53da2007-10-15 17:48:57 +00004805 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806
4807 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4808 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4809 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4810 (Tmp2C && Tmp2C->getValue() == 0 &&
4811 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4812 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4813 (Tmp2C && Tmp2C->getValue() == 1 &&
4814 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4815 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4816 // low part is known false, returns high part.
4817 // For LE / GE, if high part is known false, ignore the low part.
4818 // For LT / GT, if high part is known true, ignore the low part.
4819 Tmp1 = Tmp2;
4820 Tmp2 = SDOperand();
4821 } else {
4822 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4823 ISD::SETEQ, false, DagCombineInfo);
4824 if (!Result.Val)
4825 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4826 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4827 Result, Tmp1, Tmp2));
4828 Tmp1 = Result;
4829 Tmp2 = SDOperand();
4830 }
4831 }
4832 }
4833 }
4834 LHS = Tmp1;
4835 RHS = Tmp2;
4836}
4837
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004838/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4839/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4840/// a load from the stack slot to DestVT, extending it if needed.
4841/// The resultant code need not be legal.
4842SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4843 MVT::ValueType SlotVT,
4844 MVT::ValueType DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845 // Create the stack frame object.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004846 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4847
Dan Gohman20e37962008-02-11 18:58:42 +00004848 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004849 int SPFI = StackPtrFI->getIndex();
4850
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004851 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4852 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4853 unsigned DestSize = MVT::getSizeInBits(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004855 // Emit a store to the stack slot. Use a truncstore if the input value is
4856 // later than DestVT.
4857 SDOperand Store;
4858 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004859 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004860 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004861 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004862 else {
4863 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004864 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004865 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004866 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004867 }
4868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004869 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004870 if (SlotSize == DestSize)
4871 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4872
4873 assert(SlotSize < DestSize && "Unknown extension!");
4874 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875}
4876
4877SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4878 // Create a vector sized/aligned stack slot, store the value to element #0,
4879 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004880 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00004881
Dan Gohman20e37962008-02-11 18:58:42 +00004882 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004883 int SPFI = StackPtrFI->getIndex();
4884
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004885 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004886 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman12a9c082008-02-06 22:27:42 +00004887 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004888 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889}
4890
4891
4892/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4893/// support the operation, but do support the resultant vector type.
4894SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4895
4896 // If the only non-undef value is the low element, turn this into a
4897 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4898 unsigned NumElems = Node->getNumOperands();
4899 bool isOnlyLowElement = true;
4900 SDOperand SplatValue = Node->getOperand(0);
4901 std::map<SDOperand, std::vector<unsigned> > Values;
4902 Values[SplatValue].push_back(0);
4903 bool isConstant = true;
4904 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4905 SplatValue.getOpcode() != ISD::UNDEF)
4906 isConstant = false;
4907
4908 for (unsigned i = 1; i < NumElems; ++i) {
4909 SDOperand V = Node->getOperand(i);
4910 Values[V].push_back(i);
4911 if (V.getOpcode() != ISD::UNDEF)
4912 isOnlyLowElement = false;
4913 if (SplatValue != V)
4914 SplatValue = SDOperand(0,0);
4915
4916 // If this isn't a constant element or an undef, we can't use a constant
4917 // pool load.
4918 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4919 V.getOpcode() != ISD::UNDEF)
4920 isConstant = false;
4921 }
4922
4923 if (isOnlyLowElement) {
4924 // If the low element is an undef too, then this whole things is an undef.
4925 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4926 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4927 // Otherwise, turn this into a scalar_to_vector node.
4928 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4929 Node->getOperand(0));
4930 }
4931
4932 // If all elements are constants, create a load from the constant pool.
4933 if (isConstant) {
4934 MVT::ValueType VT = Node->getValueType(0);
4935 const Type *OpNTy =
4936 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4937 std::vector<Constant*> CV;
4938 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4939 if (ConstantFPSDNode *V =
4940 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00004941 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004942 } else if (ConstantSDNode *V =
4943 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4944 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4945 } else {
4946 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4947 CV.push_back(UndefValue::get(OpNTy));
4948 }
4949 }
4950 Constant *CP = ConstantVector::get(CV);
4951 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004952 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004953 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 }
4955
4956 if (SplatValue.Val) { // Splat of one value?
4957 // Build the shuffle constant vector: <0, 0, 0, 0>
4958 MVT::ValueType MaskVT =
4959 MVT::getIntVectorWithNumElements(NumElems);
4960 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4961 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4962 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4963 &ZeroVec[0], ZeroVec.size());
4964
4965 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4966 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4967 // Get the splatted value into the low element of a vector register.
4968 SDOperand LowValVec =
4969 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4970
4971 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4972 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4973 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4974 SplatMask);
4975 }
4976 }
4977
4978 // If there are only two unique elements, we may be able to turn this into a
4979 // vector shuffle.
4980 if (Values.size() == 2) {
4981 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4982 MVT::ValueType MaskVT =
4983 MVT::getIntVectorWithNumElements(NumElems);
4984 std::vector<SDOperand> MaskVec(NumElems);
4985 unsigned i = 0;
4986 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4987 E = Values.end(); I != E; ++I) {
4988 for (std::vector<unsigned>::iterator II = I->second.begin(),
4989 EE = I->second.end(); II != EE; ++II)
4990 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
4991 i += NumElems;
4992 }
4993 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4994 &MaskVec[0], MaskVec.size());
4995
4996 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4997 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4998 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
4999 SmallVector<SDOperand, 8> Ops;
5000 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5001 E = Values.end(); I != E; ++I) {
5002 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5003 I->first);
5004 Ops.push_back(Op);
5005 }
5006 Ops.push_back(ShuffleMask);
5007
5008 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
5009 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5010 &Ops[0], Ops.size());
5011 }
5012 }
5013
5014 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5015 // aligned object on the stack, store each element into it, then load
5016 // the result as a vector.
5017 MVT::ValueType VT = Node->getValueType(0);
5018 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00005019 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005020
5021 // Emit a store of each element to the stack slot.
5022 SmallVector<SDOperand, 8> Stores;
5023 unsigned TypeByteSize =
5024 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
5025 // Store (in the right endianness) the elements to memory.
5026 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5027 // Ignore undef elements.
5028 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5029
5030 unsigned Offset = TypeByteSize*i;
5031
5032 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5033 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5034
5035 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5036 NULL, 0));
5037 }
5038
5039 SDOperand StoreChain;
5040 if (!Stores.empty()) // Not all undef elements?
5041 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5042 &Stores[0], Stores.size());
5043 else
5044 StoreChain = DAG.getEntryNode();
5045
5046 // Result is a load from the stack slot.
5047 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5048}
5049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005050void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5051 SDOperand Op, SDOperand Amt,
5052 SDOperand &Lo, SDOperand &Hi) {
5053 // Expand the subcomponents.
5054 SDOperand LHSL, LHSH;
5055 ExpandOp(Op, LHSL, LHSH);
5056
5057 SDOperand Ops[] = { LHSL, LHSH, Amt };
5058 MVT::ValueType VT = LHSL.getValueType();
5059 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5060 Hi = Lo.getValue(1);
5061}
5062
5063
5064/// ExpandShift - Try to find a clever way to expand this shift operation out to
5065/// smaller elements. If we can't find a way that is more efficient than a
5066/// libcall on this target, return false. Otherwise, return true with the
5067/// low-parts expanded into Lo and Hi.
5068bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5069 SDOperand &Lo, SDOperand &Hi) {
5070 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5071 "This is not a shift!");
5072
5073 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
5074 SDOperand ShAmt = LegalizeOp(Amt);
5075 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohmanece0a882008-02-20 16:57:27 +00005076 unsigned ShBits = MVT::getSizeInBits(ShTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005077 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5078 unsigned NVTBits = MVT::getSizeInBits(NVT);
5079
Chris Lattner8c931452007-10-14 20:35:12 +00005080 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005081 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5082 unsigned Cst = CN->getValue();
5083 // Expand the incoming operand to be shifted, so that we have its parts
5084 SDOperand InL, InH;
5085 ExpandOp(Op, InL, InH);
5086 switch(Opc) {
5087 case ISD::SHL:
5088 if (Cst > VTBits) {
5089 Lo = DAG.getConstant(0, NVT);
5090 Hi = DAG.getConstant(0, NVT);
5091 } else if (Cst > NVTBits) {
5092 Lo = DAG.getConstant(0, NVT);
5093 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5094 } else if (Cst == NVTBits) {
5095 Lo = DAG.getConstant(0, NVT);
5096 Hi = InL;
5097 } else {
5098 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5099 Hi = DAG.getNode(ISD::OR, NVT,
5100 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5101 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5102 }
5103 return true;
5104 case ISD::SRL:
5105 if (Cst > VTBits) {
5106 Lo = DAG.getConstant(0, NVT);
5107 Hi = DAG.getConstant(0, NVT);
5108 } else if (Cst > NVTBits) {
5109 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5110 Hi = DAG.getConstant(0, NVT);
5111 } else if (Cst == NVTBits) {
5112 Lo = InH;
5113 Hi = DAG.getConstant(0, NVT);
5114 } else {
5115 Lo = DAG.getNode(ISD::OR, NVT,
5116 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5117 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5118 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5119 }
5120 return true;
5121 case ISD::SRA:
5122 if (Cst > VTBits) {
5123 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5124 DAG.getConstant(NVTBits-1, ShTy));
5125 } else if (Cst > NVTBits) {
5126 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5127 DAG.getConstant(Cst-NVTBits, ShTy));
5128 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5129 DAG.getConstant(NVTBits-1, ShTy));
5130 } else if (Cst == NVTBits) {
5131 Lo = InH;
5132 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5133 DAG.getConstant(NVTBits-1, ShTy));
5134 } else {
5135 Lo = DAG.getNode(ISD::OR, NVT,
5136 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5137 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5138 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5139 }
5140 return true;
5141 }
5142 }
5143
5144 // Okay, the shift amount isn't constant. However, if we can tell that it is
5145 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005146 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5147 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5149
5150 // If we know that the high bit of the shift amount is one, then we can do
5151 // this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005152 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005153 // Mask out the high bit, which we know is set.
5154 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005155 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005156
5157 // Expand the incoming operand to be shifted, so that we have its parts
5158 SDOperand InL, InH;
5159 ExpandOp(Op, InL, InH);
5160 switch(Opc) {
5161 case ISD::SHL:
5162 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5163 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5164 return true;
5165 case ISD::SRL:
5166 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5167 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5168 return true;
5169 case ISD::SRA:
5170 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5171 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5172 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5173 return true;
5174 }
5175 }
5176
5177 // If we know that the high bit of the shift amount is zero, then we can do
5178 // this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005179 if (KnownZero.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005180 // Compute 32-amt.
5181 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5182 DAG.getConstant(NVTBits, Amt.getValueType()),
5183 Amt);
5184
5185 // Expand the incoming operand to be shifted, so that we have its parts
5186 SDOperand InL, InH;
5187 ExpandOp(Op, InL, InH);
5188 switch(Opc) {
5189 case ISD::SHL:
5190 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5191 Hi = DAG.getNode(ISD::OR, NVT,
5192 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5193 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5194 return true;
5195 case ISD::SRL:
5196 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5197 Lo = DAG.getNode(ISD::OR, NVT,
5198 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5199 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5200 return true;
5201 case ISD::SRA:
5202 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5203 Lo = DAG.getNode(ISD::OR, NVT,
5204 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5205 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5206 return true;
5207 }
5208 }
5209
5210 return false;
5211}
5212
5213
5214// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5215// does not fit into a register, return the lo part and set the hi part to the
5216// by-reg argument. If it does fit into a single register, return the result
5217// and leave the Hi part unset.
5218SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
5219 bool isSigned, SDOperand &Hi) {
5220 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5221 // The input chain to this libcall is the entry node of the function.
5222 // Legalizing the call will automatically add the previous call to the
5223 // dependence.
5224 SDOperand InChain = DAG.getEntryNode();
5225
5226 TargetLowering::ArgListTy Args;
5227 TargetLowering::ArgListEntry Entry;
5228 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5229 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5230 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
5231 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5232 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005233 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005234 Args.push_back(Entry);
5235 }
5236 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
5237
5238 // Splice the libcall in wherever FindInputOutputChains tells us to.
5239 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
5240 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005241 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5242 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243
5244 // Legalize the call sequence, starting with the chain. This will advance
5245 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5246 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5247 LegalizeOp(CallInfo.second);
5248 SDOperand Result;
5249 switch (getTypeAction(CallInfo.first.getValueType())) {
5250 default: assert(0 && "Unknown thing");
5251 case Legal:
5252 Result = CallInfo.first;
5253 break;
5254 case Expand:
5255 ExpandOp(CallInfo.first, Result, Hi);
5256 break;
5257 }
5258 return Result;
5259}
5260
5261
5262/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5263///
5264SDOperand SelectionDAGLegalize::
5265ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
5266 assert(getTypeAction(Source.getValueType()) == Expand &&
5267 "This is not an expansion!");
5268 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5269
5270 if (!isSigned) {
5271 assert(Source.getValueType() == MVT::i64 &&
5272 "This only works for 64-bit -> FP");
5273 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5274 // incoming integer is set. To handle this, we dynamically test to see if
5275 // it is set, and, if so, add a fudge factor.
5276 SDOperand Lo, Hi;
5277 ExpandOp(Source, Lo, Hi);
5278
5279 // If this is unsigned, and not supported, first perform the conversion to
5280 // signed, then adjust the result if the sign bit is set.
5281 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5282 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5283
5284 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5285 DAG.getConstant(0, Hi.getValueType()),
5286 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005287 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005288 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5289 SignSet, Four, Zero);
5290 uint64_t FF = 0x5f800000ULL;
5291 if (TLI.isLittleEndian()) FF <<= 32;
5292 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5293
5294 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5295 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5296 SDOperand FudgeInReg;
5297 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005298 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005299 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005300 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005302 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005303 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005304 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005305 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005306 else
5307 assert(0 && "Unexpected conversion");
5308
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 MVT::ValueType SCVT = SignedConv.getValueType();
5310 if (SCVT != DestTy) {
5311 // Destination type needs to be expanded as well. The FADD now we are
5312 // constructing will be expanded into a libcall.
5313 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5314 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5315 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5316 SignedConv, SignedConv.getValue(1));
5317 }
5318 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5319 }
5320 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5321 }
5322
5323 // Check to see if the target has a custom way to lower this. If so, use it.
5324 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5325 default: assert(0 && "This action not implemented for this operation!");
5326 case TargetLowering::Legal:
5327 case TargetLowering::Expand:
5328 break; // This case is handled below.
5329 case TargetLowering::Custom: {
5330 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5331 Source), DAG);
5332 if (NV.Val)
5333 return LegalizeOp(NV);
5334 break; // The target decided this was legal after all
5335 }
5336 }
5337
5338 // Expand the source, then glue it back together for the call. We must expand
5339 // the source in case it is shared (this pass of legalize must traverse it).
5340 SDOperand SrcLo, SrcHi;
5341 ExpandOp(Source, SrcLo, SrcHi);
5342 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5343
5344 RTLIB::Libcall LC;
5345 if (DestTy == MVT::f32)
5346 LC = RTLIB::SINTTOFP_I64_F32;
5347 else {
5348 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5349 LC = RTLIB::SINTTOFP_I64_F64;
5350 }
5351
5352 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
5353 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5354 SDOperand UnusedHiPart;
5355 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5356 UnusedHiPart);
5357}
5358
5359/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5360/// INT_TO_FP operation of the specified operand when the target requests that
5361/// we expand it. At this point, we know that the result and operand types are
5362/// legal for the target.
5363SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5364 SDOperand Op0,
5365 MVT::ValueType DestVT) {
5366 if (Op0.getValueType() == MVT::i32) {
5367 // simple 32-bit [signed|unsigned] integer to float/double expansion
5368
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005369 // Get the stack frame index of a 8 byte buffer.
5370 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372 // word offset constant for Hi/Lo address computation
5373 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5374 // set up Hi and Lo (into buffer) address based on endian
5375 SDOperand Hi = StackSlot;
5376 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5377 if (TLI.isLittleEndian())
5378 std::swap(Hi, Lo);
5379
5380 // if signed map to unsigned space
5381 SDOperand Op0Mapped;
5382 if (isSigned) {
5383 // constant used to invert sign bit (signed to unsigned mapping)
5384 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5385 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5386 } else {
5387 Op0Mapped = Op0;
5388 }
5389 // store the lo of the constructed double - based on integer input
5390 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5391 Op0Mapped, Lo, NULL, 0);
5392 // initial hi portion of constructed double
5393 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5394 // store the hi of the constructed double - biased exponent
5395 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5396 // load the constructed double
5397 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5398 // FP constant to bias correct the final result
5399 SDOperand Bias = DAG.getConstantFP(isSigned ?
5400 BitsToDouble(0x4330000080000000ULL)
5401 : BitsToDouble(0x4330000000000000ULL),
5402 MVT::f64);
5403 // subtract the bias
5404 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5405 // final result
5406 SDOperand Result;
5407 // handle final rounding
5408 if (DestVT == MVT::f64) {
5409 // do nothing
5410 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005411 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005412 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5413 DAG.getIntPtrConstant(0));
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005414 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5415 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416 }
5417 return Result;
5418 }
5419 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5420 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5421
5422 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5423 DAG.getConstant(0, Op0.getValueType()),
5424 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005425 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5427 SignSet, Four, Zero);
5428
5429 // If the sign bit of the integer is set, the large number will be treated
5430 // as a negative number. To counteract this, the dynamic code adds an
5431 // offset depending on the data type.
5432 uint64_t FF;
5433 switch (Op0.getValueType()) {
5434 default: assert(0 && "Unsupported integer type!");
5435 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5436 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5437 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5438 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5439 }
5440 if (TLI.isLittleEndian()) FF <<= 32;
5441 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5442
5443 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5444 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5445 SDOperand FudgeInReg;
5446 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005447 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005448 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005450 FudgeInReg =
5451 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5452 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005453 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005454 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 }
5456
5457 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5458}
5459
5460/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5461/// *INT_TO_FP operation of the specified operand when the target requests that
5462/// we promote it. At this point, we know that the result and operand types are
5463/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5464/// operation that takes a larger input.
5465SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5466 MVT::ValueType DestVT,
5467 bool isSigned) {
5468 // First step, figure out the appropriate *INT_TO_FP operation to use.
5469 MVT::ValueType NewInTy = LegalOp.getValueType();
5470
5471 unsigned OpToUse = 0;
5472
5473 // Scan for the appropriate larger type to use.
5474 while (1) {
5475 NewInTy = (MVT::ValueType)(NewInTy+1);
5476 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5477
5478 // If the target supports SINT_TO_FP of this type, use it.
5479 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5480 default: break;
5481 case TargetLowering::Legal:
5482 if (!TLI.isTypeLegal(NewInTy))
5483 break; // Can't use this datatype.
5484 // FALL THROUGH.
5485 case TargetLowering::Custom:
5486 OpToUse = ISD::SINT_TO_FP;
5487 break;
5488 }
5489 if (OpToUse) break;
5490 if (isSigned) continue;
5491
5492 // If the target supports UINT_TO_FP of this type, use it.
5493 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5494 default: break;
5495 case TargetLowering::Legal:
5496 if (!TLI.isTypeLegal(NewInTy))
5497 break; // Can't use this datatype.
5498 // FALL THROUGH.
5499 case TargetLowering::Custom:
5500 OpToUse = ISD::UINT_TO_FP;
5501 break;
5502 }
5503 if (OpToUse) break;
5504
5505 // Otherwise, try a larger type.
5506 }
5507
5508 // Okay, we found the operation and type to use. Zero extend our input to the
5509 // desired type then run the operation on it.
5510 return DAG.getNode(OpToUse, DestVT,
5511 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5512 NewInTy, LegalOp));
5513}
5514
5515/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5516/// FP_TO_*INT operation of the specified operand when the target requests that
5517/// we promote it. At this point, we know that the result and operand types are
5518/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5519/// operation that returns a larger result.
5520SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5521 MVT::ValueType DestVT,
5522 bool isSigned) {
5523 // First step, figure out the appropriate FP_TO*INT operation to use.
5524 MVT::ValueType NewOutTy = DestVT;
5525
5526 unsigned OpToUse = 0;
5527
5528 // Scan for the appropriate larger type to use.
5529 while (1) {
5530 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5531 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5532
5533 // If the target supports FP_TO_SINT returning this type, use it.
5534 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5535 default: break;
5536 case TargetLowering::Legal:
5537 if (!TLI.isTypeLegal(NewOutTy))
5538 break; // Can't use this datatype.
5539 // FALL THROUGH.
5540 case TargetLowering::Custom:
5541 OpToUse = ISD::FP_TO_SINT;
5542 break;
5543 }
5544 if (OpToUse) break;
5545
5546 // If the target supports FP_TO_UINT of this type, use it.
5547 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5548 default: break;
5549 case TargetLowering::Legal:
5550 if (!TLI.isTypeLegal(NewOutTy))
5551 break; // Can't use this datatype.
5552 // FALL THROUGH.
5553 case TargetLowering::Custom:
5554 OpToUse = ISD::FP_TO_UINT;
5555 break;
5556 }
5557 if (OpToUse) break;
5558
5559 // Otherwise, try a larger type.
5560 }
5561
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005562
5563 // Okay, we found the operation and type to use.
5564 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5565
5566 // If the operation produces an invalid type, it must be custom lowered. Use
5567 // the target lowering hooks to expand it. Just keep the low part of the
5568 // expanded operation, we know that we're truncating anyway.
5569 if (getTypeAction(NewOutTy) == Expand) {
5570 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5571 assert(Operation.Val && "Didn't return anything");
5572 }
5573
5574 // Truncate the result of the extended FP_TO_*INT operation to the desired
5575 // size.
5576 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577}
5578
5579/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5580///
5581SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5582 MVT::ValueType VT = Op.getValueType();
5583 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5584 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5585 switch (VT) {
5586 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5587 case MVT::i16:
5588 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5589 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5590 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5591 case MVT::i32:
5592 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5593 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5594 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5595 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5596 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5597 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5598 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5599 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5600 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5601 case MVT::i64:
5602 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5603 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5604 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5605 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5606 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5607 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5608 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5609 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5610 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5611 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5612 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5613 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5614 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5615 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5616 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5617 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5618 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5619 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5620 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5621 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5622 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5623 }
5624}
5625
5626/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5627///
5628SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5629 switch (Opc) {
5630 default: assert(0 && "Cannot expand this yet!");
5631 case ISD::CTPOP: {
5632 static const uint64_t mask[6] = {
5633 0x5555555555555555ULL, 0x3333333333333333ULL,
5634 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5635 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5636 };
5637 MVT::ValueType VT = Op.getValueType();
5638 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5639 unsigned len = MVT::getSizeInBits(VT);
5640 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5641 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5642 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5643 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5644 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5645 DAG.getNode(ISD::AND, VT,
5646 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5647 }
5648 return Op;
5649 }
5650 case ISD::CTLZ: {
5651 // for now, we do this:
5652 // x = x | (x >> 1);
5653 // x = x | (x >> 2);
5654 // ...
5655 // x = x | (x >>16);
5656 // x = x | (x >>32); // for 64-bit input
5657 // return popcount(~x);
5658 //
5659 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5660 MVT::ValueType VT = Op.getValueType();
5661 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5662 unsigned len = MVT::getSizeInBits(VT);
5663 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5664 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5665 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5666 }
5667 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5668 return DAG.getNode(ISD::CTPOP, VT, Op);
5669 }
5670 case ISD::CTTZ: {
5671 // for now, we use: { return popcount(~x & (x - 1)); }
5672 // unless the target has ctlz but not ctpop, in which case we use:
5673 // { return 32 - nlz(~x & (x-1)); }
5674 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5675 MVT::ValueType VT = Op.getValueType();
5676 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5677 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5678 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5679 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5680 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5681 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5682 TLI.isOperationLegal(ISD::CTLZ, VT))
5683 return DAG.getNode(ISD::SUB, VT,
5684 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5685 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5686 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5687 }
5688 }
5689}
5690
5691/// ExpandOp - Expand the specified SDOperand into its two component pieces
5692/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5693/// LegalizeNodes map is filled in for any results that are not expanded, the
5694/// ExpandedNodes map is filled in for any results that are expanded, and the
5695/// Lo/Hi values are returned.
5696void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5697 MVT::ValueType VT = Op.getValueType();
5698 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5699 SDNode *Node = Op.Val;
5700 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5701 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5702 MVT::isVector(VT)) &&
5703 "Cannot expand to FP value or to larger int value!");
5704
5705 // See if we already expanded it.
5706 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5707 = ExpandedNodes.find(Op);
5708 if (I != ExpandedNodes.end()) {
5709 Lo = I->second.first;
5710 Hi = I->second.second;
5711 return;
5712 }
5713
5714 switch (Node->getOpcode()) {
5715 case ISD::CopyFromReg:
5716 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005717 case ISD::FP_ROUND_INREG:
5718 if (VT == MVT::ppcf128 &&
5719 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5720 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005721 SDOperand SrcLo, SrcHi, Src;
5722 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5723 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5724 SDOperand Result = TLI.LowerOperation(
5725 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005726 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5727 Lo = Result.Val->getOperand(0);
5728 Hi = Result.Val->getOperand(1);
5729 break;
5730 }
5731 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 default:
5733#ifndef NDEBUG
5734 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5735#endif
5736 assert(0 && "Do not know how to expand this operator!");
5737 abort();
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005738 case ISD::EXTRACT_VECTOR_ELT:
5739 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5740 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5741 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5742 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743 case ISD::UNDEF:
5744 NVT = TLI.getTypeToExpandTo(VT);
5745 Lo = DAG.getNode(ISD::UNDEF, NVT);
5746 Hi = DAG.getNode(ISD::UNDEF, NVT);
5747 break;
5748 case ISD::Constant: {
5749 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5750 Lo = DAG.getConstant(Cst, NVT);
5751 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5752 break;
5753 }
5754 case ISD::ConstantFP: {
5755 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005756 if (CFP->getValueType(0) == MVT::ppcf128) {
5757 APInt api = CFP->getValueAPF().convertToAPInt();
5758 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5759 MVT::f64);
5760 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5761 MVT::f64);
5762 break;
5763 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5765 if (getTypeAction(Lo.getValueType()) == Expand)
5766 ExpandOp(Lo, Lo, Hi);
5767 break;
5768 }
5769 case ISD::BUILD_PAIR:
5770 // Return the operands.
5771 Lo = Node->getOperand(0);
5772 Hi = Node->getOperand(1);
5773 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005774
5775 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005776 if (Node->getNumValues() == 1) {
5777 ExpandOp(Op.getOperand(0), Lo, Hi);
5778 break;
5779 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005780 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5781 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5782 Op.getValue(1).getValueType() == MVT::Other &&
5783 "unhandled MERGE_VALUES");
5784 ExpandOp(Op.getOperand(0), Lo, Hi);
5785 // Remember that we legalized the chain.
5786 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5787 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005788
5789 case ISD::SIGN_EXTEND_INREG:
5790 ExpandOp(Node->getOperand(0), Lo, Hi);
5791 // sext_inreg the low part if needed.
5792 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5793
5794 // The high part gets the sign extension from the lo-part. This handles
5795 // things like sextinreg V:i64 from i8.
5796 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5797 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5798 TLI.getShiftAmountTy()));
5799 break;
5800
5801 case ISD::BSWAP: {
5802 ExpandOp(Node->getOperand(0), Lo, Hi);
5803 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5804 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5805 Lo = TempLo;
5806 break;
5807 }
5808
5809 case ISD::CTPOP:
5810 ExpandOp(Node->getOperand(0), Lo, Hi);
5811 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5812 DAG.getNode(ISD::CTPOP, NVT, Lo),
5813 DAG.getNode(ISD::CTPOP, NVT, Hi));
5814 Hi = DAG.getConstant(0, NVT);
5815 break;
5816
5817 case ISD::CTLZ: {
5818 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5819 ExpandOp(Node->getOperand(0), Lo, Hi);
5820 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5821 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5822 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5823 ISD::SETNE);
5824 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5825 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5826
5827 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5828 Hi = DAG.getConstant(0, NVT);
5829 break;
5830 }
5831
5832 case ISD::CTTZ: {
5833 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5834 ExpandOp(Node->getOperand(0), Lo, Hi);
5835 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5836 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5837 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5838 ISD::SETNE);
5839 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5840 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5841
5842 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5843 Hi = DAG.getConstant(0, NVT);
5844 break;
5845 }
5846
5847 case ISD::VAARG: {
5848 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5849 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5850 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5851 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5852
5853 // Remember that we legalized the chain.
5854 Hi = LegalizeOp(Hi);
5855 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005856 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005857 std::swap(Lo, Hi);
5858 break;
5859 }
5860
5861 case ISD::LOAD: {
5862 LoadSDNode *LD = cast<LoadSDNode>(Node);
5863 SDOperand Ch = LD->getChain(); // Legalize the chain.
5864 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5865 ISD::LoadExtType ExtType = LD->getExtensionType();
5866 int SVOffset = LD->getSrcValueOffset();
5867 unsigned Alignment = LD->getAlignment();
5868 bool isVolatile = LD->isVolatile();
5869
5870 if (ExtType == ISD::NON_EXTLOAD) {
5871 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5872 isVolatile, Alignment);
5873 if (VT == MVT::f32 || VT == MVT::f64) {
5874 // f32->i32 or f64->i64 one to one expansion.
5875 // Remember that we legalized the chain.
5876 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5877 // Recursively expand the new load.
5878 if (getTypeAction(NVT) == Expand)
5879 ExpandOp(Lo, Lo, Hi);
5880 break;
5881 }
5882
5883 // Increment the pointer to the other half.
5884 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5885 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005886 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005887 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005888 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005889 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5890 isVolatile, Alignment);
5891
5892 // Build a factor node to remember that this load is independent of the
5893 // other one.
5894 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5895 Hi.getValue(1));
5896
5897 // Remember that we legalized the chain.
5898 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005899 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005900 std::swap(Lo, Hi);
5901 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005902 MVT::ValueType EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005903
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005904 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5905 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005906 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5907 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5908 SVOffset, isVolatile, Alignment);
5909 // Remember that we legalized the chain.
5910 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5911 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5912 break;
5913 }
5914
5915 if (EVT == NVT)
5916 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5917 SVOffset, isVolatile, Alignment);
5918 else
5919 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5920 SVOffset, EVT, isVolatile,
5921 Alignment);
5922
5923 // Remember that we legalized the chain.
5924 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5925
5926 if (ExtType == ISD::SEXTLOAD) {
5927 // The high part is obtained by SRA'ing all but one of the bits of the
5928 // lo part.
5929 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5930 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5931 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5932 } else if (ExtType == ISD::ZEXTLOAD) {
5933 // The high part is just a zero.
5934 Hi = DAG.getConstant(0, NVT);
5935 } else /* if (ExtType == ISD::EXTLOAD) */ {
5936 // The high part is undefined.
5937 Hi = DAG.getNode(ISD::UNDEF, NVT);
5938 }
5939 }
5940 break;
5941 }
5942 case ISD::AND:
5943 case ISD::OR:
5944 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5945 SDOperand LL, LH, RL, RH;
5946 ExpandOp(Node->getOperand(0), LL, LH);
5947 ExpandOp(Node->getOperand(1), RL, RH);
5948 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5949 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5950 break;
5951 }
5952 case ISD::SELECT: {
5953 SDOperand LL, LH, RL, RH;
5954 ExpandOp(Node->getOperand(1), LL, LH);
5955 ExpandOp(Node->getOperand(2), RL, RH);
5956 if (getTypeAction(NVT) == Expand)
5957 NVT = TLI.getTypeToExpandTo(NVT);
5958 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5959 if (VT != MVT::f32)
5960 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5961 break;
5962 }
5963 case ISD::SELECT_CC: {
5964 SDOperand TL, TH, FL, FH;
5965 ExpandOp(Node->getOperand(2), TL, TH);
5966 ExpandOp(Node->getOperand(3), FL, FH);
5967 if (getTypeAction(NVT) == Expand)
5968 NVT = TLI.getTypeToExpandTo(NVT);
5969 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5970 Node->getOperand(1), TL, FL, Node->getOperand(4));
5971 if (VT != MVT::f32)
5972 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5973 Node->getOperand(1), TH, FH, Node->getOperand(4));
5974 break;
5975 }
5976 case ISD::ANY_EXTEND:
5977 // The low part is any extension of the input (which degenerates to a copy).
5978 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5979 // The high part is undefined.
5980 Hi = DAG.getNode(ISD::UNDEF, NVT);
5981 break;
5982 case ISD::SIGN_EXTEND: {
5983 // The low part is just a sign extension of the input (which degenerates to
5984 // a copy).
5985 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
5986
5987 // The high part is obtained by SRA'ing all but one of the bits of the lo
5988 // part.
5989 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5990 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5991 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5992 break;
5993 }
5994 case ISD::ZERO_EXTEND:
5995 // The low part is just a zero extension of the input (which degenerates to
5996 // a copy).
5997 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
5998
5999 // The high part is just a zero.
6000 Hi = DAG.getConstant(0, NVT);
6001 break;
6002
6003 case ISD::TRUNCATE: {
6004 // The input value must be larger than this value. Expand *it*.
6005 SDOperand NewLo;
6006 ExpandOp(Node->getOperand(0), NewLo, Hi);
6007
6008 // The low part is now either the right size, or it is closer. If not the
6009 // right size, make an illegal truncate so we recursively expand it.
6010 if (NewLo.getValueType() != Node->getValueType(0))
6011 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6012 ExpandOp(NewLo, Lo, Hi);
6013 break;
6014 }
6015
6016 case ISD::BIT_CONVERT: {
6017 SDOperand Tmp;
6018 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6019 // If the target wants to, allow it to lower this itself.
6020 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6021 case Expand: assert(0 && "cannot expand FP!");
6022 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6023 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6024 }
6025 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6026 }
6027
6028 // f32 / f64 must be expanded to i32 / i64.
6029 if (VT == MVT::f32 || VT == MVT::f64) {
6030 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6031 if (getTypeAction(NVT) == Expand)
6032 ExpandOp(Lo, Lo, Hi);
6033 break;
6034 }
6035
6036 // If source operand will be expanded to the same type as VT, i.e.
6037 // i64 <- f64, i32 <- f32, expand the source operand instead.
6038 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6039 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6040 ExpandOp(Node->getOperand(0), Lo, Hi);
6041 break;
6042 }
6043
6044 // Turn this into a load/store pair by default.
6045 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006046 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006047
6048 ExpandOp(Tmp, Lo, Hi);
6049 break;
6050 }
6051
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006052 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006053 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6054 TargetLowering::Custom &&
6055 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006056 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6057 assert(Tmp.Val && "Node must be custom expanded!");
6058 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006059 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006060 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006061 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006062 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063
6064 // These operators cannot be expanded directly, emit them as calls to
6065 // library functions.
6066 case ISD::FP_TO_SINT: {
6067 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6068 SDOperand Op;
6069 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6070 case Expand: assert(0 && "cannot expand FP!");
6071 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6072 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6073 }
6074
6075 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6076
6077 // Now that the custom expander is done, expand the result, which is still
6078 // VT.
6079 if (Op.Val) {
6080 ExpandOp(Op, Lo, Hi);
6081 break;
6082 }
6083 }
6084
Dale Johannesenac77b272007-10-05 20:04:43 +00006085 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006086 if (Node->getOperand(0).getValueType() == MVT::f32)
6087 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006088 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006089 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006090 else if (Node->getOperand(0).getValueType() == MVT::f80)
6091 LC = RTLIB::FPTOSINT_F80_I64;
6092 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6093 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006094 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6095 false/*sign irrelevant*/, Hi);
6096 break;
6097 }
6098
6099 case ISD::FP_TO_UINT: {
6100 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6101 SDOperand Op;
6102 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6103 case Expand: assert(0 && "cannot expand FP!");
6104 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6105 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6106 }
6107
6108 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6109
6110 // Now that the custom expander is done, expand the result.
6111 if (Op.Val) {
6112 ExpandOp(Op, Lo, Hi);
6113 break;
6114 }
6115 }
6116
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00006117 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006118 if (Node->getOperand(0).getValueType() == MVT::f32)
6119 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen4e1cf5d2007-09-28 18:44:17 +00006120 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006121 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006122 else if (Node->getOperand(0).getValueType() == MVT::f80)
6123 LC = RTLIB::FPTOUINT_F80_I64;
6124 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6125 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6127 false/*sign irrelevant*/, Hi);
6128 break;
6129 }
6130
6131 case ISD::SHL: {
6132 // If the target wants custom lowering, do so.
6133 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6134 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6135 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6136 Op = TLI.LowerOperation(Op, DAG);
6137 if (Op.Val) {
6138 // Now that the custom expander is done, expand the result, which is
6139 // still VT.
6140 ExpandOp(Op, Lo, Hi);
6141 break;
6142 }
6143 }
6144
6145 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6146 // this X << 1 as X+X.
6147 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6148 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6149 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6150 SDOperand LoOps[2], HiOps[3];
6151 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6152 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6153 LoOps[1] = LoOps[0];
6154 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6155
6156 HiOps[1] = HiOps[0];
6157 HiOps[2] = Lo.getValue(1);
6158 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6159 break;
6160 }
6161 }
6162
6163 // If we can emit an efficient shift operation, do so now.
6164 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6165 break;
6166
6167 // If this target supports SHL_PARTS, use it.
6168 TargetLowering::LegalizeAction Action =
6169 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6170 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6171 Action == TargetLowering::Custom) {
6172 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6173 break;
6174 }
6175
6176 // Otherwise, emit a libcall.
6177 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6178 false/*left shift=unsigned*/, Hi);
6179 break;
6180 }
6181
6182 case ISD::SRA: {
6183 // If the target wants custom lowering, do so.
6184 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6185 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6186 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6187 Op = TLI.LowerOperation(Op, DAG);
6188 if (Op.Val) {
6189 // Now that the custom expander is done, expand the result, which is
6190 // still VT.
6191 ExpandOp(Op, Lo, Hi);
6192 break;
6193 }
6194 }
6195
6196 // If we can emit an efficient shift operation, do so now.
6197 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6198 break;
6199
6200 // If this target supports SRA_PARTS, use it.
6201 TargetLowering::LegalizeAction Action =
6202 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6203 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6204 Action == TargetLowering::Custom) {
6205 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6206 break;
6207 }
6208
6209 // Otherwise, emit a libcall.
6210 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6211 true/*ashr is signed*/, Hi);
6212 break;
6213 }
6214
6215 case ISD::SRL: {
6216 // If the target wants custom lowering, do so.
6217 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6218 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6219 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6220 Op = TLI.LowerOperation(Op, DAG);
6221 if (Op.Val) {
6222 // Now that the custom expander is done, expand the result, which is
6223 // still VT.
6224 ExpandOp(Op, Lo, Hi);
6225 break;
6226 }
6227 }
6228
6229 // If we can emit an efficient shift operation, do so now.
6230 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6231 break;
6232
6233 // If this target supports SRL_PARTS, use it.
6234 TargetLowering::LegalizeAction Action =
6235 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6236 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6237 Action == TargetLowering::Custom) {
6238 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6239 break;
6240 }
6241
6242 // Otherwise, emit a libcall.
6243 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6244 false/*lshr is unsigned*/, Hi);
6245 break;
6246 }
6247
6248 case ISD::ADD:
6249 case ISD::SUB: {
6250 // If the target wants to custom expand this, let them.
6251 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6252 TargetLowering::Custom) {
6253 Op = TLI.LowerOperation(Op, DAG);
6254 if (Op.Val) {
6255 ExpandOp(Op, Lo, Hi);
6256 break;
6257 }
6258 }
6259
6260 // Expand the subcomponents.
6261 SDOperand LHSL, LHSH, RHSL, RHSH;
6262 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6263 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6264 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6265 SDOperand LoOps[2], HiOps[3];
6266 LoOps[0] = LHSL;
6267 LoOps[1] = RHSL;
6268 HiOps[0] = LHSH;
6269 HiOps[1] = RHSH;
6270 if (Node->getOpcode() == ISD::ADD) {
6271 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6272 HiOps[2] = Lo.getValue(1);
6273 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6274 } else {
6275 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6276 HiOps[2] = Lo.getValue(1);
6277 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6278 }
6279 break;
6280 }
6281
6282 case ISD::ADDC:
6283 case ISD::SUBC: {
6284 // Expand the subcomponents.
6285 SDOperand LHSL, LHSH, RHSL, RHSH;
6286 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6287 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6288 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6289 SDOperand LoOps[2] = { LHSL, RHSL };
6290 SDOperand HiOps[3] = { LHSH, RHSH };
6291
6292 if (Node->getOpcode() == ISD::ADDC) {
6293 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6294 HiOps[2] = Lo.getValue(1);
6295 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6296 } else {
6297 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6298 HiOps[2] = Lo.getValue(1);
6299 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6300 }
6301 // Remember that we legalized the flag.
6302 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6303 break;
6304 }
6305 case ISD::ADDE:
6306 case ISD::SUBE: {
6307 // Expand the subcomponents.
6308 SDOperand LHSL, LHSH, RHSL, RHSH;
6309 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6310 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6311 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6312 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6313 SDOperand HiOps[3] = { LHSH, RHSH };
6314
6315 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6316 HiOps[2] = Lo.getValue(1);
6317 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6318
6319 // Remember that we legalized the flag.
6320 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6321 break;
6322 }
6323 case ISD::MUL: {
6324 // If the target wants to custom expand this, let them.
6325 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6326 SDOperand New = TLI.LowerOperation(Op, DAG);
6327 if (New.Val) {
6328 ExpandOp(New, Lo, Hi);
6329 break;
6330 }
6331 }
6332
6333 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6334 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006335 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6336 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6337 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006338 SDOperand LL, LH, RL, RH;
6339 ExpandOp(Node->getOperand(0), LL, LH);
6340 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman5a199552007-10-08 18:33:35 +00006341 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6342 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6343 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6344 // FIXME: generalize this to handle other bit sizes
6345 if (LHSSB == 32 && RHSSB == 32 &&
6346 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6347 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6348 // The inputs are both zero-extended.
6349 if (HasUMUL_LOHI) {
6350 // We can emit a umul_lohi.
6351 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6352 Hi = SDOperand(Lo.Val, 1);
6353 break;
6354 }
6355 if (HasMULHU) {
6356 // We can emit a mulhu+mul.
6357 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6358 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6359 break;
6360 }
Dan Gohman5a199552007-10-08 18:33:35 +00006361 }
6362 if (LHSSB > BitSize && RHSSB > BitSize) {
6363 // The input values are both sign-extended.
6364 if (HasSMUL_LOHI) {
6365 // We can emit a smul_lohi.
6366 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6367 Hi = SDOperand(Lo.Val, 1);
6368 break;
6369 }
6370 if (HasMULHS) {
6371 // We can emit a mulhs+mul.
6372 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6373 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6374 break;
6375 }
6376 }
6377 if (HasUMUL_LOHI) {
6378 // Lo,Hi = umul LHS, RHS.
6379 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6380 DAG.getVTList(NVT, NVT), LL, RL);
6381 Lo = UMulLOHI;
6382 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006383 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6384 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6385 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6386 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6387 break;
6388 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006389 if (HasMULHU) {
6390 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6391 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6392 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6393 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6394 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6395 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6396 break;
6397 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006398 }
6399
Dan Gohman5a199552007-10-08 18:33:35 +00006400 // If nothing else, we can make a libcall.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006401 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6402 false/*sign irrelevant*/, Hi);
6403 break;
6404 }
6405 case ISD::SDIV:
6406 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6407 break;
6408 case ISD::UDIV:
6409 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6410 break;
6411 case ISD::SREM:
6412 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6413 break;
6414 case ISD::UREM:
6415 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6416 break;
6417
6418 case ISD::FADD:
Duncan Sands37a3f472008-01-10 10:28:30 +00006419 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6420 RTLIB::ADD_F64,
6421 RTLIB::ADD_F80,
6422 RTLIB::ADD_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006423 Node, false, Hi);
6424 break;
6425 case ISD::FSUB:
Duncan Sands37a3f472008-01-10 10:28:30 +00006426 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6427 RTLIB::SUB_F64,
6428 RTLIB::SUB_F80,
6429 RTLIB::SUB_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006430 Node, false, Hi);
6431 break;
6432 case ISD::FMUL:
Duncan Sands37a3f472008-01-10 10:28:30 +00006433 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6434 RTLIB::MUL_F64,
6435 RTLIB::MUL_F80,
6436 RTLIB::MUL_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006437 Node, false, Hi);
6438 break;
6439 case ISD::FDIV:
Duncan Sands37a3f472008-01-10 10:28:30 +00006440 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6441 RTLIB::DIV_F64,
6442 RTLIB::DIV_F80,
6443 RTLIB::DIV_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 Node, false, Hi);
6445 break;
6446 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006447 if (VT == MVT::ppcf128) {
6448 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6449 Node->getOperand(0).getValueType()==MVT::f64);
6450 const uint64_t zero = 0;
6451 if (Node->getOperand(0).getValueType()==MVT::f32)
6452 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6453 else
6454 Hi = Node->getOperand(0);
6455 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6456 break;
6457 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
6459 break;
6460 case ISD::FP_ROUND:
6461 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
6462 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006463 case ISD::FPOWI:
Duncan Sands37a3f472008-01-10 10:28:30 +00006464 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6465 RTLIB::POWI_F64,
6466 RTLIB::POWI_F80,
6467 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006468 Node, false, Hi);
6469 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006470 case ISD::FSQRT:
6471 case ISD::FSIN:
6472 case ISD::FCOS: {
6473 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6474 switch(Node->getOpcode()) {
6475 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006476 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6477 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006478 break;
6479 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006480 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6481 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006482 break;
6483 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006484 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6485 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006486 break;
6487 default: assert(0 && "Unreachable!");
6488 }
6489 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
6490 break;
6491 }
6492 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006493 if (VT == MVT::ppcf128) {
6494 SDOperand Tmp;
6495 ExpandOp(Node->getOperand(0), Lo, Tmp);
6496 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6497 // lo = hi==fabs(hi) ? lo : -lo;
6498 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6499 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6500 DAG.getCondCode(ISD::SETEQ));
6501 break;
6502 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 SDOperand Mask = (VT == MVT::f64)
6504 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6505 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6506 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6507 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6508 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6509 if (getTypeAction(NVT) == Expand)
6510 ExpandOp(Lo, Lo, Hi);
6511 break;
6512 }
6513 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006514 if (VT == MVT::ppcf128) {
6515 ExpandOp(Node->getOperand(0), Lo, Hi);
6516 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6517 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6518 break;
6519 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520 SDOperand Mask = (VT == MVT::f64)
6521 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6522 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6523 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6524 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6525 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6526 if (getTypeAction(NVT) == Expand)
6527 ExpandOp(Lo, Lo, Hi);
6528 break;
6529 }
6530 case ISD::FCOPYSIGN: {
6531 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6532 if (getTypeAction(NVT) == Expand)
6533 ExpandOp(Lo, Lo, Hi);
6534 break;
6535 }
6536 case ISD::SINT_TO_FP:
6537 case ISD::UINT_TO_FP: {
6538 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6539 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006540 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006541 static uint64_t zero = 0;
6542 if (isSigned) {
6543 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6544 Node->getOperand(0)));
6545 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6546 } else {
6547 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6548 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6549 Node->getOperand(0)));
6550 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6551 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006552 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006553 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6554 DAG.getConstant(0, MVT::i32),
6555 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6556 DAG.getConstantFP(
6557 APFloat(APInt(128, 2, TwoE32)),
6558 MVT::ppcf128)),
6559 Hi,
6560 DAG.getCondCode(ISD::SETLT)),
6561 Lo, Hi);
6562 }
6563 break;
6564 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006565 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6566 // si64->ppcf128 done by libcall, below
6567 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6568 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6569 Lo, Hi);
6570 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6571 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6572 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6573 DAG.getConstant(0, MVT::i64),
6574 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6575 DAG.getConstantFP(
6576 APFloat(APInt(128, 2, TwoE64)),
6577 MVT::ppcf128)),
6578 Hi,
6579 DAG.getCondCode(ISD::SETLT)),
6580 Lo, Hi);
6581 break;
6582 }
Evan Cheng20186812007-09-27 07:35:39 +00006583 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584 if (Node->getOperand(0).getValueType() == MVT::i64) {
6585 if (VT == MVT::f32)
6586 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006587 else if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006588 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006589 else if (VT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00006590 assert(isSigned);
Dale Johannesenac77b272007-10-05 20:04:43 +00006591 LC = RTLIB::SINTTOFP_I64_F80;
6592 }
6593 else if (VT == MVT::ppcf128) {
6594 assert(isSigned);
6595 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006596 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006597 } else {
6598 if (VT == MVT::f32)
6599 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
6600 else
6601 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
6602 }
6603
6604 // Promote the operand if needed.
6605 if (getTypeAction(SrcVT) == Promote) {
6606 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6607 Tmp = isSigned
6608 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6609 DAG.getValueType(SrcVT))
6610 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6611 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6612 }
6613
6614 const char *LibCall = TLI.getLibcallName(LC);
6615 if (LibCall)
6616 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6617 else {
6618 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6619 Node->getOperand(0));
6620 if (getTypeAction(Lo.getValueType()) == Expand)
6621 ExpandOp(Lo, Lo, Hi);
6622 }
6623 break;
6624 }
6625 }
6626
6627 // Make sure the resultant values have been legalized themselves, unless this
6628 // is a type that requires multi-step expansion.
6629 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6630 Lo = LegalizeOp(Lo);
6631 if (Hi.Val)
6632 // Don't legalize the high part if it is expanded to a single node.
6633 Hi = LegalizeOp(Hi);
6634 }
6635
6636 // Remember in a map if the values will be reused later.
6637 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6638 assert(isNew && "Value already expanded?!?");
6639}
6640
6641/// SplitVectorOp - Given an operand of vector type, break it down into
6642/// two smaller values, still of vector type.
6643void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6644 SDOperand &Hi) {
6645 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6646 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006647 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006648 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006649
Dan Gohmana0763d92007-09-24 15:54:53 +00006650 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006651
6652 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6653 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6654
6655 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6656 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6657
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006658 // See if we already split it.
6659 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6660 = SplitNodes.find(Op);
6661 if (I != SplitNodes.end()) {
6662 Lo = I->second.first;
6663 Hi = I->second.second;
6664 return;
6665 }
6666
6667 switch (Node->getOpcode()) {
6668 default:
6669#ifndef NDEBUG
6670 Node->dump(&DAG);
6671#endif
6672 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006673 case ISD::UNDEF:
6674 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6675 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6676 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006677 case ISD::BUILD_PAIR:
6678 Lo = Node->getOperand(0);
6679 Hi = Node->getOperand(1);
6680 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006681 case ISD::INSERT_VECTOR_ELT: {
6682 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6683 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6684 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006685 if (Index < NewNumElts_Lo)
6686 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006687 DAG.getConstant(Index, TLI.getPointerTy()));
6688 else
Nate Begeman4a365ad2007-11-15 21:15:26 +00006689 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6690 DAG.getConstant(Index - NewNumElts_Lo,
6691 TLI.getPointerTy()));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006692 break;
6693 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006694 case ISD::VECTOR_SHUFFLE: {
6695 // Build the low part.
6696 SDOperand Mask = Node->getOperand(2);
6697 SmallVector<SDOperand, 8> Ops;
6698 MVT::ValueType PtrVT = TLI.getPointerTy();
6699
6700 // Insert all of the elements from the input that are needed. We use
6701 // buildvector of extractelement here because the input vectors will have
6702 // to be legalized, so this makes the code simpler.
6703 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6704 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6705 SDOperand InVec = Node->getOperand(0);
6706 if (Idx >= NumElements) {
6707 InVec = Node->getOperand(1);
6708 Idx -= NumElements;
6709 }
6710 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6711 DAG.getConstant(Idx, PtrVT)));
6712 }
6713 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6714 Ops.clear();
6715
6716 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6717 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6718 SDOperand InVec = Node->getOperand(0);
6719 if (Idx >= NumElements) {
6720 InVec = Node->getOperand(1);
6721 Idx -= NumElements;
6722 }
6723 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6724 DAG.getConstant(Idx, PtrVT)));
6725 }
6726 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6727 break;
6728 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006729 case ISD::BUILD_VECTOR: {
6730 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006731 Node->op_begin()+NewNumElts_Lo);
6732 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006733
Nate Begeman4a365ad2007-11-15 21:15:26 +00006734 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006735 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006736 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006737 break;
6738 }
6739 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006740 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006741 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6742 if (NewNumSubvectors == 1) {
6743 Lo = Node->getOperand(0);
6744 Hi = Node->getOperand(1);
6745 } else {
6746 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6747 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006748 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006749
6750 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6751 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006752 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006753 }
6754 break;
6755 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006756 case ISD::SELECT: {
6757 SDOperand Cond = Node->getOperand(0);
6758
6759 SDOperand LL, LH, RL, RH;
6760 SplitVectorOp(Node->getOperand(1), LL, LH);
6761 SplitVectorOp(Node->getOperand(2), RL, RH);
6762
6763 if (MVT::isVector(Cond.getValueType())) {
6764 // Handle a vector merge.
6765 SDOperand CL, CH;
6766 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006767 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6768 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006769 } else {
6770 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006771 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6772 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006773 }
6774 break;
6775 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006776 case ISD::ADD:
6777 case ISD::SUB:
6778 case ISD::MUL:
6779 case ISD::FADD:
6780 case ISD::FSUB:
6781 case ISD::FMUL:
6782 case ISD::SDIV:
6783 case ISD::UDIV:
6784 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006785 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006786 case ISD::AND:
6787 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006788 case ISD::XOR:
6789 case ISD::UREM:
6790 case ISD::SREM:
6791 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006792 SDOperand LL, LH, RL, RH;
6793 SplitVectorOp(Node->getOperand(0), LL, LH);
6794 SplitVectorOp(Node->getOperand(1), RL, RH);
6795
Nate Begeman4a365ad2007-11-15 21:15:26 +00006796 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6797 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006798 break;
6799 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006800 case ISD::FPOWI: {
6801 SDOperand L, H;
6802 SplitVectorOp(Node->getOperand(0), L, H);
6803
Nate Begeman4a365ad2007-11-15 21:15:26 +00006804 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6805 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006806 break;
6807 }
6808 case ISD::CTTZ:
6809 case ISD::CTLZ:
6810 case ISD::CTPOP:
6811 case ISD::FNEG:
6812 case ISD::FABS:
6813 case ISD::FSQRT:
6814 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006815 case ISD::FCOS:
6816 case ISD::FP_TO_SINT:
6817 case ISD::FP_TO_UINT:
6818 case ISD::SINT_TO_FP:
6819 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006820 SDOperand L, H;
6821 SplitVectorOp(Node->getOperand(0), L, H);
6822
Nate Begeman4a365ad2007-11-15 21:15:26 +00006823 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6824 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006825 break;
6826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827 case ISD::LOAD: {
6828 LoadSDNode *LD = cast<LoadSDNode>(Node);
6829 SDOperand Ch = LD->getChain();
6830 SDOperand Ptr = LD->getBasePtr();
6831 const Value *SV = LD->getSrcValue();
6832 int SVOffset = LD->getSrcValueOffset();
6833 unsigned Alignment = LD->getAlignment();
6834 bool isVolatile = LD->isVolatile();
6835
Nate Begeman4a365ad2007-11-15 21:15:26 +00006836 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6837 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006838 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006839 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006840 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006841 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006842 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006843
6844 // Build a factor node to remember that this load is independent of the
6845 // other one.
6846 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6847 Hi.getValue(1));
6848
6849 // Remember that we legalized the chain.
6850 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6851 break;
6852 }
6853 case ISD::BIT_CONVERT: {
6854 // We know the result is a vector. The input may be either a vector or a
6855 // scalar value.
6856 SDOperand InOp = Node->getOperand(0);
6857 if (!MVT::isVector(InOp.getValueType()) ||
6858 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6859 // The input is a scalar or single-element vector.
6860 // Lower to a store/load so that it can be split.
6861 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006862 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman20e37962008-02-11 18:58:42 +00006863 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006864
6865 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006866 InOp, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006867 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006868 FI->getIndex());
6869 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006870 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006871 FI->getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006872 }
6873 // Split the vector and convert each of the pieces now.
6874 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006875 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6876 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006877 break;
6878 }
6879 }
6880
6881 // Remember in a map if the values will be reused later.
6882 bool isNew =
6883 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6884 assert(isNew && "Value already split?!?");
6885}
6886
6887
6888/// ScalarizeVectorOp - Given an operand of single-element vector type
6889/// (e.g. v1f32), convert it into the equivalent operation that returns a
6890/// scalar (e.g. f32) value.
6891SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6892 assert(MVT::isVector(Op.getValueType()) &&
6893 "Bad ScalarizeVectorOp invocation!");
6894 SDNode *Node = Op.Val;
6895 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6896 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6897
6898 // See if we already scalarized it.
6899 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6900 if (I != ScalarizedNodes.end()) return I->second;
6901
6902 SDOperand Result;
6903 switch (Node->getOpcode()) {
6904 default:
6905#ifndef NDEBUG
6906 Node->dump(&DAG); cerr << "\n";
6907#endif
6908 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6909 case ISD::ADD:
6910 case ISD::FADD:
6911 case ISD::SUB:
6912 case ISD::FSUB:
6913 case ISD::MUL:
6914 case ISD::FMUL:
6915 case ISD::SDIV:
6916 case ISD::UDIV:
6917 case ISD::FDIV:
6918 case ISD::SREM:
6919 case ISD::UREM:
6920 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006921 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006922 case ISD::AND:
6923 case ISD::OR:
6924 case ISD::XOR:
6925 Result = DAG.getNode(Node->getOpcode(),
6926 NewVT,
6927 ScalarizeVectorOp(Node->getOperand(0)),
6928 ScalarizeVectorOp(Node->getOperand(1)));
6929 break;
6930 case ISD::FNEG:
6931 case ISD::FABS:
6932 case ISD::FSQRT:
6933 case ISD::FSIN:
6934 case ISD::FCOS:
6935 Result = DAG.getNode(Node->getOpcode(),
6936 NewVT,
6937 ScalarizeVectorOp(Node->getOperand(0)));
6938 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006939 case ISD::FPOWI:
6940 Result = DAG.getNode(Node->getOpcode(),
6941 NewVT,
6942 ScalarizeVectorOp(Node->getOperand(0)),
6943 Node->getOperand(1));
6944 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006945 case ISD::LOAD: {
6946 LoadSDNode *LD = cast<LoadSDNode>(Node);
6947 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6948 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6949
6950 const Value *SV = LD->getSrcValue();
6951 int SVOffset = LD->getSrcValueOffset();
6952 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6953 LD->isVolatile(), LD->getAlignment());
6954
6955 // Remember that we legalized the chain.
6956 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6957 break;
6958 }
6959 case ISD::BUILD_VECTOR:
6960 Result = Node->getOperand(0);
6961 break;
6962 case ISD::INSERT_VECTOR_ELT:
6963 // Returning the inserted scalar element.
6964 Result = Node->getOperand(1);
6965 break;
6966 case ISD::CONCAT_VECTORS:
6967 assert(Node->getOperand(0).getValueType() == NewVT &&
6968 "Concat of non-legal vectors not yet supported!");
6969 Result = Node->getOperand(0);
6970 break;
6971 case ISD::VECTOR_SHUFFLE: {
6972 // Figure out if the scalar is the LHS or RHS and return it.
6973 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6974 if (cast<ConstantSDNode>(EltNum)->getValue())
6975 Result = ScalarizeVectorOp(Node->getOperand(1));
6976 else
6977 Result = ScalarizeVectorOp(Node->getOperand(0));
6978 break;
6979 }
6980 case ISD::EXTRACT_SUBVECTOR:
6981 Result = Node->getOperand(0);
6982 assert(Result.getValueType() == NewVT);
6983 break;
6984 case ISD::BIT_CONVERT:
6985 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
6986 break;
6987 case ISD::SELECT:
6988 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
6989 ScalarizeVectorOp(Op.getOperand(1)),
6990 ScalarizeVectorOp(Op.getOperand(2)));
6991 break;
6992 }
6993
6994 if (TLI.isTypeLegal(NewVT))
6995 Result = LegalizeOp(Result);
6996 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6997 assert(isNew && "Value already scalarized?");
6998 return Result;
6999}
7000
7001
7002// SelectionDAG::Legalize - This is the entry point for the file.
7003//
7004void SelectionDAG::Legalize() {
7005 if (ViewLegalizeDAGs) viewGraph();
7006
7007 /// run - This is the main entry point to this class.
7008 ///
7009 SelectionDAGLegalize(*this).LegalizeDAG();
7010}
7011