blob: bd9e2302fb6e5e66c8419305935a22311a929b3a [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();
Dale Johannesendc0ee192008-02-27 22:36:00 +0000572 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
573 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen08275382007-09-08 19:29:23 +0000574 // Expand to a bitconvert of the value to the integer type of the
575 // same size, then a (misaligned) int store.
576 MVT::ValueType intVT;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000577 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000578 intVT = MVT::i128;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000579 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000580 intVT = MVT::i64;
581 else if (VT==MVT::f32)
582 intVT = MVT::i32;
583 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000584 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000585
586 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
587 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
588 SVOffset, ST->isVolatile(), Alignment);
589 }
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000590 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesendc0ee192008-02-27 22:36:00 +0000591 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen08275382007-09-08 19:29:23 +0000592 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000593 // Get the half-size VT
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000594 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000595 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000596 int IncrementSize = NumBits / 8;
597
598 // Divide the stored value in two parts.
599 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
600 SDOperand Lo = Val;
601 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
602
603 // Store the two parts
604 SDOperand Store1, Store2;
605 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
606 ST->getSrcValue(), SVOffset, NewStoredVT,
607 ST->isVolatile(), Alignment);
608 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
609 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000610 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000611 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
612 ST->getSrcValue(), SVOffset + IncrementSize,
613 NewStoredVT, ST->isVolatile(), Alignment);
614
615 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
616}
617
618/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
619static
620SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
621 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000622 int SVOffset = LD->getSrcValueOffset();
623 SDOperand Chain = LD->getChain();
624 SDOperand Ptr = LD->getBasePtr();
625 MVT::ValueType VT = LD->getValueType(0);
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000626 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesendc0ee192008-02-27 22:36:00 +0000627 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen08275382007-09-08 19:29:23 +0000628 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000629 // then bitconvert to floating point or vector.
Dale Johannesen08275382007-09-08 19:29:23 +0000630 MVT::ValueType intVT;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000631 if (MVT::is128BitVector(LoadedVT) ||
632 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000633 intVT = MVT::i128;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000634 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000635 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000636 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000637 intVT = MVT::i32;
638 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000639 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000640
641 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
642 SVOffset, LD->isVolatile(),
643 LD->getAlignment());
644 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesendc0ee192008-02-27 22:36:00 +0000645 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000646 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
647
648 SDOperand Ops[] = { Result, Chain };
649 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
650 Ops, 2);
651 }
Dale Johannesendc0ee192008-02-27 22:36:00 +0000652 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000653 "Unaligned load of unsupported type.");
654
Dale Johannesendc0ee192008-02-27 22:36:00 +0000655 // Compute the new VT that is half the size of the old one. This is an
656 // integer MVT.
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000657 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
658 MVT::ValueType NewLoadedVT;
Dale Johannesendc0ee192008-02-27 22:36:00 +0000659 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000660 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;
Chris Lattner203cd052008-02-28 05:53:40 +00001103 case TargetLowering::Expand:
1104 Result = LegalizeOp(Node->getOperand(0));
1105 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001106 }
1107 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108
1109 case ISD::DEBUG_LOC:
1110 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1111 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1112 default: assert(0 && "This action is not supported yet!");
1113 case TargetLowering::Legal:
1114 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1115 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1116 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1117 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1118 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1119 break;
1120 }
1121 break;
1122
1123 case ISD::LABEL:
Evan Cheng13d1c292008-01-31 09:59:15 +00001124 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1126 default: assert(0 && "This action is not supported yet!");
1127 case TargetLowering::Legal:
1128 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1129 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng13d1c292008-01-31 09:59:15 +00001130 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 break;
1133 case TargetLowering::Expand:
1134 Result = LegalizeOp(Node->getOperand(0));
1135 break;
1136 }
1137 break;
1138
Andrew Lenharth785610d2008-02-16 01:24:58 +00001139 case ISD::MEMBARRIER: {
1140 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001141 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1142 default: assert(0 && "This action is not supported yet!");
1143 case TargetLowering::Legal: {
1144 SDOperand Ops[6];
1145 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001146 for (int x = 1; x < 6; ++x) {
1147 Ops[x] = Node->getOperand(x);
1148 if (!isTypeLegal(Ops[x].getValueType()))
1149 Ops[x] = PromoteOp(Ops[x]);
1150 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001151 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1152 break;
1153 }
1154 case TargetLowering::Expand:
1155 //There is no libgcc call for this op
1156 Result = Node->getOperand(0); // Noop
1157 break;
1158 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001159 break;
1160 }
1161
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001162 case ISD::ATOMIC_LCS:
1163 case ISD::ATOMIC_LAS:
1164 case ISD::ATOMIC_SWAP: {
1165 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1166 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1167 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
1168 "Invalid MemBarrier node!");
1169 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
1170 MVT::ValueType VT = Node->getValueType(0);
1171 switch (TLI.getOperationAction(ISD::ATOMIC_LCS, VT)) {
1172 default: assert(0 && "This action is not supported yet!");
1173 case TargetLowering::Legal: {
1174 SDOperand Ops[4];
1175 for (int x = 0; x < num; ++x)
1176 Ops[x] = LegalizeOp(Node->getOperand(x));
1177 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1178 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1179 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1180 return Result.getValue(Op.ResNo);
1181 break;
1182 }
1183 }
1184 break;
1185 }
1186
Scott Michelf2e2b702007-08-08 23:23:31 +00001187 case ISD::Constant: {
1188 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1189 unsigned opAction =
1190 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 // We know we don't need to expand constants here, constants only have one
1193 // value and we check that it is fine above.
1194
Scott Michelf2e2b702007-08-08 23:23:31 +00001195 if (opAction == TargetLowering::Custom) {
1196 Tmp1 = TLI.LowerOperation(Result, DAG);
1197 if (Tmp1.Val)
1198 Result = Tmp1;
1199 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001201 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 case ISD::ConstantFP: {
1203 // Spill FP immediates to the constant pool if the target cannot directly
1204 // codegen them. Targets often have some immediate values that can be
1205 // efficiently generated into an FP register without a load. We explicitly
1206 // leave these constants as ConstantFP nodes for the target to deal with.
1207 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1210 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001211 case TargetLowering::Legal:
1212 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 case TargetLowering::Custom:
1214 Tmp3 = TLI.LowerOperation(Result, DAG);
1215 if (Tmp3.Val) {
1216 Result = Tmp3;
1217 break;
1218 }
1219 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001220 case TargetLowering::Expand: {
1221 // Check to see if this FP immediate is already legal.
1222 bool isLegal = false;
1223 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1224 E = TLI.legal_fpimm_end(); I != E; ++I) {
1225 if (CFP->isExactlyValue(*I)) {
1226 isLegal = true;
1227 break;
1228 }
1229 }
1230 // If this is a legal constant, turn it into a TargetConstantFP node.
1231 if (isLegal)
1232 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1234 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001235 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 break;
1237 }
1238 case ISD::TokenFactor:
1239 if (Node->getNumOperands() == 2) {
1240 Tmp1 = LegalizeOp(Node->getOperand(0));
1241 Tmp2 = LegalizeOp(Node->getOperand(1));
1242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1243 } else if (Node->getNumOperands() == 3) {
1244 Tmp1 = LegalizeOp(Node->getOperand(0));
1245 Tmp2 = LegalizeOp(Node->getOperand(1));
1246 Tmp3 = LegalizeOp(Node->getOperand(2));
1247 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1248 } else {
1249 SmallVector<SDOperand, 8> Ops;
1250 // Legalize the operands.
1251 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1252 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1253 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1254 }
1255 break;
1256
1257 case ISD::FORMAL_ARGUMENTS:
1258 case ISD::CALL:
1259 // The only option for this is to custom lower it.
1260 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1261 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001262
1263 // The number of incoming and outgoing values should match; unless the final
1264 // outgoing value is a flag.
1265 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1266 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1267 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1268 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 "Lowering call/formal_arguments produced unexpected # results!");
1270
1271 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1272 // remember that we legalized all of them, so it doesn't get relegalized.
1273 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001274 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1275 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1277 if (Op.ResNo == i)
1278 Tmp2 = Tmp1;
1279 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1280 }
1281 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001282 case ISD::EXTRACT_SUBREG: {
1283 Tmp1 = LegalizeOp(Node->getOperand(0));
1284 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1285 assert(idx && "Operand must be a constant");
1286 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1287 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1288 }
1289 break;
1290 case ISD::INSERT_SUBREG: {
1291 Tmp1 = LegalizeOp(Node->getOperand(0));
1292 Tmp2 = LegalizeOp(Node->getOperand(1));
1293 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1294 assert(idx && "Operand must be a constant");
1295 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1296 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1297 }
1298 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 case ISD::BUILD_VECTOR:
1300 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1301 default: assert(0 && "This action is not supported yet!");
1302 case TargetLowering::Custom:
1303 Tmp3 = TLI.LowerOperation(Result, DAG);
1304 if (Tmp3.Val) {
1305 Result = Tmp3;
1306 break;
1307 }
1308 // FALLTHROUGH
1309 case TargetLowering::Expand:
1310 Result = ExpandBUILD_VECTOR(Result.Val);
1311 break;
1312 }
1313 break;
1314 case ISD::INSERT_VECTOR_ELT:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001317
1318 // The type of the value to insert may not be legal, even though the vector
1319 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1320 // here.
1321 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1322 default: assert(0 && "Cannot expand insert element operand");
1323 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1324 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1325 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1327
1328 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1329 Node->getValueType(0))) {
1330 default: assert(0 && "This action is not supported yet!");
1331 case TargetLowering::Legal:
1332 break;
1333 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001334 Tmp4 = TLI.LowerOperation(Result, DAG);
1335 if (Tmp4.Val) {
1336 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 break;
1338 }
1339 // FALLTHROUGH
1340 case TargetLowering::Expand: {
1341 // If the insert index is a constant, codegen this as a scalar_to_vector,
1342 // then a shuffle that inserts it into the right position in the vector.
1343 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001344 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1345 // match the element type of the vector being created.
1346 if (Tmp2.getValueType() ==
1347 MVT::getVectorElementType(Op.getValueType())) {
1348 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1349 Tmp1.getValueType(), Tmp2);
1350
1351 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1352 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1353 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1354
1355 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1356 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1357 // elt 0 of the RHS.
1358 SmallVector<SDOperand, 8> ShufOps;
1359 for (unsigned i = 0; i != NumElts; ++i) {
1360 if (i != InsertPos->getValue())
1361 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1362 else
1363 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1364 }
1365 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1366 &ShufOps[0], ShufOps.size());
1367
1368 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1369 Tmp1, ScVec, ShufMask);
1370 Result = LegalizeOp(Result);
1371 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 }
1374
1375 // If the target doesn't support this, we have to spill the input vector
1376 // to a temporary stack slot, update the element, then reload it. This is
1377 // badness. We could also load the value into a vector register (either
1378 // with a "move to register" or "extload into register" instruction, then
1379 // permute it into place, if the idx is a constant and if the idx is
1380 // supported by the target.
1381 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001382 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 MVT::ValueType IdxVT = Tmp3.getValueType();
1384 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001385 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman12a9c082008-02-06 22:27:42 +00001386
Dan Gohman20e37962008-02-11 18:58:42 +00001387 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman12a9c082008-02-06 22:27:42 +00001388 int SPFI = StackPtrFI->getIndex();
1389
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 // Store the vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001391 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001392 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001393 SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394
1395 // Truncate or zero extend offset to target pointer type.
1396 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1397 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1398 // Add the offset to the index.
1399 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1400 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1401 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1402 // Store the scalar value.
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001403 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1404 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 // Load the updated vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001406 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001407 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408 break;
1409 }
1410 }
1411 break;
1412 case ISD::SCALAR_TO_VECTOR:
1413 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1414 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1415 break;
1416 }
1417
1418 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1419 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1420 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1421 Node->getValueType(0))) {
1422 default: assert(0 && "This action is not supported yet!");
1423 case TargetLowering::Legal:
1424 break;
1425 case TargetLowering::Custom:
1426 Tmp3 = TLI.LowerOperation(Result, DAG);
1427 if (Tmp3.Val) {
1428 Result = Tmp3;
1429 break;
1430 }
1431 // FALLTHROUGH
1432 case TargetLowering::Expand:
1433 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1434 break;
1435 }
1436 break;
1437 case ISD::VECTOR_SHUFFLE:
1438 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1439 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1440 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1441
1442 // Allow targets to custom lower the SHUFFLEs they support.
1443 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1444 default: assert(0 && "Unknown operation action!");
1445 case TargetLowering::Legal:
1446 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1447 "vector shuffle should not be created if not legal!");
1448 break;
1449 case TargetLowering::Custom:
1450 Tmp3 = TLI.LowerOperation(Result, DAG);
1451 if (Tmp3.Val) {
1452 Result = Tmp3;
1453 break;
1454 }
1455 // FALLTHROUGH
1456 case TargetLowering::Expand: {
1457 MVT::ValueType VT = Node->getValueType(0);
1458 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1459 MVT::ValueType PtrVT = TLI.getPointerTy();
1460 SDOperand Mask = Node->getOperand(2);
1461 unsigned NumElems = Mask.getNumOperands();
1462 SmallVector<SDOperand,8> Ops;
1463 for (unsigned i = 0; i != NumElems; ++i) {
1464 SDOperand Arg = Mask.getOperand(i);
1465 if (Arg.getOpcode() == ISD::UNDEF) {
1466 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1467 } else {
1468 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1469 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1470 if (Idx < NumElems)
1471 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1472 DAG.getConstant(Idx, PtrVT)));
1473 else
1474 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1475 DAG.getConstant(Idx - NumElems, PtrVT)));
1476 }
1477 }
1478 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1479 break;
1480 }
1481 case TargetLowering::Promote: {
1482 // Change base type to a different vector type.
1483 MVT::ValueType OVT = Node->getValueType(0);
1484 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1485
1486 // Cast the two input vectors.
1487 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1488 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1489
1490 // Convert the shuffle mask to the right # elements.
1491 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1492 assert(Tmp3.Val && "Shuffle not legal?");
1493 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1494 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1495 break;
1496 }
1497 }
1498 break;
1499
1500 case ISD::EXTRACT_VECTOR_ELT:
1501 Tmp1 = Node->getOperand(0);
1502 Tmp2 = LegalizeOp(Node->getOperand(1));
1503 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1504 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1505 break;
1506
1507 case ISD::EXTRACT_SUBVECTOR:
1508 Tmp1 = Node->getOperand(0);
1509 Tmp2 = LegalizeOp(Node->getOperand(1));
1510 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1511 Result = ExpandEXTRACT_SUBVECTOR(Result);
1512 break;
1513
1514 case ISD::CALLSEQ_START: {
1515 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1516
1517 // Recursively Legalize all of the inputs of the call end that do not lead
1518 // to this call start. This ensures that any libcalls that need be inserted
1519 // are inserted *before* the CALLSEQ_START.
1520 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1521 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1522 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1523 NodesLeadingTo);
1524 }
1525
1526 // Now that we legalized all of the inputs (which may have inserted
1527 // libcalls) create the new CALLSEQ_START node.
1528 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1529
1530 // Merge in the last call, to ensure that this call start after the last
1531 // call ended.
1532 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1533 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1534 Tmp1 = LegalizeOp(Tmp1);
1535 }
1536
1537 // Do not try to legalize the target-specific arguments (#1+).
1538 if (Tmp1 != Node->getOperand(0)) {
1539 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1540 Ops[0] = Tmp1;
1541 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1542 }
1543
1544 // Remember that the CALLSEQ_START is legalized.
1545 AddLegalizedOperand(Op.getValue(0), Result);
1546 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1547 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1548
1549 // Now that the callseq_start and all of the non-call nodes above this call
1550 // sequence have been legalized, legalize the call itself. During this
1551 // process, no libcalls can/will be inserted, guaranteeing that no calls
1552 // can overlap.
1553 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1554 SDOperand InCallSEQ = LastCALLSEQ_END;
1555 // Note that we are selecting this call!
1556 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1557 IsLegalizingCall = true;
1558
1559 // Legalize the call, starting from the CALLSEQ_END.
1560 LegalizeOp(LastCALLSEQ_END);
1561 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1562 return Result;
1563 }
1564 case ISD::CALLSEQ_END:
1565 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1566 // will cause this node to be legalized as well as handling libcalls right.
1567 if (LastCALLSEQ_END.Val != Node) {
1568 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1569 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1570 assert(I != LegalizedNodes.end() &&
1571 "Legalizing the call start should have legalized this node!");
1572 return I->second;
1573 }
1574
1575 // Otherwise, the call start has been legalized and everything is going
1576 // according to plan. Just legalize ourselves normally here.
1577 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1578 // Do not try to legalize the target-specific arguments (#1+), except for
1579 // an optional flag input.
1580 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1581 if (Tmp1 != Node->getOperand(0)) {
1582 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1583 Ops[0] = Tmp1;
1584 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1585 }
1586 } else {
1587 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1588 if (Tmp1 != Node->getOperand(0) ||
1589 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1590 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1591 Ops[0] = Tmp1;
1592 Ops.back() = Tmp2;
1593 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1594 }
1595 }
1596 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1597 // This finishes up call legalization.
1598 IsLegalizingCall = false;
1599
1600 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1601 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1602 if (Node->getNumValues() == 2)
1603 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1604 return Result.getValue(Op.ResNo);
1605 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001606 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1608 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1609 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1611
1612 Tmp1 = Result.getValue(0);
1613 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001614 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 default: assert(0 && "This action is not supported yet!");
1616 case TargetLowering::Expand: {
1617 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1618 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1619 " not tell us which reg is the stack pointer!");
1620 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001621
1622 // Chain the dynamic stack allocation so that it doesn't modify the stack
1623 // pointer when other instructions are using the stack.
1624 Chain = DAG.getCALLSEQ_START(Chain,
1625 DAG.getConstant(0, TLI.getPointerTy()));
1626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001628 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1629 Chain = SP.getValue(1);
1630 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1631 unsigned StackAlign =
1632 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1633 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001634 SP = DAG.getNode(ISD::AND, VT, SP,
1635 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001636 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001637 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1638
1639 Tmp2 =
1640 DAG.getCALLSEQ_END(Chain,
1641 DAG.getConstant(0, TLI.getPointerTy()),
1642 DAG.getConstant(0, TLI.getPointerTy()),
1643 SDOperand());
1644
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645 Tmp1 = LegalizeOp(Tmp1);
1646 Tmp2 = LegalizeOp(Tmp2);
1647 break;
1648 }
1649 case TargetLowering::Custom:
1650 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1651 if (Tmp3.Val) {
1652 Tmp1 = LegalizeOp(Tmp3);
1653 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1654 }
1655 break;
1656 case TargetLowering::Legal:
1657 break;
1658 }
1659 // Since this op produce two values, make sure to remember that we
1660 // legalized both of them.
1661 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1662 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1663 return Op.ResNo ? Tmp2 : Tmp1;
1664 }
1665 case ISD::INLINEASM: {
1666 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1667 bool Changed = false;
1668 // Legalize all of the operands of the inline asm, in case they are nodes
1669 // that need to be expanded or something. Note we skip the asm string and
1670 // all of the TargetConstant flags.
1671 SDOperand Op = LegalizeOp(Ops[0]);
1672 Changed = Op != Ops[0];
1673 Ops[0] = Op;
1674
1675 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1676 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1677 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1678 for (++i; NumVals; ++i, --NumVals) {
1679 SDOperand Op = LegalizeOp(Ops[i]);
1680 if (Op != Ops[i]) {
1681 Changed = true;
1682 Ops[i] = Op;
1683 }
1684 }
1685 }
1686
1687 if (HasInFlag) {
1688 Op = LegalizeOp(Ops.back());
1689 Changed |= Op != Ops.back();
1690 Ops.back() = Op;
1691 }
1692
1693 if (Changed)
1694 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1695
1696 // INLINE asm returns a chain and flag, make sure to add both to the map.
1697 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1698 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1699 return Result.getValue(Op.ResNo);
1700 }
1701 case ISD::BR:
1702 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1703 // Ensure that libcalls are emitted before a branch.
1704 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1705 Tmp1 = LegalizeOp(Tmp1);
1706 LastCALLSEQ_END = DAG.getEntryNode();
1707
1708 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1709 break;
1710 case ISD::BRIND:
1711 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1712 // Ensure that libcalls are emitted before a branch.
1713 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1714 Tmp1 = LegalizeOp(Tmp1);
1715 LastCALLSEQ_END = DAG.getEntryNode();
1716
1717 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1718 default: assert(0 && "Indirect target must be legal type (pointer)!");
1719 case Legal:
1720 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1721 break;
1722 }
1723 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1724 break;
1725 case ISD::BR_JT:
1726 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1727 // Ensure that libcalls are emitted before a branch.
1728 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1729 Tmp1 = LegalizeOp(Tmp1);
1730 LastCALLSEQ_END = DAG.getEntryNode();
1731
1732 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1733 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1734
1735 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1736 default: assert(0 && "This action is not supported yet!");
1737 case TargetLowering::Legal: break;
1738 case TargetLowering::Custom:
1739 Tmp1 = TLI.LowerOperation(Result, DAG);
1740 if (Tmp1.Val) Result = Tmp1;
1741 break;
1742 case TargetLowering::Expand: {
1743 SDOperand Chain = Result.getOperand(0);
1744 SDOperand Table = Result.getOperand(1);
1745 SDOperand Index = Result.getOperand(2);
1746
1747 MVT::ValueType PTy = TLI.getPointerTy();
1748 MachineFunction &MF = DAG.getMachineFunction();
1749 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1750 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1751 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1752
1753 SDOperand LD;
1754 switch (EntrySize) {
1755 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001756 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001757 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001758 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001759 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001760 }
1761
Evan Cheng6fb06762007-11-09 01:32:10 +00001762 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1764 // For PIC, the sequence is:
1765 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001766 // RelocBase can be JumpTable, GOT or some sort of global base.
1767 if (PTy != MVT::i32)
1768 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1769 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1770 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001772 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 }
1774 }
1775 break;
1776 case ISD::BRCOND:
1777 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1778 // Ensure that libcalls are emitted before a return.
1779 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1780 Tmp1 = LegalizeOp(Tmp1);
1781 LastCALLSEQ_END = DAG.getEntryNode();
1782
1783 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1784 case Expand: assert(0 && "It's impossible to expand bools");
1785 case Legal:
1786 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1787 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001788 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1790
1791 // The top bits of the promoted condition are not necessarily zero, ensure
1792 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001793 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001795 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1797 break;
1798 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001799 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001800
1801 // Basic block destination (Op#2) is always legal.
1802 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1803
1804 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1805 default: assert(0 && "This action is not supported yet!");
1806 case TargetLowering::Legal: break;
1807 case TargetLowering::Custom:
1808 Tmp1 = TLI.LowerOperation(Result, DAG);
1809 if (Tmp1.Val) Result = Tmp1;
1810 break;
1811 case TargetLowering::Expand:
1812 // Expand brcond's setcc into its constituent parts and create a BR_CC
1813 // Node.
1814 if (Tmp2.getOpcode() == ISD::SETCC) {
1815 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1816 Tmp2.getOperand(0), Tmp2.getOperand(1),
1817 Node->getOperand(2));
1818 } else {
1819 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1820 DAG.getCondCode(ISD::SETNE), Tmp2,
1821 DAG.getConstant(0, Tmp2.getValueType()),
1822 Node->getOperand(2));
1823 }
1824 break;
1825 }
1826 break;
1827 case ISD::BR_CC:
1828 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1829 // Ensure that libcalls are emitted before a branch.
1830 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1831 Tmp1 = LegalizeOp(Tmp1);
1832 Tmp2 = Node->getOperand(2); // LHS
1833 Tmp3 = Node->getOperand(3); // RHS
1834 Tmp4 = Node->getOperand(1); // CC
1835
1836 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1837 LastCALLSEQ_END = DAG.getEntryNode();
1838
1839 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1840 // the LHS is a legal SETCC itself. In this case, we need to compare
1841 // the result against zero to select between true and false values.
1842 if (Tmp3.Val == 0) {
1843 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1844 Tmp4 = DAG.getCondCode(ISD::SETNE);
1845 }
1846
1847 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1848 Node->getOperand(4));
1849
1850 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1851 default: assert(0 && "Unexpected action for BR_CC!");
1852 case TargetLowering::Legal: break;
1853 case TargetLowering::Custom:
1854 Tmp4 = TLI.LowerOperation(Result, DAG);
1855 if (Tmp4.Val) Result = Tmp4;
1856 break;
1857 }
1858 break;
1859 case ISD::LOAD: {
1860 LoadSDNode *LD = cast<LoadSDNode>(Node);
1861 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1862 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1863
1864 ISD::LoadExtType ExtType = LD->getExtensionType();
1865 if (ExtType == ISD::NON_EXTLOAD) {
1866 MVT::ValueType VT = Node->getValueType(0);
1867 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1868 Tmp3 = Result.getValue(0);
1869 Tmp4 = Result.getValue(1);
1870
1871 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1872 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001873 case TargetLowering::Legal:
1874 // If this is an unaligned load and the target doesn't support it,
1875 // expand it.
1876 if (!TLI.allowsUnalignedMemoryAccesses()) {
1877 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001878 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001879 if (LD->getAlignment() < ABIAlignment){
1880 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1881 TLI);
1882 Tmp3 = Result.getOperand(0);
1883 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001884 Tmp3 = LegalizeOp(Tmp3);
1885 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001886 }
1887 }
1888 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001889 case TargetLowering::Custom:
1890 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1891 if (Tmp1.Val) {
1892 Tmp3 = LegalizeOp(Tmp1);
1893 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1894 }
1895 break;
1896 case TargetLowering::Promote: {
1897 // Only promote a load of vector type to another.
1898 assert(MVT::isVector(VT) && "Cannot promote this load!");
1899 // Change base type to a different vector type.
1900 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1901
1902 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1903 LD->getSrcValueOffset(),
1904 LD->isVolatile(), LD->getAlignment());
1905 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1906 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1907 break;
1908 }
1909 }
1910 // Since loads produce two values, make sure to remember that we
1911 // legalized both of them.
1912 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1913 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1914 return Op.ResNo ? Tmp4 : Tmp3;
1915 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001916 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands082524c2008-01-23 20:39:46 +00001917 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1918 int SVOffset = LD->getSrcValueOffset();
1919 unsigned Alignment = LD->getAlignment();
1920 bool isVolatile = LD->isVolatile();
1921
1922 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1923 // Some targets pretend to have an i1 loading operation, and actually
1924 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1925 // bits are guaranteed to be zero; it helps the optimizers understand
1926 // that these bits are zero. It is also useful for EXTLOAD, since it
1927 // tells the optimizers that those bits are undefined. It would be
1928 // nice to have an effective generic way of getting these benefits...
1929 // Until such a way is found, don't insist on promoting i1 here.
1930 (SrcVT != MVT::i1 ||
1931 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1932 // Promote to a byte-sized load if not loading an integral number of
1933 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1934 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1935 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1936 SDOperand Ch;
1937
1938 // The extra bits are guaranteed to be zero, since we stored them that
1939 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1940
1941 ISD::LoadExtType NewExtType =
1942 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1943
1944 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1945 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1946 NVT, isVolatile, Alignment);
1947
1948 Ch = Result.getValue(1); // The chain.
1949
1950 if (ExtType == ISD::SEXTLOAD)
1951 // Having the top bits zero doesn't help when sign extending.
1952 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1953 Result, DAG.getValueType(SrcVT));
1954 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1955 // All the top bits are guaranteed to be zero - inform the optimizers.
1956 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1957 DAG.getValueType(SrcVT));
1958
1959 Tmp1 = LegalizeOp(Result);
1960 Tmp2 = LegalizeOp(Ch);
1961 } else if (SrcWidth & (SrcWidth - 1)) {
1962 // If not loading a power-of-2 number of bits, expand as two loads.
1963 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1964 "Unsupported extload!");
1965 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1966 assert(RoundWidth < SrcWidth);
1967 unsigned ExtraWidth = SrcWidth - RoundWidth;
1968 assert(ExtraWidth < RoundWidth);
1969 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1970 "Load size not an integral number of bytes!");
1971 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1972 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1973 SDOperand Lo, Hi, Ch;
1974 unsigned IncrementSize;
1975
1976 if (TLI.isLittleEndian()) {
1977 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1978 // Load the bottom RoundWidth bits.
1979 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1980 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1981 Alignment);
1982
1983 // Load the remaining ExtraWidth bits.
1984 IncrementSize = RoundWidth / 8;
1985 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1986 DAG.getIntPtrConstant(IncrementSize));
1987 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1988 LD->getSrcValue(), SVOffset + IncrementSize,
1989 ExtraVT, isVolatile,
1990 MinAlign(Alignment, IncrementSize));
1991
1992 // Build a factor node to remember that this load is independent of the
1993 // other one.
1994 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1995 Hi.getValue(1));
1996
1997 // Move the top bits to the right place.
1998 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1999 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2000
2001 // Join the hi and lo parts.
2002 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002003 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002004 // Big endian - avoid unaligned loads.
2005 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2006 // Load the top RoundWidth bits.
2007 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2008 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2009 Alignment);
2010
2011 // Load the remaining ExtraWidth bits.
2012 IncrementSize = RoundWidth / 8;
2013 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2014 DAG.getIntPtrConstant(IncrementSize));
2015 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2016 LD->getSrcValue(), SVOffset + IncrementSize,
2017 ExtraVT, isVolatile,
2018 MinAlign(Alignment, IncrementSize));
2019
2020 // Build a factor node to remember that this load is independent of the
2021 // other one.
2022 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2023 Hi.getValue(1));
2024
2025 // Move the top bits to the right place.
2026 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2027 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2028
2029 // Join the hi and lo parts.
2030 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2031 }
2032
2033 Tmp1 = LegalizeOp(Result);
2034 Tmp2 = LegalizeOp(Ch);
2035 } else {
2036 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2037 default: assert(0 && "This action is not supported yet!");
2038 case TargetLowering::Custom:
2039 isCustom = true;
2040 // FALLTHROUGH
2041 case TargetLowering::Legal:
2042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2043 Tmp1 = Result.getValue(0);
2044 Tmp2 = Result.getValue(1);
2045
2046 if (isCustom) {
2047 Tmp3 = TLI.LowerOperation(Result, DAG);
2048 if (Tmp3.Val) {
2049 Tmp1 = LegalizeOp(Tmp3);
2050 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2051 }
2052 } else {
2053 // If this is an unaligned load and the target doesn't support it,
2054 // expand it.
2055 if (!TLI.allowsUnalignedMemoryAccesses()) {
2056 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002057 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands082524c2008-01-23 20:39:46 +00002058 if (LD->getAlignment() < ABIAlignment){
2059 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2060 TLI);
2061 Tmp1 = Result.getOperand(0);
2062 Tmp2 = Result.getOperand(1);
2063 Tmp1 = LegalizeOp(Tmp1);
2064 Tmp2 = LegalizeOp(Tmp2);
2065 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002066 }
2067 }
Duncan Sands082524c2008-01-23 20:39:46 +00002068 break;
2069 case TargetLowering::Expand:
2070 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2071 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2072 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2073 LD->getSrcValueOffset(),
2074 LD->isVolatile(), LD->getAlignment());
2075 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2076 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2077 Tmp2 = LegalizeOp(Load.getValue(1));
2078 break;
2079 }
2080 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2081 // Turn the unsupported load into an EXTLOAD followed by an explicit
2082 // zero/sign extend inreg.
2083 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2084 Tmp1, Tmp2, LD->getSrcValue(),
2085 LD->getSrcValueOffset(), SrcVT,
2086 LD->isVolatile(), LD->getAlignment());
2087 SDOperand ValRes;
2088 if (ExtType == ISD::SEXTLOAD)
2089 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2090 Result, DAG.getValueType(SrcVT));
2091 else
2092 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2093 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2094 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095 break;
2096 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 }
Duncan Sands082524c2008-01-23 20:39:46 +00002098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 // Since loads produce two values, make sure to remember that we legalized
2100 // both of them.
2101 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2102 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2103 return Op.ResNo ? Tmp2 : Tmp1;
2104 }
2105 }
2106 case ISD::EXTRACT_ELEMENT: {
2107 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2108 switch (getTypeAction(OpTy)) {
2109 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2110 case Legal:
2111 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2112 // 1 -> Hi
2113 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2114 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2115 TLI.getShiftAmountTy()));
2116 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2117 } else {
2118 // 0 -> Lo
2119 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2120 Node->getOperand(0));
2121 }
2122 break;
2123 case Expand:
2124 // Get both the low and high parts.
2125 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2126 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2127 Result = Tmp2; // 1 -> Hi
2128 else
2129 Result = Tmp1; // 0 -> Lo
2130 break;
2131 }
2132 break;
2133 }
2134
2135 case ISD::CopyToReg:
2136 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2137
2138 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2139 "Register type must be legal!");
2140 // Legalize the incoming value (must be a legal type).
2141 Tmp2 = LegalizeOp(Node->getOperand(2));
2142 if (Node->getNumValues() == 1) {
2143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2144 } else {
2145 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2146 if (Node->getNumOperands() == 4) {
2147 Tmp3 = LegalizeOp(Node->getOperand(3));
2148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2149 Tmp3);
2150 } else {
2151 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2152 }
2153
2154 // Since this produces two values, make sure to remember that we legalized
2155 // both of them.
2156 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2157 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2158 return Result;
2159 }
2160 break;
2161
2162 case ISD::RET:
2163 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2164
2165 // Ensure that libcalls are emitted before a return.
2166 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2167 Tmp1 = LegalizeOp(Tmp1);
2168 LastCALLSEQ_END = DAG.getEntryNode();
2169
2170 switch (Node->getNumOperands()) {
2171 case 3: // ret val
2172 Tmp2 = Node->getOperand(1);
2173 Tmp3 = Node->getOperand(2); // Signness
2174 switch (getTypeAction(Tmp2.getValueType())) {
2175 case Legal:
2176 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2177 break;
2178 case Expand:
2179 if (!MVT::isVector(Tmp2.getValueType())) {
2180 SDOperand Lo, Hi;
2181 ExpandOp(Tmp2, Lo, Hi);
2182
2183 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002184 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 std::swap(Lo, Hi);
2186
2187 if (Hi.Val)
2188 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2189 else
2190 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2191 Result = LegalizeOp(Result);
2192 } else {
2193 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002194 int InIx = Tmp2.ResNo;
2195 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2196 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002197
2198 // Figure out if there is a simple type corresponding to this Vector
2199 // type. If so, convert to the vector type.
2200 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2201 if (TLI.isTypeLegal(TVT)) {
2202 // Turn this into a return of the vector type.
2203 Tmp2 = LegalizeOp(Tmp2);
2204 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2205 } else if (NumElems == 1) {
2206 // Turn this into a return of the scalar type.
2207 Tmp2 = ScalarizeVectorOp(Tmp2);
2208 Tmp2 = LegalizeOp(Tmp2);
2209 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2210
2211 // FIXME: Returns of gcc generic vectors smaller than a legal type
2212 // should be returned in integer registers!
2213
2214 // The scalarized value type may not be legal, e.g. it might require
2215 // promotion or expansion. Relegalize the return.
2216 Result = LegalizeOp(Result);
2217 } else {
2218 // FIXME: Returns of gcc generic vectors larger than a legal vector
2219 // type should be returned by reference!
2220 SDOperand Lo, Hi;
2221 SplitVectorOp(Tmp2, Lo, Hi);
2222 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2223 Result = LegalizeOp(Result);
2224 }
2225 }
2226 break;
2227 case Promote:
2228 Tmp2 = PromoteOp(Node->getOperand(1));
2229 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2230 Result = LegalizeOp(Result);
2231 break;
2232 }
2233 break;
2234 case 1: // ret void
2235 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2236 break;
2237 default: { // ret <values>
2238 SmallVector<SDOperand, 8> NewValues;
2239 NewValues.push_back(Tmp1);
2240 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2241 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2242 case Legal:
2243 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2244 NewValues.push_back(Node->getOperand(i+1));
2245 break;
2246 case Expand: {
2247 SDOperand Lo, Hi;
2248 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
2249 "FIXME: TODO: implement returning non-legal vector types!");
2250 ExpandOp(Node->getOperand(i), Lo, Hi);
2251 NewValues.push_back(Lo);
2252 NewValues.push_back(Node->getOperand(i+1));
2253 if (Hi.Val) {
2254 NewValues.push_back(Hi);
2255 NewValues.push_back(Node->getOperand(i+1));
2256 }
2257 break;
2258 }
2259 case Promote:
2260 assert(0 && "Can't promote multiple return value yet!");
2261 }
2262
2263 if (NewValues.size() == Node->getNumOperands())
2264 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2265 else
2266 Result = DAG.getNode(ISD::RET, MVT::Other,
2267 &NewValues[0], NewValues.size());
2268 break;
2269 }
2270 }
2271
2272 if (Result.getOpcode() == ISD::RET) {
2273 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2274 default: assert(0 && "This action is not supported yet!");
2275 case TargetLowering::Legal: break;
2276 case TargetLowering::Custom:
2277 Tmp1 = TLI.LowerOperation(Result, DAG);
2278 if (Tmp1.Val) Result = Tmp1;
2279 break;
2280 }
2281 }
2282 break;
2283 case ISD::STORE: {
2284 StoreSDNode *ST = cast<StoreSDNode>(Node);
2285 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2286 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2287 int SVOffset = ST->getSrcValueOffset();
2288 unsigned Alignment = ST->getAlignment();
2289 bool isVolatile = ST->isVolatile();
2290
2291 if (!ST->isTruncatingStore()) {
2292 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2293 // FIXME: We shouldn't do this for TargetConstantFP's.
2294 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2295 // to phase ordering between legalized code and the dag combiner. This
2296 // probably means that we need to integrate dag combiner and legalizer
2297 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002298 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002299 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002300 if (CFP->getValueType(0) == MVT::f32 &&
2301 getTypeAction(MVT::i32) == Legal) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00002302 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2303 convertToAPInt().getZExtValue(),
Dale Johannesen1616e902007-09-11 18:32:33 +00002304 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002305 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2306 SVOffset, isVolatile, Alignment);
2307 break;
2308 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002309 // If this target supports 64-bit registers, do a single 64-bit store.
2310 if (getTypeAction(MVT::i64) == Legal) {
2311 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2312 getZExtValue(), MVT::i64);
2313 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2314 SVOffset, isVolatile, Alignment);
2315 break;
2316 } else if (getTypeAction(MVT::i32) == Legal) {
2317 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2318 // stores. If the target supports neither 32- nor 64-bits, this
2319 // xform is certainly not worth it.
2320 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2321 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2322 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002323 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002324
2325 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2326 SVOffset, isVolatile, Alignment);
2327 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002328 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002329 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002330 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002331
2332 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2333 break;
2334 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002336 }
2337
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002338 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339 case Legal: {
2340 Tmp3 = LegalizeOp(ST->getValue());
2341 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2342 ST->getOffset());
2343
2344 MVT::ValueType VT = Tmp3.getValueType();
2345 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2346 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002347 case TargetLowering::Legal:
2348 // If this is an unaligned store and the target doesn't support it,
2349 // expand it.
2350 if (!TLI.allowsUnalignedMemoryAccesses()) {
2351 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002352 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002353 if (ST->getAlignment() < ABIAlignment)
2354 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2355 TLI);
2356 }
2357 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002358 case TargetLowering::Custom:
2359 Tmp1 = TLI.LowerOperation(Result, DAG);
2360 if (Tmp1.Val) Result = Tmp1;
2361 break;
2362 case TargetLowering::Promote:
2363 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2364 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2365 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2366 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2367 ST->getSrcValue(), SVOffset, isVolatile,
2368 Alignment);
2369 break;
2370 }
2371 break;
2372 }
2373 case Promote:
2374 // Truncate the value and store the result.
2375 Tmp3 = PromoteOp(ST->getValue());
2376 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002377 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 isVolatile, Alignment);
2379 break;
2380
2381 case Expand:
2382 unsigned IncrementSize = 0;
2383 SDOperand Lo, Hi;
2384
2385 // If this is a vector type, then we have to calculate the increment as
2386 // the product of the element size in bytes, and the number of elements
2387 // in the high half of the vector.
2388 if (MVT::isVector(ST->getValue().getValueType())) {
2389 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002390 int InIx = ST->getValue().ResNo;
Chris Lattner5872a362008-01-17 07:00:52 +00002391 MVT::ValueType InVT = InVal->getValueType(InIx);
2392 unsigned NumElems = MVT::getVectorNumElements(InVT);
2393 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002394
2395 // Figure out if there is a simple type corresponding to this Vector
2396 // type. If so, convert to the vector type.
2397 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2398 if (TLI.isTypeLegal(TVT)) {
2399 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002400 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002401 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2402 SVOffset, isVolatile, Alignment);
2403 Result = LegalizeOp(Result);
2404 break;
2405 } else if (NumElems == 1) {
2406 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002407 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002408 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2409 SVOffset, isVolatile, Alignment);
2410 // The scalarized value type may not be legal, e.g. it might require
2411 // promotion or expansion. Relegalize the scalar store.
2412 Result = LegalizeOp(Result);
2413 break;
2414 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002415 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00002416 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2417 MVT::getSizeInBits(EVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002418 }
2419 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002420 ExpandOp(ST->getValue(), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2422
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002423 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 std::swap(Lo, Hi);
2425 }
2426
2427 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2428 SVOffset, isVolatile, Alignment);
2429
2430 if (Hi.Val == NULL) {
2431 // Must be int <-> float one-to-one expansion.
2432 Result = Lo;
2433 break;
2434 }
2435
2436 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002437 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002438 assert(isTypeLegal(Tmp2.getValueType()) &&
2439 "Pointers must be legal!");
2440 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002441 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2443 SVOffset, isVolatile, Alignment);
2444 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2445 break;
2446 }
2447 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002448 switch (getTypeAction(ST->getValue().getValueType())) {
2449 case Legal:
2450 Tmp3 = LegalizeOp(ST->getValue());
2451 break;
2452 case Promote:
2453 // We can promote the value, the truncstore will still take care of it.
2454 Tmp3 = PromoteOp(ST->getValue());
2455 break;
2456 case Expand:
2457 // Just store the low part. This may become a non-trunc store, so make
2458 // sure to use getTruncStore, not UpdateNodeOperands below.
2459 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2460 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2461 SVOffset, MVT::i8, isVolatile, Alignment);
2462 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002464 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands40676662008-01-22 07:17:34 +00002465 unsigned StWidth = MVT::getSizeInBits(StVT);
2466
2467 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2468 // Promote to a byte-sized store with upper bits zero if not
2469 // storing an integral number of bytes. For example, promote
2470 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2471 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2472 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2473 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2474 SVOffset, NVT, isVolatile, Alignment);
2475 } else if (StWidth & (StWidth - 1)) {
2476 // If not storing a power-of-2 number of bits, expand as two stores.
2477 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2478 "Unsupported truncstore!");
2479 unsigned RoundWidth = 1 << Log2_32(StWidth);
2480 assert(RoundWidth < StWidth);
2481 unsigned ExtraWidth = StWidth - RoundWidth;
2482 assert(ExtraWidth < RoundWidth);
2483 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2484 "Store size not an integral number of bytes!");
2485 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2486 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2487 SDOperand Lo, Hi;
2488 unsigned IncrementSize;
2489
2490 if (TLI.isLittleEndian()) {
2491 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2492 // Store the bottom RoundWidth bits.
2493 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2494 SVOffset, RoundVT,
2495 isVolatile, Alignment);
2496
2497 // Store the remaining ExtraWidth bits.
2498 IncrementSize = RoundWidth / 8;
2499 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2500 DAG.getIntPtrConstant(IncrementSize));
2501 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2502 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2503 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2504 SVOffset + IncrementSize, ExtraVT, isVolatile,
2505 MinAlign(Alignment, IncrementSize));
2506 } else {
2507 // Big endian - avoid unaligned stores.
2508 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2509 // Store the top RoundWidth bits.
2510 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2511 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2512 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2513 RoundVT, isVolatile, Alignment);
2514
2515 // Store the remaining ExtraWidth bits.
2516 IncrementSize = RoundWidth / 8;
2517 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2518 DAG.getIntPtrConstant(IncrementSize));
2519 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2520 SVOffset + IncrementSize, ExtraVT, isVolatile,
2521 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002522 }
Duncan Sands40676662008-01-22 07:17:34 +00002523
2524 // The order of the stores doesn't matter.
2525 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2526 } else {
2527 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2528 Tmp2 != ST->getBasePtr())
2529 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2530 ST->getOffset());
2531
2532 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2533 default: assert(0 && "This action is not supported yet!");
2534 case TargetLowering::Legal:
2535 // If this is an unaligned store and the target doesn't support it,
2536 // expand it.
2537 if (!TLI.allowsUnalignedMemoryAccesses()) {
2538 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002539 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands40676662008-01-22 07:17:34 +00002540 if (ST->getAlignment() < ABIAlignment)
2541 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2542 TLI);
2543 }
2544 break;
2545 case TargetLowering::Custom:
2546 Result = TLI.LowerOperation(Result, DAG);
2547 break;
2548 case Expand:
2549 // TRUNCSTORE:i16 i32 -> STORE i16
2550 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2551 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2552 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2553 isVolatile, Alignment);
2554 break;
2555 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 }
2557 }
2558 break;
2559 }
2560 case ISD::PCMARKER:
2561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2563 break;
2564 case ISD::STACKSAVE:
2565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2566 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2567 Tmp1 = Result.getValue(0);
2568 Tmp2 = Result.getValue(1);
2569
2570 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2571 default: assert(0 && "This action is not supported yet!");
2572 case TargetLowering::Legal: break;
2573 case TargetLowering::Custom:
2574 Tmp3 = TLI.LowerOperation(Result, DAG);
2575 if (Tmp3.Val) {
2576 Tmp1 = LegalizeOp(Tmp3);
2577 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2578 }
2579 break;
2580 case TargetLowering::Expand:
2581 // Expand to CopyFromReg if the target set
2582 // StackPointerRegisterToSaveRestore.
2583 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2584 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2585 Node->getValueType(0));
2586 Tmp2 = Tmp1.getValue(1);
2587 } else {
2588 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2589 Tmp2 = Node->getOperand(0);
2590 }
2591 break;
2592 }
2593
2594 // Since stacksave produce two values, make sure to remember that we
2595 // legalized both of them.
2596 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2597 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2598 return Op.ResNo ? Tmp2 : Tmp1;
2599
2600 case ISD::STACKRESTORE:
2601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2602 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2603 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2604
2605 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2606 default: assert(0 && "This action is not supported yet!");
2607 case TargetLowering::Legal: break;
2608 case TargetLowering::Custom:
2609 Tmp1 = TLI.LowerOperation(Result, DAG);
2610 if (Tmp1.Val) Result = Tmp1;
2611 break;
2612 case TargetLowering::Expand:
2613 // Expand to CopyToReg if the target set
2614 // StackPointerRegisterToSaveRestore.
2615 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2616 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2617 } else {
2618 Result = Tmp1;
2619 }
2620 break;
2621 }
2622 break;
2623
2624 case ISD::READCYCLECOUNTER:
2625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2626 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2627 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2628 Node->getValueType(0))) {
2629 default: assert(0 && "This action is not supported yet!");
2630 case TargetLowering::Legal:
2631 Tmp1 = Result.getValue(0);
2632 Tmp2 = Result.getValue(1);
2633 break;
2634 case TargetLowering::Custom:
2635 Result = TLI.LowerOperation(Result, DAG);
2636 Tmp1 = LegalizeOp(Result.getValue(0));
2637 Tmp2 = LegalizeOp(Result.getValue(1));
2638 break;
2639 }
2640
2641 // Since rdcc produce two values, make sure to remember that we legalized
2642 // both of them.
2643 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2644 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2645 return Result;
2646
2647 case ISD::SELECT:
2648 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2649 case Expand: assert(0 && "It's impossible to expand bools");
2650 case Legal:
2651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2652 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002653 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2655 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002656 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002658 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2660 break;
2661 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002662 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2664 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2665
2666 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2667
2668 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2669 default: assert(0 && "This action is not supported yet!");
2670 case TargetLowering::Legal: break;
2671 case TargetLowering::Custom: {
2672 Tmp1 = TLI.LowerOperation(Result, DAG);
2673 if (Tmp1.Val) Result = Tmp1;
2674 break;
2675 }
2676 case TargetLowering::Expand:
2677 if (Tmp1.getOpcode() == ISD::SETCC) {
2678 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2679 Tmp2, Tmp3,
2680 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2681 } else {
2682 Result = DAG.getSelectCC(Tmp1,
2683 DAG.getConstant(0, Tmp1.getValueType()),
2684 Tmp2, Tmp3, ISD::SETNE);
2685 }
2686 break;
2687 case TargetLowering::Promote: {
2688 MVT::ValueType NVT =
2689 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2690 unsigned ExtOp, TruncOp;
2691 if (MVT::isVector(Tmp2.getValueType())) {
2692 ExtOp = ISD::BIT_CONVERT;
2693 TruncOp = ISD::BIT_CONVERT;
2694 } else if (MVT::isInteger(Tmp2.getValueType())) {
2695 ExtOp = ISD::ANY_EXTEND;
2696 TruncOp = ISD::TRUNCATE;
2697 } else {
2698 ExtOp = ISD::FP_EXTEND;
2699 TruncOp = ISD::FP_ROUND;
2700 }
2701 // Promote each of the values to the new type.
2702 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2703 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2704 // Perform the larger operation, then round down.
2705 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002706 if (TruncOp != ISD::FP_ROUND)
2707 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2708 else
2709 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2710 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 break;
2712 }
2713 }
2714 break;
2715 case ISD::SELECT_CC: {
2716 Tmp1 = Node->getOperand(0); // LHS
2717 Tmp2 = Node->getOperand(1); // RHS
2718 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2719 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2720 SDOperand CC = Node->getOperand(4);
2721
2722 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2723
2724 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2725 // the LHS is a legal SETCC itself. In this case, we need to compare
2726 // the result against zero to select between true and false values.
2727 if (Tmp2.Val == 0) {
2728 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2729 CC = DAG.getCondCode(ISD::SETNE);
2730 }
2731 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2732
2733 // Everything is legal, see if we should expand this op or something.
2734 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2735 default: assert(0 && "This action is not supported yet!");
2736 case TargetLowering::Legal: break;
2737 case TargetLowering::Custom:
2738 Tmp1 = TLI.LowerOperation(Result, DAG);
2739 if (Tmp1.Val) Result = Tmp1;
2740 break;
2741 }
2742 break;
2743 }
2744 case ISD::SETCC:
2745 Tmp1 = Node->getOperand(0);
2746 Tmp2 = Node->getOperand(1);
2747 Tmp3 = Node->getOperand(2);
2748 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2749
2750 // If we had to Expand the SetCC operands into a SELECT node, then it may
2751 // not always be possible to return a true LHS & RHS. In this case, just
2752 // return the value we legalized, returned in the LHS
2753 if (Tmp2.Val == 0) {
2754 Result = Tmp1;
2755 break;
2756 }
2757
2758 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2759 default: assert(0 && "Cannot handle this action for SETCC yet!");
2760 case TargetLowering::Custom:
2761 isCustom = true;
2762 // FALLTHROUGH.
2763 case TargetLowering::Legal:
2764 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2765 if (isCustom) {
2766 Tmp4 = TLI.LowerOperation(Result, DAG);
2767 if (Tmp4.Val) Result = Tmp4;
2768 }
2769 break;
2770 case TargetLowering::Promote: {
2771 // First step, figure out the appropriate operation to use.
2772 // Allow SETCC to not be supported for all legal data types
2773 // Mostly this targets FP
2774 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2775 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2776
2777 // Scan for the appropriate larger type to use.
2778 while (1) {
2779 NewInTy = (MVT::ValueType)(NewInTy+1);
2780
2781 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2782 "Fell off of the edge of the integer world");
2783 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2784 "Fell off of the edge of the floating point world");
2785
2786 // If the target supports SETCC of this type, use it.
2787 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2788 break;
2789 }
2790 if (MVT::isInteger(NewInTy))
2791 assert(0 && "Cannot promote Legal Integer SETCC yet");
2792 else {
2793 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2794 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2795 }
2796 Tmp1 = LegalizeOp(Tmp1);
2797 Tmp2 = LegalizeOp(Tmp2);
2798 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2799 Result = LegalizeOp(Result);
2800 break;
2801 }
2802 case TargetLowering::Expand:
2803 // Expand a setcc node into a select_cc of the same condition, lhs, and
2804 // rhs that selects between const 1 (true) and const 0 (false).
2805 MVT::ValueType VT = Node->getValueType(0);
2806 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2807 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2808 Tmp3);
2809 break;
2810 }
2811 break;
2812 case ISD::MEMSET:
2813 case ISD::MEMCPY:
2814 case ISD::MEMMOVE: {
2815 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2816 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2817
2818 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2819 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2820 case Expand: assert(0 && "Cannot expand a byte!");
2821 case Legal:
2822 Tmp3 = LegalizeOp(Node->getOperand(2));
2823 break;
2824 case Promote:
2825 Tmp3 = PromoteOp(Node->getOperand(2));
2826 break;
2827 }
2828 } else {
2829 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2830 }
2831
2832 SDOperand Tmp4;
2833 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2834 case Expand: {
2835 // Length is too big, just take the lo-part of the length.
2836 SDOperand HiPart;
2837 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2838 break;
2839 }
2840 case Legal:
2841 Tmp4 = LegalizeOp(Node->getOperand(3));
2842 break;
2843 case Promote:
2844 Tmp4 = PromoteOp(Node->getOperand(3));
2845 break;
2846 }
2847
2848 SDOperand Tmp5;
2849 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2850 case Expand: assert(0 && "Cannot expand this yet!");
2851 case Legal:
2852 Tmp5 = LegalizeOp(Node->getOperand(4));
2853 break;
2854 case Promote:
2855 Tmp5 = PromoteOp(Node->getOperand(4));
2856 break;
2857 }
2858
Rafael Espindola80825902007-10-19 10:41:11 +00002859 SDOperand Tmp6;
2860 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2861 case Expand: assert(0 && "Cannot expand this yet!");
2862 case Legal:
2863 Tmp6 = LegalizeOp(Node->getOperand(5));
2864 break;
2865 case Promote:
2866 Tmp6 = PromoteOp(Node->getOperand(5));
2867 break;
2868 }
2869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2871 default: assert(0 && "This action not implemented for this operation!");
2872 case TargetLowering::Custom:
2873 isCustom = true;
2874 // FALLTHROUGH
Rafael Espindola80825902007-10-19 10:41:11 +00002875 case TargetLowering::Legal: {
2876 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2877 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 if (isCustom) {
2879 Tmp1 = TLI.LowerOperation(Result, DAG);
2880 if (Tmp1.Val) Result = Tmp1;
2881 }
2882 break;
Rafael Espindola80825902007-10-19 10:41:11 +00002883 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884 case TargetLowering::Expand: {
2885 // Otherwise, the target does not support this operation. Lower the
2886 // operation to an explicit libcall as appropriate.
2887 MVT::ValueType IntPtr = TLI.getPointerTy();
2888 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2889 TargetLowering::ArgListTy Args;
2890 TargetLowering::ArgListEntry Entry;
2891
2892 const char *FnName = 0;
2893 if (Node->getOpcode() == ISD::MEMSET) {
2894 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2895 Args.push_back(Entry);
2896 // Extend the (previously legalized) ubyte argument to be an int value
2897 // for the call.
2898 if (Tmp3.getValueType() > MVT::i32)
2899 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2900 else
2901 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2902 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2903 Args.push_back(Entry);
2904 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2905 Args.push_back(Entry);
2906
2907 FnName = "memset";
2908 } else if (Node->getOpcode() == ISD::MEMCPY ||
2909 Node->getOpcode() == ISD::MEMMOVE) {
2910 Entry.Ty = IntPtrTy;
2911 Entry.Node = Tmp2; Args.push_back(Entry);
2912 Entry.Node = Tmp3; Args.push_back(Entry);
2913 Entry.Node = Tmp4; Args.push_back(Entry);
2914 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2915 } else {
2916 assert(0 && "Unknown op!");
2917 }
2918
2919 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00002920 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2921 false, false, false, CallingConv::C, false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2923 Result = CallResult.second;
2924 break;
2925 }
2926 }
2927 break;
2928 }
2929
2930 case ISD::SHL_PARTS:
2931 case ISD::SRA_PARTS:
2932 case ISD::SRL_PARTS: {
2933 SmallVector<SDOperand, 8> Ops;
2934 bool Changed = false;
2935 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2936 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2937 Changed |= Ops.back() != Node->getOperand(i);
2938 }
2939 if (Changed)
2940 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2941
2942 switch (TLI.getOperationAction(Node->getOpcode(),
2943 Node->getValueType(0))) {
2944 default: assert(0 && "This action is not supported yet!");
2945 case TargetLowering::Legal: break;
2946 case TargetLowering::Custom:
2947 Tmp1 = TLI.LowerOperation(Result, DAG);
2948 if (Tmp1.Val) {
2949 SDOperand Tmp2, RetVal(0, 0);
2950 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2951 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2952 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2953 if (i == Op.ResNo)
2954 RetVal = Tmp2;
2955 }
2956 assert(RetVal.Val && "Illegal result number");
2957 return RetVal;
2958 }
2959 break;
2960 }
2961
2962 // Since these produce multiple values, make sure to remember that we
2963 // legalized all of them.
2964 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2965 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2966 return Result.getValue(Op.ResNo);
2967 }
2968
2969 // Binary operators
2970 case ISD::ADD:
2971 case ISD::SUB:
2972 case ISD::MUL:
2973 case ISD::MULHS:
2974 case ISD::MULHU:
2975 case ISD::UDIV:
2976 case ISD::SDIV:
2977 case ISD::AND:
2978 case ISD::OR:
2979 case ISD::XOR:
2980 case ISD::SHL:
2981 case ISD::SRL:
2982 case ISD::SRA:
2983 case ISD::FADD:
2984 case ISD::FSUB:
2985 case ISD::FMUL:
2986 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002987 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002988 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2989 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2990 case Expand: assert(0 && "Not possible");
2991 case Legal:
2992 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2993 break;
2994 case Promote:
2995 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2996 break;
2997 }
2998
2999 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3000
3001 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3002 default: assert(0 && "BinOp legalize operation not supported");
3003 case TargetLowering::Legal: break;
3004 case TargetLowering::Custom:
3005 Tmp1 = TLI.LowerOperation(Result, DAG);
3006 if (Tmp1.Val) Result = Tmp1;
3007 break;
3008 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00003009 MVT::ValueType VT = Op.getValueType();
3010
3011 // See if multiply or divide can be lowered using two-result operations.
3012 SDVTList VTs = DAG.getVTList(VT, VT);
3013 if (Node->getOpcode() == ISD::MUL) {
3014 // We just need the low half of the multiply; try both the signed
3015 // and unsigned forms. If the target supports both SMUL_LOHI and
3016 // UMUL_LOHI, form a preference by checking which forms of plain
3017 // MULH it supports.
3018 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3019 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3020 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3021 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3022 unsigned OpToUse = 0;
3023 if (HasSMUL_LOHI && !HasMULHS) {
3024 OpToUse = ISD::SMUL_LOHI;
3025 } else if (HasUMUL_LOHI && !HasMULHU) {
3026 OpToUse = ISD::UMUL_LOHI;
3027 } else if (HasSMUL_LOHI) {
3028 OpToUse = ISD::SMUL_LOHI;
3029 } else if (HasUMUL_LOHI) {
3030 OpToUse = ISD::UMUL_LOHI;
3031 }
3032 if (OpToUse) {
3033 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3034 break;
3035 }
3036 }
3037 if (Node->getOpcode() == ISD::MULHS &&
3038 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3039 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3040 break;
3041 }
3042 if (Node->getOpcode() == ISD::MULHU &&
3043 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3044 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3045 break;
3046 }
3047 if (Node->getOpcode() == ISD::SDIV &&
3048 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3049 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3050 break;
3051 }
3052 if (Node->getOpcode() == ISD::UDIV &&
3053 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3054 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3055 break;
3056 }
3057
Dan Gohman6d05cac2007-10-11 23:57:53 +00003058 // Check to see if we have a libcall for this operator.
3059 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3060 bool isSigned = false;
3061 switch (Node->getOpcode()) {
3062 case ISD::UDIV:
3063 case ISD::SDIV:
3064 if (VT == MVT::i32) {
3065 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003067 isSigned = Node->getOpcode() == ISD::SDIV;
3068 }
3069 break;
3070 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003071 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3072 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003073 break;
3074 default: break;
3075 }
3076 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3077 SDOperand Dummy;
3078 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079 break;
3080 }
3081
3082 assert(MVT::isVector(Node->getValueType(0)) &&
3083 "Cannot expand this binary operator!");
3084 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003085 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 break;
3087 }
3088 case TargetLowering::Promote: {
3089 switch (Node->getOpcode()) {
3090 default: assert(0 && "Do not know how to promote this BinOp!");
3091 case ISD::AND:
3092 case ISD::OR:
3093 case ISD::XOR: {
3094 MVT::ValueType OVT = Node->getValueType(0);
3095 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3096 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3097 // Bit convert each of the values to the new type.
3098 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3099 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3100 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3101 // Bit convert the result back the original type.
3102 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3103 break;
3104 }
3105 }
3106 }
3107 }
3108 break;
3109
Dan Gohman475cd732007-10-05 14:17:22 +00003110 case ISD::SMUL_LOHI:
3111 case ISD::UMUL_LOHI:
3112 case ISD::SDIVREM:
3113 case ISD::UDIVREM:
3114 // These nodes will only be produced by target-specific lowering, so
3115 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003116 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003117 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003118
3119 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3120 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3121 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003122 break;
3123
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003124 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3125 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3126 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3127 case Expand: assert(0 && "Not possible");
3128 case Legal:
3129 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3130 break;
3131 case Promote:
3132 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3133 break;
3134 }
3135
3136 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3137
3138 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3139 default: assert(0 && "Operation not supported");
3140 case TargetLowering::Custom:
3141 Tmp1 = TLI.LowerOperation(Result, DAG);
3142 if (Tmp1.Val) Result = Tmp1;
3143 break;
3144 case TargetLowering::Legal: break;
3145 case TargetLowering::Expand: {
3146 // If this target supports fabs/fneg natively and select is cheap,
3147 // do this efficiently.
3148 if (!TLI.isSelectExpensive() &&
3149 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3150 TargetLowering::Legal &&
3151 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3152 TargetLowering::Legal) {
3153 // Get the sign bit of the RHS.
3154 MVT::ValueType IVT =
3155 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3156 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3157 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3158 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3159 // Get the absolute value of the result.
3160 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3161 // Select between the nabs and abs value based on the sign bit of
3162 // the input.
3163 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3164 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3165 AbsVal),
3166 AbsVal);
3167 Result = LegalizeOp(Result);
3168 break;
3169 }
3170
3171 // Otherwise, do bitwise ops!
3172 MVT::ValueType NVT =
3173 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3174 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3175 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3176 Result = LegalizeOp(Result);
3177 break;
3178 }
3179 }
3180 break;
3181
3182 case ISD::ADDC:
3183 case ISD::SUBC:
3184 Tmp1 = LegalizeOp(Node->getOperand(0));
3185 Tmp2 = LegalizeOp(Node->getOperand(1));
3186 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3187 // Since this produces two values, make sure to remember that we legalized
3188 // both of them.
3189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3191 return Result;
3192
3193 case ISD::ADDE:
3194 case ISD::SUBE:
3195 Tmp1 = LegalizeOp(Node->getOperand(0));
3196 Tmp2 = LegalizeOp(Node->getOperand(1));
3197 Tmp3 = LegalizeOp(Node->getOperand(2));
3198 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3199 // Since this produces two values, make sure to remember that we legalized
3200 // both of them.
3201 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3202 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3203 return Result;
3204
3205 case ISD::BUILD_PAIR: {
3206 MVT::ValueType PairTy = Node->getValueType(0);
3207 // TODO: handle the case where the Lo and Hi operands are not of legal type
3208 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3209 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3210 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3211 case TargetLowering::Promote:
3212 case TargetLowering::Custom:
3213 assert(0 && "Cannot promote/custom this yet!");
3214 case TargetLowering::Legal:
3215 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3216 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3217 break;
3218 case TargetLowering::Expand:
3219 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3220 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3221 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3222 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3223 TLI.getShiftAmountTy()));
3224 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3225 break;
3226 }
3227 break;
3228 }
3229
3230 case ISD::UREM:
3231 case ISD::SREM:
3232 case ISD::FREM:
3233 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3234 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3235
3236 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3237 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3238 case TargetLowering::Custom:
3239 isCustom = true;
3240 // FALLTHROUGH
3241 case TargetLowering::Legal:
3242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3243 if (isCustom) {
3244 Tmp1 = TLI.LowerOperation(Result, DAG);
3245 if (Tmp1.Val) Result = Tmp1;
3246 }
3247 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003248 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3250 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00003251 MVT::ValueType VT = Node->getValueType(0);
3252
3253 // See if remainder can be lowered using two-result operations.
3254 SDVTList VTs = DAG.getVTList(VT, VT);
3255 if (Node->getOpcode() == ISD::SREM &&
3256 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3257 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3258 break;
3259 }
3260 if (Node->getOpcode() == ISD::UREM &&
3261 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3262 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3263 break;
3264 }
3265
3266 if (MVT::isInteger(VT)) {
3267 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003268 TargetLowering::Legal) {
3269 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3271 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3272 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003273 } else if (MVT::isVector(VT)) {
3274 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003275 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003276 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277 "Cannot expand this binary operator!");
3278 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3279 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3280 SDOperand Dummy;
3281 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
3282 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003283 } else {
3284 assert(MVT::isFloatingPoint(VT) &&
3285 "remainder op must have integer or floating-point type");
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003286 if (MVT::isVector(VT)) {
3287 Result = LegalizeOp(UnrollVectorOp(Op));
3288 } else {
3289 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003290 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3291 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003292 SDOperand Dummy;
3293 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3294 false/*sign irrelevant*/, Dummy);
3295 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 }
3297 break;
3298 }
Dan Gohman5a199552007-10-08 18:33:35 +00003299 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 break;
3301 case ISD::VAARG: {
3302 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3303 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3304
3305 MVT::ValueType VT = Node->getValueType(0);
3306 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3307 default: assert(0 && "This action is not supported yet!");
3308 case TargetLowering::Custom:
3309 isCustom = true;
3310 // FALLTHROUGH
3311 case TargetLowering::Legal:
3312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3313 Result = Result.getValue(0);
3314 Tmp1 = Result.getValue(1);
3315
3316 if (isCustom) {
3317 Tmp2 = TLI.LowerOperation(Result, DAG);
3318 if (Tmp2.Val) {
3319 Result = LegalizeOp(Tmp2);
3320 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3321 }
3322 }
3323 break;
3324 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003325 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3326 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 // Increment the pointer, VAList, to the next vaarg
3328 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3329 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3330 TLI.getPointerTy()));
3331 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003332 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333 // Load the actual argument out of the pointer VAList
3334 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3335 Tmp1 = LegalizeOp(Result.getValue(1));
3336 Result = LegalizeOp(Result);
3337 break;
3338 }
3339 }
3340 // Since VAARG produces two values, make sure to remember that we
3341 // legalized both of them.
3342 AddLegalizedOperand(SDOperand(Node, 0), Result);
3343 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3344 return Op.ResNo ? Tmp1 : Result;
3345 }
3346
3347 case ISD::VACOPY:
3348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3349 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3350 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3351
3352 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3353 default: assert(0 && "This action is not supported yet!");
3354 case TargetLowering::Custom:
3355 isCustom = true;
3356 // FALLTHROUGH
3357 case TargetLowering::Legal:
3358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3359 Node->getOperand(3), Node->getOperand(4));
3360 if (isCustom) {
3361 Tmp1 = TLI.LowerOperation(Result, DAG);
3362 if (Tmp1.Val) Result = Tmp1;
3363 }
3364 break;
3365 case TargetLowering::Expand:
3366 // This defaults to loading a pointer from the input and storing it to the
3367 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003368 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3369 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3370 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3371 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003372 break;
3373 }
3374 break;
3375
3376 case ISD::VAEND:
3377 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3378 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3379
3380 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3381 default: assert(0 && "This action is not supported yet!");
3382 case TargetLowering::Custom:
3383 isCustom = true;
3384 // FALLTHROUGH
3385 case TargetLowering::Legal:
3386 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3387 if (isCustom) {
3388 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3389 if (Tmp1.Val) Result = Tmp1;
3390 }
3391 break;
3392 case TargetLowering::Expand:
3393 Result = Tmp1; // Default to a no-op, return the chain
3394 break;
3395 }
3396 break;
3397
3398 case ISD::VASTART:
3399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3400 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3401
3402 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3403
3404 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3405 default: assert(0 && "This action is not supported yet!");
3406 case TargetLowering::Legal: break;
3407 case TargetLowering::Custom:
3408 Tmp1 = TLI.LowerOperation(Result, DAG);
3409 if (Tmp1.Val) Result = Tmp1;
3410 break;
3411 }
3412 break;
3413
3414 case ISD::ROTL:
3415 case ISD::ROTR:
3416 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3417 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3418 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3419 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3420 default:
3421 assert(0 && "ROTL/ROTR legalize operation not supported");
3422 break;
3423 case TargetLowering::Legal:
3424 break;
3425 case TargetLowering::Custom:
3426 Tmp1 = TLI.LowerOperation(Result, DAG);
3427 if (Tmp1.Val) Result = Tmp1;
3428 break;
3429 case TargetLowering::Promote:
3430 assert(0 && "Do not know how to promote ROTL/ROTR");
3431 break;
3432 case TargetLowering::Expand:
3433 assert(0 && "Do not know how to expand ROTL/ROTR");
3434 break;
3435 }
3436 break;
3437
3438 case ISD::BSWAP:
3439 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3440 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3441 case TargetLowering::Custom:
3442 assert(0 && "Cannot custom legalize this yet!");
3443 case TargetLowering::Legal:
3444 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3445 break;
3446 case TargetLowering::Promote: {
3447 MVT::ValueType OVT = Tmp1.getValueType();
3448 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3449 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3450
3451 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3452 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3453 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3454 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3455 break;
3456 }
3457 case TargetLowering::Expand:
3458 Result = ExpandBSWAP(Tmp1);
3459 break;
3460 }
3461 break;
3462
3463 case ISD::CTPOP:
3464 case ISD::CTTZ:
3465 case ISD::CTLZ:
3466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3467 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003468 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003469 case TargetLowering::Legal:
3470 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003471 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003472 TargetLowering::Custom) {
3473 Tmp1 = TLI.LowerOperation(Result, DAG);
3474 if (Tmp1.Val) {
3475 Result = Tmp1;
3476 }
Scott Michel48b63e62007-07-30 21:00:31 +00003477 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003478 break;
3479 case TargetLowering::Promote: {
3480 MVT::ValueType OVT = Tmp1.getValueType();
3481 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3482
3483 // Zero extend the argument.
3484 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3485 // Perform the larger operation, then subtract if needed.
3486 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3487 switch (Node->getOpcode()) {
3488 case ISD::CTPOP:
3489 Result = Tmp1;
3490 break;
3491 case ISD::CTTZ:
3492 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3493 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3494 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3495 ISD::SETEQ);
3496 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003497 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498 break;
3499 case ISD::CTLZ:
3500 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3501 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3502 DAG.getConstant(MVT::getSizeInBits(NVT) -
3503 MVT::getSizeInBits(OVT), NVT));
3504 break;
3505 }
3506 break;
3507 }
3508 case TargetLowering::Expand:
3509 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3510 break;
3511 }
3512 break;
3513
3514 // Unary operators
3515 case ISD::FABS:
3516 case ISD::FNEG:
3517 case ISD::FSQRT:
3518 case ISD::FSIN:
3519 case ISD::FCOS:
3520 Tmp1 = LegalizeOp(Node->getOperand(0));
3521 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3522 case TargetLowering::Promote:
3523 case TargetLowering::Custom:
3524 isCustom = true;
3525 // FALLTHROUGH
3526 case TargetLowering::Legal:
3527 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3528 if (isCustom) {
3529 Tmp1 = TLI.LowerOperation(Result, DAG);
3530 if (Tmp1.Val) Result = Tmp1;
3531 }
3532 break;
3533 case TargetLowering::Expand:
3534 switch (Node->getOpcode()) {
3535 default: assert(0 && "Unreachable!");
3536 case ISD::FNEG:
3537 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3538 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3539 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3540 break;
3541 case ISD::FABS: {
3542 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3543 MVT::ValueType VT = Node->getValueType(0);
3544 Tmp2 = DAG.getConstantFP(0.0, VT);
3545 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
3546 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3547 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3548 break;
3549 }
3550 case ISD::FSQRT:
3551 case ISD::FSIN:
3552 case ISD::FCOS: {
3553 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003554
3555 // Expand unsupported unary vector operators by unrolling them.
3556 if (MVT::isVector(VT)) {
3557 Result = LegalizeOp(UnrollVectorOp(Op));
3558 break;
3559 }
3560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003561 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3562 switch(Node->getOpcode()) {
3563 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003564 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3565 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003566 break;
3567 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003568 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3569 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003570 break;
3571 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003572 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3573 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003574 break;
3575 default: assert(0 && "Unreachable!");
3576 }
3577 SDOperand Dummy;
3578 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3579 false/*sign irrelevant*/, Dummy);
3580 break;
3581 }
3582 }
3583 break;
3584 }
3585 break;
3586 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003587 MVT::ValueType VT = Node->getValueType(0);
3588
3589 // Expand unsupported unary vector operators by unrolling them.
3590 if (MVT::isVector(VT)) {
3591 Result = LegalizeOp(UnrollVectorOp(Op));
3592 break;
3593 }
3594
3595 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003596 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3597 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003598 SDOperand Dummy;
3599 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3600 false/*sign irrelevant*/, Dummy);
3601 break;
3602 }
3603 case ISD::BIT_CONVERT:
3604 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003605 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3606 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3608 // The input has to be a vector type, we have to either scalarize it, pack
3609 // it, or convert it based on whether the input vector type is legal.
3610 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003611 int InIx = Node->getOperand(0).ResNo;
3612 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3613 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614
3615 // Figure out if there is a simple type corresponding to this Vector
3616 // type. If so, convert to the vector type.
3617 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3618 if (TLI.isTypeLegal(TVT)) {
3619 // Turn this into a bit convert of the vector input.
3620 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3621 LegalizeOp(Node->getOperand(0)));
3622 break;
3623 } else if (NumElems == 1) {
3624 // Turn this into a bit convert of the scalar input.
3625 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3626 ScalarizeVectorOp(Node->getOperand(0)));
3627 break;
3628 } else {
3629 // FIXME: UNIMP! Store then reload
3630 assert(0 && "Cast from unsupported vector type not implemented yet!");
3631 }
3632 } else {
3633 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3634 Node->getOperand(0).getValueType())) {
3635 default: assert(0 && "Unknown operation action!");
3636 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003637 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3638 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003639 break;
3640 case TargetLowering::Legal:
3641 Tmp1 = LegalizeOp(Node->getOperand(0));
3642 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3643 break;
3644 }
3645 }
3646 break;
3647
3648 // Conversion operators. The source and destination have different types.
3649 case ISD::SINT_TO_FP:
3650 case ISD::UINT_TO_FP: {
3651 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3652 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3653 case Legal:
3654 switch (TLI.getOperationAction(Node->getOpcode(),
3655 Node->getOperand(0).getValueType())) {
3656 default: assert(0 && "Unknown operation action!");
3657 case TargetLowering::Custom:
3658 isCustom = true;
3659 // FALLTHROUGH
3660 case TargetLowering::Legal:
3661 Tmp1 = LegalizeOp(Node->getOperand(0));
3662 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3663 if (isCustom) {
3664 Tmp1 = TLI.LowerOperation(Result, DAG);
3665 if (Tmp1.Val) Result = Tmp1;
3666 }
3667 break;
3668 case TargetLowering::Expand:
3669 Result = ExpandLegalINT_TO_FP(isSigned,
3670 LegalizeOp(Node->getOperand(0)),
3671 Node->getValueType(0));
3672 break;
3673 case TargetLowering::Promote:
3674 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3675 Node->getValueType(0),
3676 isSigned);
3677 break;
3678 }
3679 break;
3680 case Expand:
3681 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3682 Node->getValueType(0), Node->getOperand(0));
3683 break;
3684 case Promote:
3685 Tmp1 = PromoteOp(Node->getOperand(0));
3686 if (isSigned) {
3687 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3688 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3689 } else {
3690 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3691 Node->getOperand(0).getValueType());
3692 }
3693 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3694 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3695 break;
3696 }
3697 break;
3698 }
3699 case ISD::TRUNCATE:
3700 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3701 case Legal:
3702 Tmp1 = LegalizeOp(Node->getOperand(0));
3703 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3704 break;
3705 case Expand:
3706 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3707
3708 // Since the result is legal, we should just be able to truncate the low
3709 // part of the source.
3710 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3711 break;
3712 case Promote:
3713 Result = PromoteOp(Node->getOperand(0));
3714 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3715 break;
3716 }
3717 break;
3718
3719 case ISD::FP_TO_SINT:
3720 case ISD::FP_TO_UINT:
3721 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3722 case Legal:
3723 Tmp1 = LegalizeOp(Node->getOperand(0));
3724
3725 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3726 default: assert(0 && "Unknown operation action!");
3727 case TargetLowering::Custom:
3728 isCustom = true;
3729 // FALLTHROUGH
3730 case TargetLowering::Legal:
3731 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3732 if (isCustom) {
3733 Tmp1 = TLI.LowerOperation(Result, DAG);
3734 if (Tmp1.Val) Result = Tmp1;
3735 }
3736 break;
3737 case TargetLowering::Promote:
3738 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3739 Node->getOpcode() == ISD::FP_TO_SINT);
3740 break;
3741 case TargetLowering::Expand:
3742 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3743 SDOperand True, False;
3744 MVT::ValueType VT = Node->getOperand(0).getValueType();
3745 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003746 const uint64_t zero[] = {0, 0};
3747 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohman88ae8c52008-02-29 01:44:25 +00003748 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3749 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003750 Tmp2 = DAG.getConstantFP(apf, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3752 Node->getOperand(0), Tmp2, ISD::SETLT);
3753 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3754 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3755 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3756 Tmp2));
3757 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003758 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3760 break;
3761 } else {
3762 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3763 }
3764 break;
3765 }
3766 break;
3767 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003769 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003770 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003771 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003772 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3773 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3774 Node->getOperand(0), DAG.getValueType(MVT::f64));
3775 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3776 DAG.getIntPtrConstant(1));
3777 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3778 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003779 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3780 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3781 Tmp2 = DAG.getConstantFP(apf, OVT);
3782 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3783 // FIXME: generated code sucks.
3784 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3785 DAG.getNode(ISD::ADD, MVT::i32,
3786 DAG.getNode(ISD::FP_TO_SINT, VT,
3787 DAG.getNode(ISD::FSUB, OVT,
3788 Node->getOperand(0), Tmp2)),
3789 DAG.getConstant(0x80000000, MVT::i32)),
3790 DAG.getNode(ISD::FP_TO_SINT, VT,
3791 Node->getOperand(0)),
3792 DAG.getCondCode(ISD::SETGE));
3793 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003794 break;
3795 }
Dale Johannesend3b6af32007-10-11 23:32:15 +00003796 // Convert f32 / f64 to i32 / i64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003797 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3798 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003799 case ISD::FP_TO_SINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003800 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003801 LC = (VT == MVT::i32)
3802 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003803 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804 LC = (VT == MVT::i32)
3805 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003806 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003807 assert(VT == MVT::i64);
Dale Johannesenac77b272007-10-05 20:04:43 +00003808 LC = RTLIB::FPTOSINT_F80_I64;
3809 }
3810 else if (OVT == MVT::ppcf128) {
3811 assert(VT == MVT::i64);
3812 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003813 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003814 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003815 }
3816 case ISD::FP_TO_UINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003817 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003818 LC = (VT == MVT::i32)
3819 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003820 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 LC = (VT == MVT::i32)
3822 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003823 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003824 LC = (VT == MVT::i32)
Dale Johannesenac77b272007-10-05 20:04:43 +00003825 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3826 }
3827 else if (OVT == MVT::ppcf128) {
3828 assert(VT == MVT::i64);
3829 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003830 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003832 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003833 default: assert(0 && "Unreachable!");
3834 }
3835 SDOperand Dummy;
3836 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3837 false/*sign irrelevant*/, Dummy);
3838 break;
3839 }
3840 case Promote:
3841 Tmp1 = PromoteOp(Node->getOperand(0));
3842 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3843 Result = LegalizeOp(Result);
3844 break;
3845 }
3846 break;
3847
Chris Lattner56ecde32008-01-16 06:57:07 +00003848 case ISD::FP_EXTEND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003849 MVT::ValueType DstVT = Op.getValueType();
3850 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3851 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3852 // The only other way we can lower this is to turn it into a STORE,
3853 // LOAD pair, targetting a temporary location (a stack slot).
3854 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3855 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003856 }
3857 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3858 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3859 case Legal:
3860 Tmp1 = LegalizeOp(Node->getOperand(0));
3861 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3862 break;
3863 case Promote:
3864 Tmp1 = PromoteOp(Node->getOperand(0));
3865 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3866 break;
3867 }
3868 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003869 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003870 case ISD::FP_ROUND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003871 MVT::ValueType DstVT = Op.getValueType();
3872 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3873 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3874 if (SrcVT == MVT::ppcf128) {
Dale Johannesena0d36082008-01-20 01:18:38 +00003875 SDOperand Lo;
3876 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003877 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003878 if (DstVT!=MVT::f64)
3879 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003880 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003881 }
Chris Lattner5872a362008-01-17 07:00:52 +00003882 // The only other way we can lower this is to turn it into a STORE,
3883 // LOAD pair, targetting a temporary location (a stack slot).
3884 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3885 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003886 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003887 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3888 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3889 case Legal:
3890 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003891 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003892 break;
3893 case Promote:
3894 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003895 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3896 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003897 break;
3898 }
3899 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003900 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 case ISD::ANY_EXTEND:
3902 case ISD::ZERO_EXTEND:
3903 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3905 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3906 case Legal:
3907 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac7091c2008-02-15 23:05:48 +00003908 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3909 TargetLowering::Custom) {
3910 Tmp2 = TLI.LowerOperation(Result, DAG);
3911 if (Tmp2.Val) {
3912 Tmp1 = Tmp2;
3913 }
3914 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003915 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3916 break;
3917 case Promote:
3918 switch (Node->getOpcode()) {
3919 case ISD::ANY_EXTEND:
3920 Tmp1 = PromoteOp(Node->getOperand(0));
3921 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3922 break;
3923 case ISD::ZERO_EXTEND:
3924 Result = PromoteOp(Node->getOperand(0));
3925 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3926 Result = DAG.getZeroExtendInReg(Result,
3927 Node->getOperand(0).getValueType());
3928 break;
3929 case ISD::SIGN_EXTEND:
3930 Result = PromoteOp(Node->getOperand(0));
3931 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3932 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3933 Result,
3934 DAG.getValueType(Node->getOperand(0).getValueType()));
3935 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003936 }
3937 }
3938 break;
3939 case ISD::FP_ROUND_INREG:
3940 case ISD::SIGN_EXTEND_INREG: {
3941 Tmp1 = LegalizeOp(Node->getOperand(0));
3942 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3943
3944 // If this operation is not supported, convert it to a shl/shr or load/store
3945 // pair.
3946 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3947 default: assert(0 && "This action not supported for this op yet!");
3948 case TargetLowering::Legal:
3949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3950 break;
3951 case TargetLowering::Expand:
3952 // If this is an integer extend and shifts are supported, do that.
3953 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3954 // NOTE: we could fall back on load/store here too for targets without
3955 // SAR. However, it is doubtful that any exist.
3956 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3957 MVT::getSizeInBits(ExtraVT);
3958 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3959 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3960 Node->getOperand(0), ShiftCst);
3961 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3962 Result, ShiftCst);
3963 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3964 // The only way we can lower this is to turn it into a TRUNCSTORE,
3965 // EXTLOAD pair, targetting a temporary location (a stack slot).
3966
3967 // NOTE: there is a choice here between constantly creating new stack
3968 // slots and always reusing the same one. We currently always create
3969 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003970 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3971 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 } else {
3973 assert(0 && "Unknown op");
3974 }
3975 break;
3976 }
3977 break;
3978 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003979 case ISD::TRAMPOLINE: {
3980 SDOperand Ops[6];
3981 for (unsigned i = 0; i != 6; ++i)
3982 Ops[i] = LegalizeOp(Node->getOperand(i));
3983 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3984 // The only option for this node is to custom lower it.
3985 Result = TLI.LowerOperation(Result, DAG);
3986 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003987
3988 // Since trampoline produces two values, make sure to remember that we
3989 // legalized both of them.
3990 Tmp1 = LegalizeOp(Result.getValue(1));
3991 Result = LegalizeOp(Result);
3992 AddLegalizedOperand(SDOperand(Node, 0), Result);
3993 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3994 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003995 }
Dan Gohman819574c2008-01-31 00:41:03 +00003996 case ISD::FLT_ROUNDS_: {
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003997 MVT::ValueType VT = Node->getValueType(0);
3998 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3999 default: assert(0 && "This action not supported for this op yet!");
4000 case TargetLowering::Custom:
4001 Result = TLI.LowerOperation(Op, DAG);
4002 if (Result.Val) break;
4003 // Fall Thru
4004 case TargetLowering::Legal:
4005 // If this operation is not supported, lower it to constant 1
4006 Result = DAG.getConstant(1, VT);
4007 break;
4008 }
4009 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004010 case ISD::TRAP: {
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004011 MVT::ValueType VT = Node->getValueType(0);
4012 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4013 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004014 case TargetLowering::Legal:
4015 Tmp1 = LegalizeOp(Node->getOperand(0));
4016 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4017 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004018 case TargetLowering::Custom:
4019 Result = TLI.LowerOperation(Op, DAG);
4020 if (Result.Val) break;
4021 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004022 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004023 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004024 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004025 TargetLowering::ArgListTy Args;
4026 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004027 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4028 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00004029 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4030 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004031 Result = CallResult.second;
4032 break;
4033 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004034 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004035 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 }
4037
4038 assert(Result.getValueType() == Op.getValueType() &&
4039 "Bad legalization!");
4040
4041 // Make sure that the generated code is itself legal.
4042 if (Result != Op)
4043 Result = LegalizeOp(Result);
4044
4045 // Note that LegalizeOp may be reentered even from single-use nodes, which
4046 // means that we always must cache transformed nodes.
4047 AddLegalizedOperand(Op, Result);
4048 return Result;
4049}
4050
4051/// PromoteOp - Given an operation that produces a value in an invalid type,
4052/// promote it to compute the value into a larger type. The produced value will
4053/// have the correct bits for the low portion of the register, but no guarantee
4054/// is made about the top bits: it may be zero, sign-extended, or garbage.
4055SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4056 MVT::ValueType VT = Op.getValueType();
4057 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4058 assert(getTypeAction(VT) == Promote &&
4059 "Caller should expand or legalize operands that are not promotable!");
4060 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4061 "Cannot promote to smaller type!");
4062
4063 SDOperand Tmp1, Tmp2, Tmp3;
4064 SDOperand Result;
4065 SDNode *Node = Op.Val;
4066
4067 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
4068 if (I != PromotedNodes.end()) return I->second;
4069
4070 switch (Node->getOpcode()) {
4071 case ISD::CopyFromReg:
4072 assert(0 && "CopyFromReg must be legal!");
4073 default:
4074#ifndef NDEBUG
4075 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4076#endif
4077 assert(0 && "Do not know how to promote this operator!");
4078 abort();
4079 case ISD::UNDEF:
4080 Result = DAG.getNode(ISD::UNDEF, NVT);
4081 break;
4082 case ISD::Constant:
4083 if (VT != MVT::i1)
4084 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4085 else
4086 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4087 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4088 break;
4089 case ISD::ConstantFP:
4090 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4091 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4092 break;
4093
4094 case ISD::SETCC:
4095 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
4096 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4097 Node->getOperand(1), Node->getOperand(2));
4098 break;
4099
4100 case ISD::TRUNCATE:
4101 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4102 case Legal:
4103 Result = LegalizeOp(Node->getOperand(0));
4104 assert(Result.getValueType() >= NVT &&
4105 "This truncation doesn't make sense!");
4106 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4107 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4108 break;
4109 case Promote:
4110 // The truncation is not required, because we don't guarantee anything
4111 // about high bits anyway.
4112 Result = PromoteOp(Node->getOperand(0));
4113 break;
4114 case Expand:
4115 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4116 // Truncate the low part of the expanded value to the result type
4117 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4118 }
4119 break;
4120 case ISD::SIGN_EXTEND:
4121 case ISD::ZERO_EXTEND:
4122 case ISD::ANY_EXTEND:
4123 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4124 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4125 case Legal:
4126 // Input is legal? Just do extend all the way to the larger type.
4127 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4128 break;
4129 case Promote:
4130 // Promote the reg if it's smaller.
4131 Result = PromoteOp(Node->getOperand(0));
4132 // The high bits are not guaranteed to be anything. Insert an extend.
4133 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4134 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4135 DAG.getValueType(Node->getOperand(0).getValueType()));
4136 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4137 Result = DAG.getZeroExtendInReg(Result,
4138 Node->getOperand(0).getValueType());
4139 break;
4140 }
4141 break;
4142 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004143 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4144 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004145 Result = PromoteOp(Result);
4146 break;
4147
4148 case ISD::FP_EXTEND:
4149 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4150 case ISD::FP_ROUND:
4151 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4152 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4153 case Promote: assert(0 && "Unreachable with 2 FP types!");
4154 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004155 if (Node->getConstantOperandVal(1) == 0) {
4156 // Input is legal? Do an FP_ROUND_INREG.
4157 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4158 DAG.getValueType(VT));
4159 } else {
4160 // Just remove the truncate, it isn't affecting the value.
4161 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4162 Node->getOperand(1));
4163 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 break;
4165 }
4166 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 case ISD::SINT_TO_FP:
4168 case ISD::UINT_TO_FP:
4169 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4170 case Legal:
4171 // No extra round required here.
4172 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4173 break;
4174
4175 case Promote:
4176 Result = PromoteOp(Node->getOperand(0));
4177 if (Node->getOpcode() == ISD::SINT_TO_FP)
4178 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4179 Result,
4180 DAG.getValueType(Node->getOperand(0).getValueType()));
4181 else
4182 Result = DAG.getZeroExtendInReg(Result,
4183 Node->getOperand(0).getValueType());
4184 // No extra round required here.
4185 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4186 break;
4187 case Expand:
4188 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4189 Node->getOperand(0));
4190 // Round if we cannot tolerate excess precision.
4191 if (NoExcessFPPrecision)
4192 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4193 DAG.getValueType(VT));
4194 break;
4195 }
4196 break;
4197
4198 case ISD::SIGN_EXTEND_INREG:
4199 Result = PromoteOp(Node->getOperand(0));
4200 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4201 Node->getOperand(1));
4202 break;
4203 case ISD::FP_TO_SINT:
4204 case ISD::FP_TO_UINT:
4205 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4206 case Legal:
4207 case Expand:
4208 Tmp1 = Node->getOperand(0);
4209 break;
4210 case Promote:
4211 // The input result is prerounded, so we don't have to do anything
4212 // special.
4213 Tmp1 = PromoteOp(Node->getOperand(0));
4214 break;
4215 }
4216 // If we're promoting a UINT to a larger size, check to see if the new node
4217 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4218 // we can use that instead. This allows us to generate better code for
4219 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4220 // legal, such as PowerPC.
4221 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4222 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4223 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4224 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4225 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4226 } else {
4227 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4228 }
4229 break;
4230
4231 case ISD::FABS:
4232 case ISD::FNEG:
4233 Tmp1 = PromoteOp(Node->getOperand(0));
4234 assert(Tmp1.getValueType() == NVT);
4235 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4236 // NOTE: we do not have to do any extra rounding here for
4237 // NoExcessFPPrecision, because we know the input will have the appropriate
4238 // precision, and these operations don't modify precision at all.
4239 break;
4240
4241 case ISD::FSQRT:
4242 case ISD::FSIN:
4243 case ISD::FCOS:
4244 Tmp1 = PromoteOp(Node->getOperand(0));
4245 assert(Tmp1.getValueType() == NVT);
4246 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4247 if (NoExcessFPPrecision)
4248 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4249 DAG.getValueType(VT));
4250 break;
4251
4252 case ISD::FPOWI: {
4253 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4254 // directly as well, which may be better.
4255 Tmp1 = PromoteOp(Node->getOperand(0));
4256 assert(Tmp1.getValueType() == NVT);
4257 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4258 if (NoExcessFPPrecision)
4259 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4260 DAG.getValueType(VT));
4261 break;
4262 }
4263
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004264 case ISD::ATOMIC_LCS: {
4265 Tmp2 = PromoteOp(Node->getOperand(2));
4266 Tmp3 = PromoteOp(Node->getOperand(3));
4267 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4268 Node->getOperand(1), Tmp2, Tmp3,
4269 cast<AtomicSDNode>(Node)->getVT());
4270 // Remember that we legalized the chain.
4271 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4272 break;
4273 }
4274 case ISD::ATOMIC_LAS:
4275 case ISD::ATOMIC_SWAP: {
4276 Tmp2 = PromoteOp(Node->getOperand(2));
4277 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4278 Node->getOperand(1), Tmp2,
4279 cast<AtomicSDNode>(Node)->getVT());
4280 // Remember that we legalized the chain.
4281 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4282 break;
4283 }
4284
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285 case ISD::AND:
4286 case ISD::OR:
4287 case ISD::XOR:
4288 case ISD::ADD:
4289 case ISD::SUB:
4290 case ISD::MUL:
4291 // The input may have strange things in the top bits of the registers, but
4292 // these operations don't care. They may have weird bits going out, but
4293 // that too is okay if they are integer operations.
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 break;
4299 case ISD::FADD:
4300 case ISD::FSUB:
4301 case ISD::FMUL:
4302 Tmp1 = PromoteOp(Node->getOperand(0));
4303 Tmp2 = PromoteOp(Node->getOperand(1));
4304 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4305 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4306
4307 // Floating point operations will give excess precision that we may not be
4308 // able to tolerate. If we DO allow excess precision, just leave it,
4309 // otherwise excise it.
4310 // FIXME: Why would we need to round FP ops more than integer ones?
4311 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4312 if (NoExcessFPPrecision)
4313 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4314 DAG.getValueType(VT));
4315 break;
4316
4317 case ISD::SDIV:
4318 case ISD::SREM:
4319 // These operators require that their input be sign extended.
4320 Tmp1 = PromoteOp(Node->getOperand(0));
4321 Tmp2 = PromoteOp(Node->getOperand(1));
4322 if (MVT::isInteger(NVT)) {
4323 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4324 DAG.getValueType(VT));
4325 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4326 DAG.getValueType(VT));
4327 }
4328 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4329
4330 // Perform FP_ROUND: this is probably overly pessimistic.
4331 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
4332 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4333 DAG.getValueType(VT));
4334 break;
4335 case ISD::FDIV:
4336 case ISD::FREM:
4337 case ISD::FCOPYSIGN:
4338 // These operators require that their input be fp extended.
4339 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004340 case Expand: assert(0 && "not implemented");
4341 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4342 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 }
4344 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004345 case Expand: assert(0 && "not implemented");
4346 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4347 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348 }
4349 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4350
4351 // Perform FP_ROUND: this is probably overly pessimistic.
4352 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4353 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4354 DAG.getValueType(VT));
4355 break;
4356
4357 case ISD::UDIV:
4358 case ISD::UREM:
4359 // These operators require that their input be zero extended.
4360 Tmp1 = PromoteOp(Node->getOperand(0));
4361 Tmp2 = PromoteOp(Node->getOperand(1));
4362 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
4363 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4364 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4365 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4366 break;
4367
4368 case ISD::SHL:
4369 Tmp1 = PromoteOp(Node->getOperand(0));
4370 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4371 break;
4372 case ISD::SRA:
4373 // The input value must be properly sign extended.
4374 Tmp1 = PromoteOp(Node->getOperand(0));
4375 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4376 DAG.getValueType(VT));
4377 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4378 break;
4379 case ISD::SRL:
4380 // The input value must be properly zero extended.
4381 Tmp1 = PromoteOp(Node->getOperand(0));
4382 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4383 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4384 break;
4385
4386 case ISD::VAARG:
4387 Tmp1 = Node->getOperand(0); // Get the chain.
4388 Tmp2 = Node->getOperand(1); // Get the pointer.
4389 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4390 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4391 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4392 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004393 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4394 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004395 // Increment the pointer, VAList, to the next vaarg
4396 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4397 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4398 TLI.getPointerTy()));
4399 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004400 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 // Load the actual argument out of the pointer VAList
4402 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4403 }
4404 // Remember that we legalized the chain.
4405 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4406 break;
4407
4408 case ISD::LOAD: {
4409 LoadSDNode *LD = cast<LoadSDNode>(Node);
4410 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4411 ? ISD::EXTLOAD : LD->getExtensionType();
4412 Result = DAG.getExtLoad(ExtType, NVT,
4413 LD->getChain(), LD->getBasePtr(),
4414 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004415 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416 LD->isVolatile(),
4417 LD->getAlignment());
4418 // Remember that we legalized the chain.
4419 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4420 break;
4421 }
4422 case ISD::SELECT:
4423 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4424 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4425 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4426 break;
4427 case ISD::SELECT_CC:
4428 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4429 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4430 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4431 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4432 break;
4433 case ISD::BSWAP:
4434 Tmp1 = Node->getOperand(0);
4435 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4436 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4437 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4438 DAG.getConstant(MVT::getSizeInBits(NVT) -
4439 MVT::getSizeInBits(VT),
4440 TLI.getShiftAmountTy()));
4441 break;
4442 case ISD::CTPOP:
4443 case ISD::CTTZ:
4444 case ISD::CTLZ:
4445 // Zero extend the argument
4446 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4447 // Perform the larger operation, then subtract if needed.
4448 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4449 switch(Node->getOpcode()) {
4450 case ISD::CTPOP:
4451 Result = Tmp1;
4452 break;
4453 case ISD::CTTZ:
4454 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4455 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
4456 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4457 ISD::SETEQ);
4458 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4459 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4460 break;
4461 case ISD::CTLZ:
4462 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4463 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4464 DAG.getConstant(MVT::getSizeInBits(NVT) -
4465 MVT::getSizeInBits(VT), NVT));
4466 break;
4467 }
4468 break;
4469 case ISD::EXTRACT_SUBVECTOR:
4470 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4471 break;
4472 case ISD::EXTRACT_VECTOR_ELT:
4473 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4474 break;
4475 }
4476
4477 assert(Result.Val && "Didn't set a result!");
4478
4479 // Make sure the result is itself legal.
4480 Result = LegalizeOp(Result);
4481
4482 // Remember that we promoted this!
4483 AddPromotedOperand(Op, Result);
4484 return Result;
4485}
4486
4487/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4488/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4489/// based on the vector type. The return type of this matches the element type
4490/// of the vector, which may not be legal for the target.
4491SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4492 // We know that operand #0 is the Vec vector. If the index is a constant
4493 // or if the invec is a supported hardware type, we can use it. Otherwise,
4494 // lower to a store then an indexed load.
4495 SDOperand Vec = Op.getOperand(0);
4496 SDOperand Idx = Op.getOperand(1);
4497
Dan Gohmana0763d92007-09-24 15:54:53 +00004498 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 unsigned NumElems = MVT::getVectorNumElements(TVT);
4500
4501 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4502 default: assert(0 && "This action is not supported yet!");
4503 case TargetLowering::Custom: {
4504 Vec = LegalizeOp(Vec);
4505 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4506 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4507 if (Tmp3.Val)
4508 return Tmp3;
4509 break;
4510 }
4511 case TargetLowering::Legal:
4512 if (isTypeLegal(TVT)) {
4513 Vec = LegalizeOp(Vec);
4514 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004515 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004516 }
4517 break;
4518 case TargetLowering::Expand:
4519 break;
4520 }
4521
4522 if (NumElems == 1) {
4523 // This must be an access of the only element. Return it.
4524 Op = ScalarizeVectorOp(Vec);
4525 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004526 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4528 SDOperand Lo, Hi;
4529 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004530 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004531 Vec = Lo;
4532 } else {
4533 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004534 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535 Idx.getValueType());
4536 }
4537
4538 // It's now an extract from the appropriate high or low part. Recurse.
4539 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4540 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4541 } else {
4542 // Store the value to a temporary stack slot, then LOAD the scalar
4543 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004544 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4546
4547 // Add the offset to the index.
4548 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4549 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4550 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004551
4552 if (MVT::getSizeInBits(Idx.getValueType()) >
4553 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004554 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004555 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004556 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4559
4560 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4561 }
4562 return Op;
4563}
4564
4565/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4566/// we assume the operation can be split if it is not already legal.
4567SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4568 // We know that operand #0 is the Vec vector. For now we assume the index
4569 // is a constant and that the extracted result is a supported hardware type.
4570 SDOperand Vec = Op.getOperand(0);
4571 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4572
4573 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4574
4575 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4576 // This must be an access of the desired vector length. Return it.
4577 return Vec;
4578 }
4579
4580 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4581 SDOperand Lo, Hi;
4582 SplitVectorOp(Vec, Lo, Hi);
4583 if (CIdx->getValue() < NumElems/2) {
4584 Vec = Lo;
4585 } else {
4586 Vec = Hi;
4587 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4588 }
4589
4590 // It's now an extract from the appropriate high or low part. Recurse.
4591 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4592 return ExpandEXTRACT_SUBVECTOR(Op);
4593}
4594
4595/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4596/// with condition CC on the current target. This usually involves legalizing
4597/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4598/// there may be no choice but to create a new SetCC node to represent the
4599/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4600/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4601void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4602 SDOperand &RHS,
4603 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004604 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605
4606 switch (getTypeAction(LHS.getValueType())) {
4607 case Legal:
4608 Tmp1 = LegalizeOp(LHS); // LHS
4609 Tmp2 = LegalizeOp(RHS); // RHS
4610 break;
4611 case Promote:
4612 Tmp1 = PromoteOp(LHS); // LHS
4613 Tmp2 = PromoteOp(RHS); // RHS
4614
4615 // If this is an FP compare, the operands have already been extended.
4616 if (MVT::isInteger(LHS.getValueType())) {
4617 MVT::ValueType VT = LHS.getValueType();
4618 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4619
4620 // Otherwise, we have to insert explicit sign or zero extends. Note
4621 // that we could insert sign extends for ALL conditions, but zero extend
4622 // is cheaper on many machines (an AND instead of two shifts), so prefer
4623 // it.
4624 switch (cast<CondCodeSDNode>(CC)->get()) {
4625 default: assert(0 && "Unknown integer comparison!");
4626 case ISD::SETEQ:
4627 case ISD::SETNE:
4628 case ISD::SETUGE:
4629 case ISD::SETUGT:
4630 case ISD::SETULE:
4631 case ISD::SETULT:
4632 // ALL of these operations will work if we either sign or zero extend
4633 // the operands (including the unsigned comparisons!). Zero extend is
4634 // usually a simpler/cheaper operation, so prefer it.
4635 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4636 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4637 break;
4638 case ISD::SETGE:
4639 case ISD::SETGT:
4640 case ISD::SETLT:
4641 case ISD::SETLE:
4642 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4643 DAG.getValueType(VT));
4644 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4645 DAG.getValueType(VT));
4646 break;
4647 }
4648 }
4649 break;
4650 case Expand: {
4651 MVT::ValueType VT = LHS.getValueType();
4652 if (VT == MVT::f32 || VT == MVT::f64) {
4653 // Expand into one or more soft-fp libcall(s).
4654 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4655 switch (cast<CondCodeSDNode>(CC)->get()) {
4656 case ISD::SETEQ:
4657 case ISD::SETOEQ:
4658 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4659 break;
4660 case ISD::SETNE:
4661 case ISD::SETUNE:
4662 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4663 break;
4664 case ISD::SETGE:
4665 case ISD::SETOGE:
4666 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4667 break;
4668 case ISD::SETLT:
4669 case ISD::SETOLT:
4670 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4671 break;
4672 case ISD::SETLE:
4673 case ISD::SETOLE:
4674 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4675 break;
4676 case ISD::SETGT:
4677 case ISD::SETOGT:
4678 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4679 break;
4680 case ISD::SETUO:
4681 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4682 break;
4683 case ISD::SETO:
4684 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4685 break;
4686 default:
4687 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4688 switch (cast<CondCodeSDNode>(CC)->get()) {
4689 case ISD::SETONE:
4690 // SETONE = SETOLT | SETOGT
4691 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4692 // Fallthrough
4693 case ISD::SETUGT:
4694 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4695 break;
4696 case ISD::SETUGE:
4697 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4698 break;
4699 case ISD::SETULT:
4700 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4701 break;
4702 case ISD::SETULE:
4703 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4704 break;
4705 case ISD::SETUEQ:
4706 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4707 break;
4708 default: assert(0 && "Unsupported FP setcc!");
4709 }
4710 }
4711
4712 SDOperand Dummy;
4713 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
4714 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4715 false /*sign irrelevant*/, Dummy);
4716 Tmp2 = DAG.getConstant(0, MVT::i32);
4717 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4718 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
4719 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
4720 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
4721 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4722 false /*sign irrelevant*/, Dummy);
4723 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4724 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4725 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4726 Tmp2 = SDOperand();
4727 }
4728 LHS = Tmp1;
4729 RHS = Tmp2;
4730 return;
4731 }
4732
4733 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4734 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004735 ExpandOp(RHS, RHSLo, RHSHi);
4736 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4737
4738 if (VT==MVT::ppcf128) {
4739 // FIXME: This generated code sucks. We want to generate
4740 // FCMP crN, hi1, hi2
4741 // BNE crN, L:
4742 // FCMP crN, lo1, lo2
4743 // The following can be improved, but not that much.
4744 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4745 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4746 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4747 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4748 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4749 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4750 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4751 Tmp2 = SDOperand();
4752 break;
4753 }
4754
4755 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756 case ISD::SETEQ:
4757 case ISD::SETNE:
4758 if (RHSLo == RHSHi)
4759 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4760 if (RHSCST->isAllOnesValue()) {
4761 // Comparison to -1.
4762 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4763 Tmp2 = RHSLo;
4764 break;
4765 }
4766
4767 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4768 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4769 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4770 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4771 break;
4772 default:
4773 // If this is a comparison of the sign bit, just look at the top part.
4774 // X > -1, x < 0
4775 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4776 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4777 CST->getValue() == 0) || // X < 0
4778 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4779 CST->isAllOnesValue())) { // X > -1
4780 Tmp1 = LHSHi;
4781 Tmp2 = RHSHi;
4782 break;
4783 }
4784
4785 // FIXME: This generated code sucks.
4786 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 switch (CCCode) {
4788 default: assert(0 && "Unknown integer setcc!");
4789 case ISD::SETLT:
4790 case ISD::SETULT: LowCC = ISD::SETULT; break;
4791 case ISD::SETGT:
4792 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4793 case ISD::SETLE:
4794 case ISD::SETULE: LowCC = ISD::SETULE; break;
4795 case ISD::SETGE:
4796 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4797 }
4798
4799 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4800 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4801 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4802
4803 // NOTE: on targets without efficient SELECT of bools, we can always use
4804 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4805 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4806 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4807 false, DagCombineInfo);
4808 if (!Tmp1.Val)
4809 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4810 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4811 CCCode, false, DagCombineInfo);
4812 if (!Tmp2.Val)
Chris Lattner6fb53da2007-10-15 17:48:57 +00004813 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814
4815 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4816 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4817 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4818 (Tmp2C && Tmp2C->getValue() == 0 &&
4819 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4820 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4821 (Tmp2C && Tmp2C->getValue() == 1 &&
4822 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4823 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4824 // low part is known false, returns high part.
4825 // For LE / GE, if high part is known false, ignore the low part.
4826 // For LT / GT, if high part is known true, ignore the low part.
4827 Tmp1 = Tmp2;
4828 Tmp2 = SDOperand();
4829 } else {
4830 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4831 ISD::SETEQ, false, DagCombineInfo);
4832 if (!Result.Val)
4833 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4834 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4835 Result, Tmp1, Tmp2));
4836 Tmp1 = Result;
4837 Tmp2 = SDOperand();
4838 }
4839 }
4840 }
4841 }
4842 LHS = Tmp1;
4843 RHS = Tmp2;
4844}
4845
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004846/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4847/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4848/// a load from the stack slot to DestVT, extending it if needed.
4849/// The resultant code need not be legal.
4850SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4851 MVT::ValueType SlotVT,
4852 MVT::ValueType DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004853 // Create the stack frame object.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004854 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4855
Dan Gohman20e37962008-02-11 18:58:42 +00004856 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004857 int SPFI = StackPtrFI->getIndex();
4858
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004859 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4860 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4861 unsigned DestSize = MVT::getSizeInBits(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004863 // Emit a store to the stack slot. Use a truncstore if the input value is
4864 // later than DestVT.
4865 SDOperand Store;
4866 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004867 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004868 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004869 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004870 else {
4871 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004872 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004873 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004874 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004875 }
4876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004878 if (SlotSize == DestSize)
4879 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4880
4881 assert(SlotSize < DestSize && "Unknown extension!");
4882 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883}
4884
4885SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4886 // Create a vector sized/aligned stack slot, store the value to element #0,
4887 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004888 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00004889
Dan Gohman20e37962008-02-11 18:58:42 +00004890 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004891 int SPFI = StackPtrFI->getIndex();
4892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004894 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman12a9c082008-02-06 22:27:42 +00004895 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004896 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004897}
4898
4899
4900/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4901/// support the operation, but do support the resultant vector type.
4902SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4903
4904 // If the only non-undef value is the low element, turn this into a
4905 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4906 unsigned NumElems = Node->getNumOperands();
4907 bool isOnlyLowElement = true;
4908 SDOperand SplatValue = Node->getOperand(0);
4909 std::map<SDOperand, std::vector<unsigned> > Values;
4910 Values[SplatValue].push_back(0);
4911 bool isConstant = true;
4912 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4913 SplatValue.getOpcode() != ISD::UNDEF)
4914 isConstant = false;
4915
4916 for (unsigned i = 1; i < NumElems; ++i) {
4917 SDOperand V = Node->getOperand(i);
4918 Values[V].push_back(i);
4919 if (V.getOpcode() != ISD::UNDEF)
4920 isOnlyLowElement = false;
4921 if (SplatValue != V)
4922 SplatValue = SDOperand(0,0);
4923
4924 // If this isn't a constant element or an undef, we can't use a constant
4925 // pool load.
4926 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4927 V.getOpcode() != ISD::UNDEF)
4928 isConstant = false;
4929 }
4930
4931 if (isOnlyLowElement) {
4932 // If the low element is an undef too, then this whole things is an undef.
4933 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4934 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4935 // Otherwise, turn this into a scalar_to_vector node.
4936 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4937 Node->getOperand(0));
4938 }
4939
4940 // If all elements are constants, create a load from the constant pool.
4941 if (isConstant) {
4942 MVT::ValueType VT = Node->getValueType(0);
4943 const Type *OpNTy =
4944 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4945 std::vector<Constant*> CV;
4946 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4947 if (ConstantFPSDNode *V =
4948 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00004949 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 } else if (ConstantSDNode *V =
4951 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4952 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4953 } else {
4954 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4955 CV.push_back(UndefValue::get(OpNTy));
4956 }
4957 }
4958 Constant *CP = ConstantVector::get(CV);
4959 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004960 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004961 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 }
4963
4964 if (SplatValue.Val) { // Splat of one value?
4965 // Build the shuffle constant vector: <0, 0, 0, 0>
4966 MVT::ValueType MaskVT =
4967 MVT::getIntVectorWithNumElements(NumElems);
4968 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4969 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4970 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4971 &ZeroVec[0], ZeroVec.size());
4972
4973 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4974 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4975 // Get the splatted value into the low element of a vector register.
4976 SDOperand LowValVec =
4977 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4978
4979 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4980 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4981 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4982 SplatMask);
4983 }
4984 }
4985
4986 // If there are only two unique elements, we may be able to turn this into a
4987 // vector shuffle.
4988 if (Values.size() == 2) {
4989 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4990 MVT::ValueType MaskVT =
4991 MVT::getIntVectorWithNumElements(NumElems);
4992 std::vector<SDOperand> MaskVec(NumElems);
4993 unsigned i = 0;
4994 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4995 E = Values.end(); I != E; ++I) {
4996 for (std::vector<unsigned>::iterator II = I->second.begin(),
4997 EE = I->second.end(); II != EE; ++II)
4998 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
4999 i += NumElems;
5000 }
5001 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5002 &MaskVec[0], MaskVec.size());
5003
5004 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5005 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5006 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
5007 SmallVector<SDOperand, 8> Ops;
5008 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5009 E = Values.end(); I != E; ++I) {
5010 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5011 I->first);
5012 Ops.push_back(Op);
5013 }
5014 Ops.push_back(ShuffleMask);
5015
5016 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
5017 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5018 &Ops[0], Ops.size());
5019 }
5020 }
5021
5022 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5023 // aligned object on the stack, store each element into it, then load
5024 // the result as a vector.
5025 MVT::ValueType VT = Node->getValueType(0);
5026 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00005027 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
5029 // Emit a store of each element to the stack slot.
5030 SmallVector<SDOperand, 8> Stores;
5031 unsigned TypeByteSize =
5032 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
5033 // Store (in the right endianness) the elements to memory.
5034 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5035 // Ignore undef elements.
5036 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5037
5038 unsigned Offset = TypeByteSize*i;
5039
5040 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5041 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5042
5043 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5044 NULL, 0));
5045 }
5046
5047 SDOperand StoreChain;
5048 if (!Stores.empty()) // Not all undef elements?
5049 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5050 &Stores[0], Stores.size());
5051 else
5052 StoreChain = DAG.getEntryNode();
5053
5054 // Result is a load from the stack slot.
5055 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5056}
5057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5059 SDOperand Op, SDOperand Amt,
5060 SDOperand &Lo, SDOperand &Hi) {
5061 // Expand the subcomponents.
5062 SDOperand LHSL, LHSH;
5063 ExpandOp(Op, LHSL, LHSH);
5064
5065 SDOperand Ops[] = { LHSL, LHSH, Amt };
5066 MVT::ValueType VT = LHSL.getValueType();
5067 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5068 Hi = Lo.getValue(1);
5069}
5070
5071
5072/// ExpandShift - Try to find a clever way to expand this shift operation out to
5073/// smaller elements. If we can't find a way that is more efficient than a
5074/// libcall on this target, return false. Otherwise, return true with the
5075/// low-parts expanded into Lo and Hi.
5076bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5077 SDOperand &Lo, SDOperand &Hi) {
5078 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5079 "This is not a shift!");
5080
5081 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
5082 SDOperand ShAmt = LegalizeOp(Amt);
5083 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohmanece0a882008-02-20 16:57:27 +00005084 unsigned ShBits = MVT::getSizeInBits(ShTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005085 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5086 unsigned NVTBits = MVT::getSizeInBits(NVT);
5087
Chris Lattner8c931452007-10-14 20:35:12 +00005088 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005089 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5090 unsigned Cst = CN->getValue();
5091 // Expand the incoming operand to be shifted, so that we have its parts
5092 SDOperand InL, InH;
5093 ExpandOp(Op, InL, InH);
5094 switch(Opc) {
5095 case ISD::SHL:
5096 if (Cst > VTBits) {
5097 Lo = DAG.getConstant(0, NVT);
5098 Hi = DAG.getConstant(0, NVT);
5099 } else if (Cst > NVTBits) {
5100 Lo = DAG.getConstant(0, NVT);
5101 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5102 } else if (Cst == NVTBits) {
5103 Lo = DAG.getConstant(0, NVT);
5104 Hi = InL;
5105 } else {
5106 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5107 Hi = DAG.getNode(ISD::OR, NVT,
5108 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5109 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5110 }
5111 return true;
5112 case ISD::SRL:
5113 if (Cst > VTBits) {
5114 Lo = DAG.getConstant(0, NVT);
5115 Hi = DAG.getConstant(0, NVT);
5116 } else if (Cst > NVTBits) {
5117 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5118 Hi = DAG.getConstant(0, NVT);
5119 } else if (Cst == NVTBits) {
5120 Lo = InH;
5121 Hi = DAG.getConstant(0, NVT);
5122 } else {
5123 Lo = DAG.getNode(ISD::OR, NVT,
5124 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5125 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5126 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5127 }
5128 return true;
5129 case ISD::SRA:
5130 if (Cst > VTBits) {
5131 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5132 DAG.getConstant(NVTBits-1, ShTy));
5133 } else if (Cst > NVTBits) {
5134 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5135 DAG.getConstant(Cst-NVTBits, ShTy));
5136 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5137 DAG.getConstant(NVTBits-1, ShTy));
5138 } else if (Cst == NVTBits) {
5139 Lo = InH;
5140 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5141 DAG.getConstant(NVTBits-1, ShTy));
5142 } else {
5143 Lo = DAG.getNode(ISD::OR, NVT,
5144 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5145 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5146 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5147 }
5148 return true;
5149 }
5150 }
5151
5152 // Okay, the shift amount isn't constant. However, if we can tell that it is
5153 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005154 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5155 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005156 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5157
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005158 // If we know that if any of the high bits of the shift amount are one, then
5159 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005160 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005161 // Mask out the high bit, which we know is set.
5162 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005163 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005164
5165 // Expand the incoming operand to be shifted, so that we have its parts
5166 SDOperand InL, InH;
5167 ExpandOp(Op, InL, InH);
5168 switch(Opc) {
5169 case ISD::SHL:
5170 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5171 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5172 return true;
5173 case ISD::SRL:
5174 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5175 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5176 return true;
5177 case ISD::SRA:
5178 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5179 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5180 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5181 return true;
5182 }
5183 }
5184
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005185 // If we know that the high bits of the shift amount are all zero, then we can
5186 // do this as a couple of simple shifts.
5187 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188 // Compute 32-amt.
5189 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5190 DAG.getConstant(NVTBits, Amt.getValueType()),
5191 Amt);
5192
5193 // Expand the incoming operand to be shifted, so that we have its parts
5194 SDOperand InL, InH;
5195 ExpandOp(Op, InL, InH);
5196 switch(Opc) {
5197 case ISD::SHL:
5198 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5199 Hi = DAG.getNode(ISD::OR, NVT,
5200 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5201 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5202 return true;
5203 case ISD::SRL:
5204 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5205 Lo = DAG.getNode(ISD::OR, NVT,
5206 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5207 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5208 return true;
5209 case ISD::SRA:
5210 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5211 Lo = DAG.getNode(ISD::OR, NVT,
5212 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5213 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5214 return true;
5215 }
5216 }
5217
5218 return false;
5219}
5220
5221
5222// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5223// does not fit into a register, return the lo part and set the hi part to the
5224// by-reg argument. If it does fit into a single register, return the result
5225// and leave the Hi part unset.
5226SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
5227 bool isSigned, SDOperand &Hi) {
5228 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5229 // The input chain to this libcall is the entry node of the function.
5230 // Legalizing the call will automatically add the previous call to the
5231 // dependence.
5232 SDOperand InChain = DAG.getEntryNode();
5233
5234 TargetLowering::ArgListTy Args;
5235 TargetLowering::ArgListEntry Entry;
5236 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5237 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5238 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
5239 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5240 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005241 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005242 Args.push_back(Entry);
5243 }
5244 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
5245
5246 // Splice the libcall in wherever FindInputOutputChains tells us to.
5247 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
5248 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005249 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5250 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005251
5252 // Legalize the call sequence, starting with the chain. This will advance
5253 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5254 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5255 LegalizeOp(CallInfo.second);
5256 SDOperand Result;
5257 switch (getTypeAction(CallInfo.first.getValueType())) {
5258 default: assert(0 && "Unknown thing");
5259 case Legal:
5260 Result = CallInfo.first;
5261 break;
5262 case Expand:
5263 ExpandOp(CallInfo.first, Result, Hi);
5264 break;
5265 }
5266 return Result;
5267}
5268
5269
5270/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5271///
5272SDOperand SelectionDAGLegalize::
5273ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
5274 assert(getTypeAction(Source.getValueType()) == Expand &&
5275 "This is not an expansion!");
5276 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5277
5278 if (!isSigned) {
5279 assert(Source.getValueType() == MVT::i64 &&
5280 "This only works for 64-bit -> FP");
5281 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5282 // incoming integer is set. To handle this, we dynamically test to see if
5283 // it is set, and, if so, add a fudge factor.
5284 SDOperand Lo, Hi;
5285 ExpandOp(Source, Lo, Hi);
5286
5287 // If this is unsigned, and not supported, first perform the conversion to
5288 // signed, then adjust the result if the sign bit is set.
5289 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5290 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5291
5292 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5293 DAG.getConstant(0, Hi.getValueType()),
5294 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005295 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5297 SignSet, Four, Zero);
5298 uint64_t FF = 0x5f800000ULL;
5299 if (TLI.isLittleEndian()) FF <<= 32;
5300 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5301
5302 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5303 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5304 SDOperand FudgeInReg;
5305 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005306 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005307 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005308 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005310 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005311 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005312 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005313 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005314 else
5315 assert(0 && "Unexpected conversion");
5316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317 MVT::ValueType SCVT = SignedConv.getValueType();
5318 if (SCVT != DestTy) {
5319 // Destination type needs to be expanded as well. The FADD now we are
5320 // constructing will be expanded into a libcall.
5321 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5322 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5323 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5324 SignedConv, SignedConv.getValue(1));
5325 }
5326 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5327 }
5328 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5329 }
5330
5331 // Check to see if the target has a custom way to lower this. If so, use it.
5332 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5333 default: assert(0 && "This action not implemented for this operation!");
5334 case TargetLowering::Legal:
5335 case TargetLowering::Expand:
5336 break; // This case is handled below.
5337 case TargetLowering::Custom: {
5338 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5339 Source), DAG);
5340 if (NV.Val)
5341 return LegalizeOp(NV);
5342 break; // The target decided this was legal after all
5343 }
5344 }
5345
5346 // Expand the source, then glue it back together for the call. We must expand
5347 // the source in case it is shared (this pass of legalize must traverse it).
5348 SDOperand SrcLo, SrcHi;
5349 ExpandOp(Source, SrcLo, SrcHi);
5350 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5351
5352 RTLIB::Libcall LC;
5353 if (DestTy == MVT::f32)
5354 LC = RTLIB::SINTTOFP_I64_F32;
5355 else {
5356 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5357 LC = RTLIB::SINTTOFP_I64_F64;
5358 }
5359
5360 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
5361 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5362 SDOperand UnusedHiPart;
5363 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5364 UnusedHiPart);
5365}
5366
5367/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5368/// INT_TO_FP operation of the specified operand when the target requests that
5369/// we expand it. At this point, we know that the result and operand types are
5370/// legal for the target.
5371SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5372 SDOperand Op0,
5373 MVT::ValueType DestVT) {
5374 if (Op0.getValueType() == MVT::i32) {
5375 // simple 32-bit [signed|unsigned] integer to float/double expansion
5376
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005377 // Get the stack frame index of a 8 byte buffer.
5378 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380 // word offset constant for Hi/Lo address computation
5381 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5382 // set up Hi and Lo (into buffer) address based on endian
5383 SDOperand Hi = StackSlot;
5384 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5385 if (TLI.isLittleEndian())
5386 std::swap(Hi, Lo);
5387
5388 // if signed map to unsigned space
5389 SDOperand Op0Mapped;
5390 if (isSigned) {
5391 // constant used to invert sign bit (signed to unsigned mapping)
5392 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5393 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5394 } else {
5395 Op0Mapped = Op0;
5396 }
5397 // store the lo of the constructed double - based on integer input
5398 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5399 Op0Mapped, Lo, NULL, 0);
5400 // initial hi portion of constructed double
5401 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5402 // store the hi of the constructed double - biased exponent
5403 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5404 // load the constructed double
5405 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5406 // FP constant to bias correct the final result
5407 SDOperand Bias = DAG.getConstantFP(isSigned ?
5408 BitsToDouble(0x4330000080000000ULL)
5409 : BitsToDouble(0x4330000000000000ULL),
5410 MVT::f64);
5411 // subtract the bias
5412 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5413 // final result
5414 SDOperand Result;
5415 // handle final rounding
5416 if (DestVT == MVT::f64) {
5417 // do nothing
5418 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005419 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005420 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5421 DAG.getIntPtrConstant(0));
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005422 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5423 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005424 }
5425 return Result;
5426 }
5427 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5428 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5429
5430 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5431 DAG.getConstant(0, Op0.getValueType()),
5432 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005433 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5435 SignSet, Four, Zero);
5436
5437 // If the sign bit of the integer is set, the large number will be treated
5438 // as a negative number. To counteract this, the dynamic code adds an
5439 // offset depending on the data type.
5440 uint64_t FF;
5441 switch (Op0.getValueType()) {
5442 default: assert(0 && "Unsupported integer type!");
5443 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5444 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5445 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5446 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5447 }
5448 if (TLI.isLittleEndian()) FF <<= 32;
5449 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5450
5451 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5452 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5453 SDOperand FudgeInReg;
5454 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005455 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005456 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005457 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005458 FudgeInReg =
5459 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5460 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005461 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005462 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005463 }
5464
5465 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5466}
5467
5468/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5469/// *INT_TO_FP operation of the specified operand when the target requests that
5470/// we promote it. At this point, we know that the result and operand types are
5471/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5472/// operation that takes a larger input.
5473SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5474 MVT::ValueType DestVT,
5475 bool isSigned) {
5476 // First step, figure out the appropriate *INT_TO_FP operation to use.
5477 MVT::ValueType NewInTy = LegalOp.getValueType();
5478
5479 unsigned OpToUse = 0;
5480
5481 // Scan for the appropriate larger type to use.
5482 while (1) {
5483 NewInTy = (MVT::ValueType)(NewInTy+1);
5484 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5485
5486 // If the target supports SINT_TO_FP of this type, use it.
5487 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5488 default: break;
5489 case TargetLowering::Legal:
5490 if (!TLI.isTypeLegal(NewInTy))
5491 break; // Can't use this datatype.
5492 // FALL THROUGH.
5493 case TargetLowering::Custom:
5494 OpToUse = ISD::SINT_TO_FP;
5495 break;
5496 }
5497 if (OpToUse) break;
5498 if (isSigned) continue;
5499
5500 // If the target supports UINT_TO_FP of this type, use it.
5501 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5502 default: break;
5503 case TargetLowering::Legal:
5504 if (!TLI.isTypeLegal(NewInTy))
5505 break; // Can't use this datatype.
5506 // FALL THROUGH.
5507 case TargetLowering::Custom:
5508 OpToUse = ISD::UINT_TO_FP;
5509 break;
5510 }
5511 if (OpToUse) break;
5512
5513 // Otherwise, try a larger type.
5514 }
5515
5516 // Okay, we found the operation and type to use. Zero extend our input to the
5517 // desired type then run the operation on it.
5518 return DAG.getNode(OpToUse, DestVT,
5519 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5520 NewInTy, LegalOp));
5521}
5522
5523/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5524/// FP_TO_*INT operation of the specified operand when the target requests that
5525/// we promote it. At this point, we know that the result and operand types are
5526/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5527/// operation that returns a larger result.
5528SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5529 MVT::ValueType DestVT,
5530 bool isSigned) {
5531 // First step, figure out the appropriate FP_TO*INT operation to use.
5532 MVT::ValueType NewOutTy = DestVT;
5533
5534 unsigned OpToUse = 0;
5535
5536 // Scan for the appropriate larger type to use.
5537 while (1) {
5538 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5539 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5540
5541 // If the target supports FP_TO_SINT returning this type, use it.
5542 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5543 default: break;
5544 case TargetLowering::Legal:
5545 if (!TLI.isTypeLegal(NewOutTy))
5546 break; // Can't use this datatype.
5547 // FALL THROUGH.
5548 case TargetLowering::Custom:
5549 OpToUse = ISD::FP_TO_SINT;
5550 break;
5551 }
5552 if (OpToUse) break;
5553
5554 // If the target supports FP_TO_UINT of this type, use it.
5555 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5556 default: break;
5557 case TargetLowering::Legal:
5558 if (!TLI.isTypeLegal(NewOutTy))
5559 break; // Can't use this datatype.
5560 // FALL THROUGH.
5561 case TargetLowering::Custom:
5562 OpToUse = ISD::FP_TO_UINT;
5563 break;
5564 }
5565 if (OpToUse) break;
5566
5567 // Otherwise, try a larger type.
5568 }
5569
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005570
5571 // Okay, we found the operation and type to use.
5572 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5573
5574 // If the operation produces an invalid type, it must be custom lowered. Use
5575 // the target lowering hooks to expand it. Just keep the low part of the
5576 // expanded operation, we know that we're truncating anyway.
5577 if (getTypeAction(NewOutTy) == Expand) {
5578 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5579 assert(Operation.Val && "Didn't return anything");
5580 }
5581
5582 // Truncate the result of the extended FP_TO_*INT operation to the desired
5583 // size.
5584 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585}
5586
5587/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5588///
5589SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5590 MVT::ValueType VT = Op.getValueType();
5591 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5592 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5593 switch (VT) {
5594 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5595 case MVT::i16:
5596 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5597 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5598 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5599 case MVT::i32:
5600 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5601 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5602 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5603 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5604 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5605 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5606 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5607 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5608 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5609 case MVT::i64:
5610 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5611 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5612 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5613 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5614 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5615 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5616 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5617 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5618 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5619 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5620 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5621 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5622 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5623 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5624 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5625 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5626 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5627 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5628 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5629 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5630 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5631 }
5632}
5633
5634/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5635///
5636SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5637 switch (Opc) {
5638 default: assert(0 && "Cannot expand this yet!");
5639 case ISD::CTPOP: {
5640 static const uint64_t mask[6] = {
5641 0x5555555555555555ULL, 0x3333333333333333ULL,
5642 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5643 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5644 };
5645 MVT::ValueType VT = Op.getValueType();
5646 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5647 unsigned len = MVT::getSizeInBits(VT);
5648 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5649 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5650 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5651 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5652 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5653 DAG.getNode(ISD::AND, VT,
5654 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5655 }
5656 return Op;
5657 }
5658 case ISD::CTLZ: {
5659 // for now, we do this:
5660 // x = x | (x >> 1);
5661 // x = x | (x >> 2);
5662 // ...
5663 // x = x | (x >>16);
5664 // x = x | (x >>32); // for 64-bit input
5665 // return popcount(~x);
5666 //
5667 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5668 MVT::ValueType VT = Op.getValueType();
5669 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5670 unsigned len = MVT::getSizeInBits(VT);
5671 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5672 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5673 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5674 }
5675 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5676 return DAG.getNode(ISD::CTPOP, VT, Op);
5677 }
5678 case ISD::CTTZ: {
5679 // for now, we use: { return popcount(~x & (x - 1)); }
5680 // unless the target has ctlz but not ctpop, in which case we use:
5681 // { return 32 - nlz(~x & (x-1)); }
5682 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5683 MVT::ValueType VT = Op.getValueType();
5684 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5685 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5686 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5687 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5688 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5689 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5690 TLI.isOperationLegal(ISD::CTLZ, VT))
5691 return DAG.getNode(ISD::SUB, VT,
5692 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5693 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5694 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5695 }
5696 }
5697}
5698
5699/// ExpandOp - Expand the specified SDOperand into its two component pieces
5700/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5701/// LegalizeNodes map is filled in for any results that are not expanded, the
5702/// ExpandedNodes map is filled in for any results that are expanded, and the
5703/// Lo/Hi values are returned.
5704void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5705 MVT::ValueType VT = Op.getValueType();
5706 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5707 SDNode *Node = Op.Val;
5708 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5709 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5710 MVT::isVector(VT)) &&
5711 "Cannot expand to FP value or to larger int value!");
5712
5713 // See if we already expanded it.
5714 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5715 = ExpandedNodes.find(Op);
5716 if (I != ExpandedNodes.end()) {
5717 Lo = I->second.first;
5718 Hi = I->second.second;
5719 return;
5720 }
5721
5722 switch (Node->getOpcode()) {
5723 case ISD::CopyFromReg:
5724 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005725 case ISD::FP_ROUND_INREG:
5726 if (VT == MVT::ppcf128 &&
5727 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5728 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005729 SDOperand SrcLo, SrcHi, Src;
5730 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5731 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5732 SDOperand Result = TLI.LowerOperation(
5733 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005734 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5735 Lo = Result.Val->getOperand(0);
5736 Hi = Result.Val->getOperand(1);
5737 break;
5738 }
5739 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005740 default:
5741#ifndef NDEBUG
5742 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5743#endif
5744 assert(0 && "Do not know how to expand this operator!");
5745 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00005746 case ISD::EXTRACT_ELEMENT:
5747 ExpandOp(Node->getOperand(0), Lo, Hi);
5748 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5749 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00005750 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005751 case ISD::EXTRACT_VECTOR_ELT:
5752 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5753 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5754 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5755 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005756 case ISD::UNDEF:
5757 NVT = TLI.getTypeToExpandTo(VT);
5758 Lo = DAG.getNode(ISD::UNDEF, NVT);
5759 Hi = DAG.getNode(ISD::UNDEF, NVT);
5760 break;
5761 case ISD::Constant: {
5762 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5763 Lo = DAG.getConstant(Cst, NVT);
5764 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5765 break;
5766 }
5767 case ISD::ConstantFP: {
5768 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005769 if (CFP->getValueType(0) == MVT::ppcf128) {
5770 APInt api = CFP->getValueAPF().convertToAPInt();
5771 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5772 MVT::f64);
5773 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5774 MVT::f64);
5775 break;
5776 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5778 if (getTypeAction(Lo.getValueType()) == Expand)
5779 ExpandOp(Lo, Lo, Hi);
5780 break;
5781 }
5782 case ISD::BUILD_PAIR:
5783 // Return the operands.
5784 Lo = Node->getOperand(0);
5785 Hi = Node->getOperand(1);
5786 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005787
5788 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005789 if (Node->getNumValues() == 1) {
5790 ExpandOp(Op.getOperand(0), Lo, Hi);
5791 break;
5792 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005793 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5794 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5795 Op.getValue(1).getValueType() == MVT::Other &&
5796 "unhandled MERGE_VALUES");
5797 ExpandOp(Op.getOperand(0), Lo, Hi);
5798 // Remember that we legalized the chain.
5799 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5800 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005801
5802 case ISD::SIGN_EXTEND_INREG:
5803 ExpandOp(Node->getOperand(0), Lo, Hi);
5804 // sext_inreg the low part if needed.
5805 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5806
5807 // The high part gets the sign extension from the lo-part. This handles
5808 // things like sextinreg V:i64 from i8.
5809 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5810 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5811 TLI.getShiftAmountTy()));
5812 break;
5813
5814 case ISD::BSWAP: {
5815 ExpandOp(Node->getOperand(0), Lo, Hi);
5816 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5817 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5818 Lo = TempLo;
5819 break;
5820 }
5821
5822 case ISD::CTPOP:
5823 ExpandOp(Node->getOperand(0), Lo, Hi);
5824 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5825 DAG.getNode(ISD::CTPOP, NVT, Lo),
5826 DAG.getNode(ISD::CTPOP, NVT, Hi));
5827 Hi = DAG.getConstant(0, NVT);
5828 break;
5829
5830 case ISD::CTLZ: {
5831 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5832 ExpandOp(Node->getOperand(0), Lo, Hi);
5833 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5834 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5835 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5836 ISD::SETNE);
5837 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5838 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5839
5840 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5841 Hi = DAG.getConstant(0, NVT);
5842 break;
5843 }
5844
5845 case ISD::CTTZ: {
5846 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5847 ExpandOp(Node->getOperand(0), Lo, Hi);
5848 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5849 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5850 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5851 ISD::SETNE);
5852 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5853 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5854
5855 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5856 Hi = DAG.getConstant(0, NVT);
5857 break;
5858 }
5859
5860 case ISD::VAARG: {
5861 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5862 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5863 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5864 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5865
5866 // Remember that we legalized the chain.
5867 Hi = LegalizeOp(Hi);
5868 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005869 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005870 std::swap(Lo, Hi);
5871 break;
5872 }
5873
5874 case ISD::LOAD: {
5875 LoadSDNode *LD = cast<LoadSDNode>(Node);
5876 SDOperand Ch = LD->getChain(); // Legalize the chain.
5877 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5878 ISD::LoadExtType ExtType = LD->getExtensionType();
5879 int SVOffset = LD->getSrcValueOffset();
5880 unsigned Alignment = LD->getAlignment();
5881 bool isVolatile = LD->isVolatile();
5882
5883 if (ExtType == ISD::NON_EXTLOAD) {
5884 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5885 isVolatile, Alignment);
5886 if (VT == MVT::f32 || VT == MVT::f64) {
5887 // f32->i32 or f64->i64 one to one expansion.
5888 // Remember that we legalized the chain.
5889 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5890 // Recursively expand the new load.
5891 if (getTypeAction(NVT) == Expand)
5892 ExpandOp(Lo, Lo, Hi);
5893 break;
5894 }
5895
5896 // Increment the pointer to the other half.
5897 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5898 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005899 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005900 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005901 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005902 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5903 isVolatile, Alignment);
5904
5905 // Build a factor node to remember that this load is independent of the
5906 // other one.
5907 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5908 Hi.getValue(1));
5909
5910 // Remember that we legalized the chain.
5911 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005912 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005913 std::swap(Lo, Hi);
5914 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005915 MVT::ValueType EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005916
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005917 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5918 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005919 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5920 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5921 SVOffset, isVolatile, Alignment);
5922 // Remember that we legalized the chain.
5923 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5924 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5925 break;
5926 }
5927
5928 if (EVT == NVT)
5929 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5930 SVOffset, isVolatile, Alignment);
5931 else
5932 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5933 SVOffset, EVT, isVolatile,
5934 Alignment);
5935
5936 // Remember that we legalized the chain.
5937 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5938
5939 if (ExtType == ISD::SEXTLOAD) {
5940 // The high part is obtained by SRA'ing all but one of the bits of the
5941 // lo part.
5942 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5943 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5944 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5945 } else if (ExtType == ISD::ZEXTLOAD) {
5946 // The high part is just a zero.
5947 Hi = DAG.getConstant(0, NVT);
5948 } else /* if (ExtType == ISD::EXTLOAD) */ {
5949 // The high part is undefined.
5950 Hi = DAG.getNode(ISD::UNDEF, NVT);
5951 }
5952 }
5953 break;
5954 }
5955 case ISD::AND:
5956 case ISD::OR:
5957 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5958 SDOperand LL, LH, RL, RH;
5959 ExpandOp(Node->getOperand(0), LL, LH);
5960 ExpandOp(Node->getOperand(1), RL, RH);
5961 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5962 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5963 break;
5964 }
5965 case ISD::SELECT: {
5966 SDOperand LL, LH, RL, RH;
5967 ExpandOp(Node->getOperand(1), LL, LH);
5968 ExpandOp(Node->getOperand(2), RL, RH);
5969 if (getTypeAction(NVT) == Expand)
5970 NVT = TLI.getTypeToExpandTo(NVT);
5971 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5972 if (VT != MVT::f32)
5973 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5974 break;
5975 }
5976 case ISD::SELECT_CC: {
5977 SDOperand TL, TH, FL, FH;
5978 ExpandOp(Node->getOperand(2), TL, TH);
5979 ExpandOp(Node->getOperand(3), FL, FH);
5980 if (getTypeAction(NVT) == Expand)
5981 NVT = TLI.getTypeToExpandTo(NVT);
5982 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5983 Node->getOperand(1), TL, FL, Node->getOperand(4));
5984 if (VT != MVT::f32)
5985 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5986 Node->getOperand(1), TH, FH, Node->getOperand(4));
5987 break;
5988 }
5989 case ISD::ANY_EXTEND:
5990 // The low part is any extension of the input (which degenerates to a copy).
5991 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5992 // The high part is undefined.
5993 Hi = DAG.getNode(ISD::UNDEF, NVT);
5994 break;
5995 case ISD::SIGN_EXTEND: {
5996 // The low part is just a sign extension of the input (which degenerates to
5997 // a copy).
5998 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
5999
6000 // The high part is obtained by SRA'ing all but one of the bits of the lo
6001 // part.
6002 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
6003 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6004 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6005 break;
6006 }
6007 case ISD::ZERO_EXTEND:
6008 // The low part is just a zero extension of the input (which degenerates to
6009 // a copy).
6010 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6011
6012 // The high part is just a zero.
6013 Hi = DAG.getConstant(0, NVT);
6014 break;
6015
6016 case ISD::TRUNCATE: {
6017 // The input value must be larger than this value. Expand *it*.
6018 SDOperand NewLo;
6019 ExpandOp(Node->getOperand(0), NewLo, Hi);
6020
6021 // The low part is now either the right size, or it is closer. If not the
6022 // right size, make an illegal truncate so we recursively expand it.
6023 if (NewLo.getValueType() != Node->getValueType(0))
6024 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6025 ExpandOp(NewLo, Lo, Hi);
6026 break;
6027 }
6028
6029 case ISD::BIT_CONVERT: {
6030 SDOperand Tmp;
6031 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6032 // If the target wants to, allow it to lower this itself.
6033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6034 case Expand: assert(0 && "cannot expand FP!");
6035 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6036 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6037 }
6038 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6039 }
6040
6041 // f32 / f64 must be expanded to i32 / i64.
6042 if (VT == MVT::f32 || VT == MVT::f64) {
6043 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6044 if (getTypeAction(NVT) == Expand)
6045 ExpandOp(Lo, Lo, Hi);
6046 break;
6047 }
6048
6049 // If source operand will be expanded to the same type as VT, i.e.
6050 // i64 <- f64, i32 <- f32, expand the source operand instead.
6051 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6052 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6053 ExpandOp(Node->getOperand(0), Lo, Hi);
6054 break;
6055 }
6056
6057 // Turn this into a load/store pair by default.
6058 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006059 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060
6061 ExpandOp(Tmp, Lo, Hi);
6062 break;
6063 }
6064
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006065 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006066 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6067 TargetLowering::Custom &&
6068 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006069 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6070 assert(Tmp.Val && "Node must be custom expanded!");
6071 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006073 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006074 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006075 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006076
6077 // These operators cannot be expanded directly, emit them as calls to
6078 // library functions.
6079 case ISD::FP_TO_SINT: {
6080 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6081 SDOperand Op;
6082 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6083 case Expand: assert(0 && "cannot expand FP!");
6084 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6085 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6086 }
6087
6088 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6089
6090 // Now that the custom expander is done, expand the result, which is still
6091 // VT.
6092 if (Op.Val) {
6093 ExpandOp(Op, Lo, Hi);
6094 break;
6095 }
6096 }
6097
Dale Johannesenac77b272007-10-05 20:04:43 +00006098 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006099 if (Node->getOperand(0).getValueType() == MVT::f32)
6100 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006101 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006102 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006103 else if (Node->getOperand(0).getValueType() == MVT::f80)
6104 LC = RTLIB::FPTOSINT_F80_I64;
6105 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6106 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006107 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6108 false/*sign irrelevant*/, Hi);
6109 break;
6110 }
6111
6112 case ISD::FP_TO_UINT: {
6113 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6114 SDOperand Op;
6115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6116 case Expand: assert(0 && "cannot expand FP!");
6117 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6118 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6119 }
6120
6121 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6122
6123 // Now that the custom expander is done, expand the result.
6124 if (Op.Val) {
6125 ExpandOp(Op, Lo, Hi);
6126 break;
6127 }
6128 }
6129
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00006130 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006131 if (Node->getOperand(0).getValueType() == MVT::f32)
6132 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen4e1cf5d2007-09-28 18:44:17 +00006133 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006134 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006135 else if (Node->getOperand(0).getValueType() == MVT::f80)
6136 LC = RTLIB::FPTOUINT_F80_I64;
6137 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6138 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006139 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6140 false/*sign irrelevant*/, Hi);
6141 break;
6142 }
6143
6144 case ISD::SHL: {
6145 // If the target wants custom lowering, do so.
6146 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6147 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6148 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6149 Op = TLI.LowerOperation(Op, DAG);
6150 if (Op.Val) {
6151 // Now that the custom expander is done, expand the result, which is
6152 // still VT.
6153 ExpandOp(Op, Lo, Hi);
6154 break;
6155 }
6156 }
6157
6158 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6159 // this X << 1 as X+X.
6160 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6161 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6162 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6163 SDOperand LoOps[2], HiOps[3];
6164 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6165 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6166 LoOps[1] = LoOps[0];
6167 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6168
6169 HiOps[1] = HiOps[0];
6170 HiOps[2] = Lo.getValue(1);
6171 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6172 break;
6173 }
6174 }
6175
6176 // If we can emit an efficient shift operation, do so now.
6177 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6178 break;
6179
6180 // If this target supports SHL_PARTS, use it.
6181 TargetLowering::LegalizeAction Action =
6182 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6183 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6184 Action == TargetLowering::Custom) {
6185 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6186 break;
6187 }
6188
6189 // Otherwise, emit a libcall.
6190 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6191 false/*left shift=unsigned*/, Hi);
6192 break;
6193 }
6194
6195 case ISD::SRA: {
6196 // If the target wants custom lowering, do so.
6197 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6198 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6199 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6200 Op = TLI.LowerOperation(Op, DAG);
6201 if (Op.Val) {
6202 // Now that the custom expander is done, expand the result, which is
6203 // still VT.
6204 ExpandOp(Op, Lo, Hi);
6205 break;
6206 }
6207 }
6208
6209 // If we can emit an efficient shift operation, do so now.
6210 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6211 break;
6212
6213 // If this target supports SRA_PARTS, use it.
6214 TargetLowering::LegalizeAction Action =
6215 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6216 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6217 Action == TargetLowering::Custom) {
6218 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6219 break;
6220 }
6221
6222 // Otherwise, emit a libcall.
6223 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6224 true/*ashr is signed*/, Hi);
6225 break;
6226 }
6227
6228 case ISD::SRL: {
6229 // If the target wants custom lowering, do so.
6230 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6231 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6232 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6233 Op = TLI.LowerOperation(Op, DAG);
6234 if (Op.Val) {
6235 // Now that the custom expander is done, expand the result, which is
6236 // still VT.
6237 ExpandOp(Op, Lo, Hi);
6238 break;
6239 }
6240 }
6241
6242 // If we can emit an efficient shift operation, do so now.
6243 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6244 break;
6245
6246 // If this target supports SRL_PARTS, use it.
6247 TargetLowering::LegalizeAction Action =
6248 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6249 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6250 Action == TargetLowering::Custom) {
6251 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6252 break;
6253 }
6254
6255 // Otherwise, emit a libcall.
6256 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6257 false/*lshr is unsigned*/, Hi);
6258 break;
6259 }
6260
6261 case ISD::ADD:
6262 case ISD::SUB: {
6263 // If the target wants to custom expand this, let them.
6264 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6265 TargetLowering::Custom) {
6266 Op = TLI.LowerOperation(Op, DAG);
6267 if (Op.Val) {
6268 ExpandOp(Op, Lo, Hi);
6269 break;
6270 }
6271 }
6272
6273 // Expand the subcomponents.
6274 SDOperand LHSL, LHSH, RHSL, RHSH;
6275 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6276 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6277 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6278 SDOperand LoOps[2], HiOps[3];
6279 LoOps[0] = LHSL;
6280 LoOps[1] = RHSL;
6281 HiOps[0] = LHSH;
6282 HiOps[1] = RHSH;
6283 if (Node->getOpcode() == ISD::ADD) {
6284 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6285 HiOps[2] = Lo.getValue(1);
6286 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6287 } else {
6288 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6289 HiOps[2] = Lo.getValue(1);
6290 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6291 }
6292 break;
6293 }
6294
6295 case ISD::ADDC:
6296 case ISD::SUBC: {
6297 // Expand the subcomponents.
6298 SDOperand LHSL, LHSH, RHSL, RHSH;
6299 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6300 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6301 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6302 SDOperand LoOps[2] = { LHSL, RHSL };
6303 SDOperand HiOps[3] = { LHSH, RHSH };
6304
6305 if (Node->getOpcode() == ISD::ADDC) {
6306 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6307 HiOps[2] = Lo.getValue(1);
6308 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6309 } else {
6310 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6311 HiOps[2] = Lo.getValue(1);
6312 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6313 }
6314 // Remember that we legalized the flag.
6315 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6316 break;
6317 }
6318 case ISD::ADDE:
6319 case ISD::SUBE: {
6320 // Expand the subcomponents.
6321 SDOperand LHSL, LHSH, RHSL, RHSH;
6322 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6323 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6324 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6325 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6326 SDOperand HiOps[3] = { LHSH, RHSH };
6327
6328 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6329 HiOps[2] = Lo.getValue(1);
6330 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6331
6332 // Remember that we legalized the flag.
6333 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6334 break;
6335 }
6336 case ISD::MUL: {
6337 // If the target wants to custom expand this, let them.
6338 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6339 SDOperand New = TLI.LowerOperation(Op, DAG);
6340 if (New.Val) {
6341 ExpandOp(New, Lo, Hi);
6342 break;
6343 }
6344 }
6345
6346 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6347 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006348 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6349 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6350 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006351 SDOperand LL, LH, RL, RH;
6352 ExpandOp(Node->getOperand(0), LL, LH);
6353 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006354 unsigned OuterBitSize = Op.getValueSizeInBits();
6355 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006356 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6357 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman07961cd2008-02-25 21:11:39 +00006358 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6359 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6360 DAG.MaskedValueIsZero(Op.getOperand(1),
6361 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohman5a199552007-10-08 18:33:35 +00006362 // The inputs are both zero-extended.
6363 if (HasUMUL_LOHI) {
6364 // We can emit a umul_lohi.
6365 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6366 Hi = SDOperand(Lo.Val, 1);
6367 break;
6368 }
6369 if (HasMULHU) {
6370 // We can emit a mulhu+mul.
6371 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6372 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6373 break;
6374 }
Dan Gohman5a199552007-10-08 18:33:35 +00006375 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006376 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006377 // The input values are both sign-extended.
6378 if (HasSMUL_LOHI) {
6379 // We can emit a smul_lohi.
6380 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6381 Hi = SDOperand(Lo.Val, 1);
6382 break;
6383 }
6384 if (HasMULHS) {
6385 // We can emit a mulhs+mul.
6386 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6387 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6388 break;
6389 }
6390 }
6391 if (HasUMUL_LOHI) {
6392 // Lo,Hi = umul LHS, RHS.
6393 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6394 DAG.getVTList(NVT, NVT), LL, RL);
6395 Lo = UMulLOHI;
6396 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006397 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6398 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6399 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6400 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6401 break;
6402 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006403 if (HasMULHU) {
6404 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6405 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6406 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6407 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6408 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6409 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6410 break;
6411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006412 }
6413
Dan Gohman5a199552007-10-08 18:33:35 +00006414 // If nothing else, we can make a libcall.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006415 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6416 false/*sign irrelevant*/, Hi);
6417 break;
6418 }
6419 case ISD::SDIV:
6420 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6421 break;
6422 case ISD::UDIV:
6423 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6424 break;
6425 case ISD::SREM:
6426 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6427 break;
6428 case ISD::UREM:
6429 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6430 break;
6431
6432 case ISD::FADD:
Duncan Sands37a3f472008-01-10 10:28:30 +00006433 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6434 RTLIB::ADD_F64,
6435 RTLIB::ADD_F80,
6436 RTLIB::ADD_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006437 Node, false, Hi);
6438 break;
6439 case ISD::FSUB:
Duncan Sands37a3f472008-01-10 10:28:30 +00006440 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6441 RTLIB::SUB_F64,
6442 RTLIB::SUB_F80,
6443 RTLIB::SUB_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 Node, false, Hi);
6445 break;
6446 case ISD::FMUL:
Duncan Sands37a3f472008-01-10 10:28:30 +00006447 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6448 RTLIB::MUL_F64,
6449 RTLIB::MUL_F80,
6450 RTLIB::MUL_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 Node, false, Hi);
6452 break;
6453 case ISD::FDIV:
Duncan Sands37a3f472008-01-10 10:28:30 +00006454 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6455 RTLIB::DIV_F64,
6456 RTLIB::DIV_F80,
6457 RTLIB::DIV_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 Node, false, Hi);
6459 break;
6460 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006461 if (VT == MVT::ppcf128) {
6462 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6463 Node->getOperand(0).getValueType()==MVT::f64);
6464 const uint64_t zero = 0;
6465 if (Node->getOperand(0).getValueType()==MVT::f32)
6466 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6467 else
6468 Hi = Node->getOperand(0);
6469 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6470 break;
6471 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
6473 break;
6474 case ISD::FP_ROUND:
6475 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
6476 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006477 case ISD::FPOWI:
Duncan Sands37a3f472008-01-10 10:28:30 +00006478 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6479 RTLIB::POWI_F64,
6480 RTLIB::POWI_F80,
6481 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006482 Node, false, Hi);
6483 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006484 case ISD::FSQRT:
6485 case ISD::FSIN:
6486 case ISD::FCOS: {
6487 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6488 switch(Node->getOpcode()) {
6489 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006490 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6491 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492 break;
6493 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006494 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6495 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006496 break;
6497 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006498 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6499 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006500 break;
6501 default: assert(0 && "Unreachable!");
6502 }
6503 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
6504 break;
6505 }
6506 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006507 if (VT == MVT::ppcf128) {
6508 SDOperand Tmp;
6509 ExpandOp(Node->getOperand(0), Lo, Tmp);
6510 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6511 // lo = hi==fabs(hi) ? lo : -lo;
6512 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6513 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6514 DAG.getCondCode(ISD::SETEQ));
6515 break;
6516 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517 SDOperand Mask = (VT == MVT::f64)
6518 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6519 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6520 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6521 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6522 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6523 if (getTypeAction(NVT) == Expand)
6524 ExpandOp(Lo, Lo, Hi);
6525 break;
6526 }
6527 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006528 if (VT == MVT::ppcf128) {
6529 ExpandOp(Node->getOperand(0), Lo, Hi);
6530 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6531 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6532 break;
6533 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006534 SDOperand Mask = (VT == MVT::f64)
6535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6536 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6537 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6538 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6539 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6540 if (getTypeAction(NVT) == Expand)
6541 ExpandOp(Lo, Lo, Hi);
6542 break;
6543 }
6544 case ISD::FCOPYSIGN: {
6545 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6546 if (getTypeAction(NVT) == Expand)
6547 ExpandOp(Lo, Lo, Hi);
6548 break;
6549 }
6550 case ISD::SINT_TO_FP:
6551 case ISD::UINT_TO_FP: {
6552 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6553 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006554 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohman84d00962008-02-25 21:39:34 +00006555 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00006556 if (isSigned) {
6557 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6558 Node->getOperand(0)));
6559 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6560 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00006561 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00006562 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6563 Node->getOperand(0)));
6564 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6565 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006566 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006567 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6568 DAG.getConstant(0, MVT::i32),
6569 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6570 DAG.getConstantFP(
6571 APFloat(APInt(128, 2, TwoE32)),
6572 MVT::ppcf128)),
6573 Hi,
6574 DAG.getCondCode(ISD::SETLT)),
6575 Lo, Hi);
6576 }
6577 break;
6578 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006579 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6580 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00006581 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006582 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6583 Lo, Hi);
6584 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6585 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6586 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6587 DAG.getConstant(0, MVT::i64),
6588 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6589 DAG.getConstantFP(
6590 APFloat(APInt(128, 2, TwoE64)),
6591 MVT::ppcf128)),
6592 Hi,
6593 DAG.getCondCode(ISD::SETLT)),
6594 Lo, Hi);
6595 break;
6596 }
Evan Cheng20186812007-09-27 07:35:39 +00006597 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006598 if (Node->getOperand(0).getValueType() == MVT::i64) {
6599 if (VT == MVT::f32)
6600 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006601 else if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006602 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006603 else if (VT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00006604 assert(isSigned);
Dale Johannesenac77b272007-10-05 20:04:43 +00006605 LC = RTLIB::SINTTOFP_I64_F80;
6606 }
6607 else if (VT == MVT::ppcf128) {
6608 assert(isSigned);
6609 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006610 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006611 } else {
6612 if (VT == MVT::f32)
6613 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
6614 else
6615 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
6616 }
6617
6618 // Promote the operand if needed.
6619 if (getTypeAction(SrcVT) == Promote) {
6620 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6621 Tmp = isSigned
6622 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6623 DAG.getValueType(SrcVT))
6624 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6625 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6626 }
6627
6628 const char *LibCall = TLI.getLibcallName(LC);
6629 if (LibCall)
6630 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6631 else {
6632 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6633 Node->getOperand(0));
6634 if (getTypeAction(Lo.getValueType()) == Expand)
6635 ExpandOp(Lo, Lo, Hi);
6636 }
6637 break;
6638 }
6639 }
6640
6641 // Make sure the resultant values have been legalized themselves, unless this
6642 // is a type that requires multi-step expansion.
6643 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6644 Lo = LegalizeOp(Lo);
6645 if (Hi.Val)
6646 // Don't legalize the high part if it is expanded to a single node.
6647 Hi = LegalizeOp(Hi);
6648 }
6649
6650 // Remember in a map if the values will be reused later.
6651 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6652 assert(isNew && "Value already expanded?!?");
6653}
6654
6655/// SplitVectorOp - Given an operand of vector type, break it down into
6656/// two smaller values, still of vector type.
6657void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6658 SDOperand &Hi) {
6659 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6660 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006661 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006662 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006663
Dan Gohmana0763d92007-09-24 15:54:53 +00006664 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006665
6666 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6667 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6668
6669 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6670 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006672 // See if we already split it.
6673 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6674 = SplitNodes.find(Op);
6675 if (I != SplitNodes.end()) {
6676 Lo = I->second.first;
6677 Hi = I->second.second;
6678 return;
6679 }
6680
6681 switch (Node->getOpcode()) {
6682 default:
6683#ifndef NDEBUG
6684 Node->dump(&DAG);
6685#endif
6686 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006687 case ISD::UNDEF:
6688 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6689 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6690 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006691 case ISD::BUILD_PAIR:
6692 Lo = Node->getOperand(0);
6693 Hi = Node->getOperand(1);
6694 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006695 case ISD::INSERT_VECTOR_ELT: {
6696 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6697 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6698 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006699 if (Index < NewNumElts_Lo)
6700 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006701 DAG.getConstant(Index, TLI.getPointerTy()));
6702 else
Nate Begeman4a365ad2007-11-15 21:15:26 +00006703 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6704 DAG.getConstant(Index - NewNumElts_Lo,
6705 TLI.getPointerTy()));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006706 break;
6707 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006708 case ISD::VECTOR_SHUFFLE: {
6709 // Build the low part.
6710 SDOperand Mask = Node->getOperand(2);
6711 SmallVector<SDOperand, 8> Ops;
6712 MVT::ValueType PtrVT = TLI.getPointerTy();
6713
6714 // Insert all of the elements from the input that are needed. We use
6715 // buildvector of extractelement here because the input vectors will have
6716 // to be legalized, so this makes the code simpler.
6717 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6718 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6719 SDOperand InVec = Node->getOperand(0);
6720 if (Idx >= NumElements) {
6721 InVec = Node->getOperand(1);
6722 Idx -= NumElements;
6723 }
6724 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6725 DAG.getConstant(Idx, PtrVT)));
6726 }
6727 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6728 Ops.clear();
6729
6730 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6731 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6732 SDOperand InVec = Node->getOperand(0);
6733 if (Idx >= NumElements) {
6734 InVec = Node->getOperand(1);
6735 Idx -= NumElements;
6736 }
6737 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6738 DAG.getConstant(Idx, PtrVT)));
6739 }
6740 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6741 break;
6742 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006743 case ISD::BUILD_VECTOR: {
6744 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006745 Node->op_begin()+NewNumElts_Lo);
6746 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006747
Nate Begeman4a365ad2007-11-15 21:15:26 +00006748 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006749 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006750 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751 break;
6752 }
6753 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006754 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006755 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6756 if (NewNumSubvectors == 1) {
6757 Lo = Node->getOperand(0);
6758 Hi = Node->getOperand(1);
6759 } else {
6760 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6761 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006762 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006763
6764 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6765 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006766 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006767 }
6768 break;
6769 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006770 case ISD::SELECT: {
6771 SDOperand Cond = Node->getOperand(0);
6772
6773 SDOperand LL, LH, RL, RH;
6774 SplitVectorOp(Node->getOperand(1), LL, LH);
6775 SplitVectorOp(Node->getOperand(2), RL, RH);
6776
6777 if (MVT::isVector(Cond.getValueType())) {
6778 // Handle a vector merge.
6779 SDOperand CL, CH;
6780 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006781 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6782 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006783 } else {
6784 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006785 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6786 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006787 }
6788 break;
6789 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006790 case ISD::ADD:
6791 case ISD::SUB:
6792 case ISD::MUL:
6793 case ISD::FADD:
6794 case ISD::FSUB:
6795 case ISD::FMUL:
6796 case ISD::SDIV:
6797 case ISD::UDIV:
6798 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006799 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006800 case ISD::AND:
6801 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006802 case ISD::XOR:
6803 case ISD::UREM:
6804 case ISD::SREM:
6805 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006806 SDOperand LL, LH, RL, RH;
6807 SplitVectorOp(Node->getOperand(0), LL, LH);
6808 SplitVectorOp(Node->getOperand(1), RL, RH);
6809
Nate Begeman4a365ad2007-11-15 21:15:26 +00006810 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6811 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006812 break;
6813 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006814 case ISD::FPOWI: {
6815 SDOperand L, H;
6816 SplitVectorOp(Node->getOperand(0), L, H);
6817
Nate Begeman4a365ad2007-11-15 21:15:26 +00006818 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6819 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006820 break;
6821 }
6822 case ISD::CTTZ:
6823 case ISD::CTLZ:
6824 case ISD::CTPOP:
6825 case ISD::FNEG:
6826 case ISD::FABS:
6827 case ISD::FSQRT:
6828 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006829 case ISD::FCOS:
6830 case ISD::FP_TO_SINT:
6831 case ISD::FP_TO_UINT:
6832 case ISD::SINT_TO_FP:
6833 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006834 SDOperand L, H;
6835 SplitVectorOp(Node->getOperand(0), L, H);
6836
Nate Begeman4a365ad2007-11-15 21:15:26 +00006837 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6838 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006839 break;
6840 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006841 case ISD::LOAD: {
6842 LoadSDNode *LD = cast<LoadSDNode>(Node);
6843 SDOperand Ch = LD->getChain();
6844 SDOperand Ptr = LD->getBasePtr();
6845 const Value *SV = LD->getSrcValue();
6846 int SVOffset = LD->getSrcValueOffset();
6847 unsigned Alignment = LD->getAlignment();
6848 bool isVolatile = LD->isVolatile();
6849
Nate Begeman4a365ad2007-11-15 21:15:26 +00006850 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6851 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006852 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006853 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006854 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006855 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006856 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006857
6858 // Build a factor node to remember that this load is independent of the
6859 // other one.
6860 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6861 Hi.getValue(1));
6862
6863 // Remember that we legalized the chain.
6864 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6865 break;
6866 }
6867 case ISD::BIT_CONVERT: {
6868 // We know the result is a vector. The input may be either a vector or a
6869 // scalar value.
6870 SDOperand InOp = Node->getOperand(0);
6871 if (!MVT::isVector(InOp.getValueType()) ||
6872 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6873 // The input is a scalar or single-element vector.
6874 // Lower to a store/load so that it can be split.
6875 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006876 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman20e37962008-02-11 18:58:42 +00006877 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006878
6879 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006880 InOp, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006881 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006882 FI->getIndex());
6883 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006884 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006885 FI->getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006886 }
6887 // Split the vector and convert each of the pieces now.
6888 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006889 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6890 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006891 break;
6892 }
6893 }
6894
6895 // Remember in a map if the values will be reused later.
6896 bool isNew =
6897 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6898 assert(isNew && "Value already split?!?");
6899}
6900
6901
6902/// ScalarizeVectorOp - Given an operand of single-element vector type
6903/// (e.g. v1f32), convert it into the equivalent operation that returns a
6904/// scalar (e.g. f32) value.
6905SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6906 assert(MVT::isVector(Op.getValueType()) &&
6907 "Bad ScalarizeVectorOp invocation!");
6908 SDNode *Node = Op.Val;
6909 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6910 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6911
6912 // See if we already scalarized it.
6913 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6914 if (I != ScalarizedNodes.end()) return I->second;
6915
6916 SDOperand Result;
6917 switch (Node->getOpcode()) {
6918 default:
6919#ifndef NDEBUG
6920 Node->dump(&DAG); cerr << "\n";
6921#endif
6922 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6923 case ISD::ADD:
6924 case ISD::FADD:
6925 case ISD::SUB:
6926 case ISD::FSUB:
6927 case ISD::MUL:
6928 case ISD::FMUL:
6929 case ISD::SDIV:
6930 case ISD::UDIV:
6931 case ISD::FDIV:
6932 case ISD::SREM:
6933 case ISD::UREM:
6934 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006935 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006936 case ISD::AND:
6937 case ISD::OR:
6938 case ISD::XOR:
6939 Result = DAG.getNode(Node->getOpcode(),
6940 NewVT,
6941 ScalarizeVectorOp(Node->getOperand(0)),
6942 ScalarizeVectorOp(Node->getOperand(1)));
6943 break;
6944 case ISD::FNEG:
6945 case ISD::FABS:
6946 case ISD::FSQRT:
6947 case ISD::FSIN:
6948 case ISD::FCOS:
6949 Result = DAG.getNode(Node->getOpcode(),
6950 NewVT,
6951 ScalarizeVectorOp(Node->getOperand(0)));
6952 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006953 case ISD::FPOWI:
6954 Result = DAG.getNode(Node->getOpcode(),
6955 NewVT,
6956 ScalarizeVectorOp(Node->getOperand(0)),
6957 Node->getOperand(1));
6958 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006959 case ISD::LOAD: {
6960 LoadSDNode *LD = cast<LoadSDNode>(Node);
6961 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6962 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6963
6964 const Value *SV = LD->getSrcValue();
6965 int SVOffset = LD->getSrcValueOffset();
6966 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6967 LD->isVolatile(), LD->getAlignment());
6968
6969 // Remember that we legalized the chain.
6970 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6971 break;
6972 }
6973 case ISD::BUILD_VECTOR:
6974 Result = Node->getOperand(0);
6975 break;
6976 case ISD::INSERT_VECTOR_ELT:
6977 // Returning the inserted scalar element.
6978 Result = Node->getOperand(1);
6979 break;
6980 case ISD::CONCAT_VECTORS:
6981 assert(Node->getOperand(0).getValueType() == NewVT &&
6982 "Concat of non-legal vectors not yet supported!");
6983 Result = Node->getOperand(0);
6984 break;
6985 case ISD::VECTOR_SHUFFLE: {
6986 // Figure out if the scalar is the LHS or RHS and return it.
6987 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6988 if (cast<ConstantSDNode>(EltNum)->getValue())
6989 Result = ScalarizeVectorOp(Node->getOperand(1));
6990 else
6991 Result = ScalarizeVectorOp(Node->getOperand(0));
6992 break;
6993 }
6994 case ISD::EXTRACT_SUBVECTOR:
6995 Result = Node->getOperand(0);
6996 assert(Result.getValueType() == NewVT);
6997 break;
6998 case ISD::BIT_CONVERT:
6999 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
7000 break;
7001 case ISD::SELECT:
7002 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7003 ScalarizeVectorOp(Op.getOperand(1)),
7004 ScalarizeVectorOp(Op.getOperand(2)));
7005 break;
7006 }
7007
7008 if (TLI.isTypeLegal(NewVT))
7009 Result = LegalizeOp(Result);
7010 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7011 assert(isNew && "Value already scalarized?");
7012 return Result;
7013}
7014
7015
7016// SelectionDAG::Legalize - This is the entry point for the file.
7017//
7018void SelectionDAG::Legalize() {
7019 if (ViewLegalizeDAGs) viewGraph();
7020
7021 /// run - This is the main entry point to this class.
7022 ///
7023 SelectionDAGLegalize(*this).LegalizeDAG();
7024}
7025