blob: 37dab7d7c165933949376122779a01210c2a82c2 [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 Johannesendc0ee192008-02-27 22:36:00 +0000577 if (VT == MVT::v8i16 || VT == MVT::v4i32 ||
578 VT == MVT::v2i64 || VT == MVT::v2f64 ||
579 VT == MVT::v4f32 || VT == MVT::v16i8 ||
580 VT == MVT::ppcf128)
581 intVT = MVT::i128;
582 else if (VT==MVT::f64 ||
583 VT == MVT::v8i8 || VT == MVT::v4i16 ||
584 VT == MVT::v2i32 || VT == MVT::v1i64 ||
585 VT == MVT::v2f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000586 intVT = MVT::i64;
587 else if (VT==MVT::f32)
588 intVT = MVT::i32;
589 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000590 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000591
592 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
593 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
594 SVOffset, ST->isVolatile(), Alignment);
595 }
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000596 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesendc0ee192008-02-27 22:36:00 +0000597 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen08275382007-09-08 19:29:23 +0000598 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000599 // Get the half-size VT
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000600 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000601 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000602 int IncrementSize = NumBits / 8;
603
604 // Divide the stored value in two parts.
605 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
606 SDOperand Lo = Val;
607 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
608
609 // Store the two parts
610 SDOperand Store1, Store2;
611 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
612 ST->getSrcValue(), SVOffset, NewStoredVT,
613 ST->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000616 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000617 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
618 ST->getSrcValue(), SVOffset + IncrementSize,
619 NewStoredVT, ST->isVolatile(), Alignment);
620
621 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
622}
623
624/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
625static
626SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
627 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000628 int SVOffset = LD->getSrcValueOffset();
629 SDOperand Chain = LD->getChain();
630 SDOperand Ptr = LD->getBasePtr();
631 MVT::ValueType VT = LD->getValueType(0);
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000632 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesendc0ee192008-02-27 22:36:00 +0000633 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen08275382007-09-08 19:29:23 +0000634 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000635 // then bitconvert to floating point or vector.
Dale Johannesen08275382007-09-08 19:29:23 +0000636 MVT::ValueType intVT;
Dale Johannesendc0ee192008-02-27 22:36:00 +0000637 if (LoadedVT == MVT::v8i16 || LoadedVT == MVT::v4i32 ||
638 LoadedVT == MVT::v2i64 || LoadedVT == MVT::v2f64 ||
639 LoadedVT == MVT::v4f32 || LoadedVT == MVT::v16i8 ||
640 LoadedVT == MVT::ppcf128)
641 intVT = MVT::i128;
642 else if (LoadedVT == MVT::f64 ||
643 LoadedVT == MVT::v8i8 || LoadedVT == MVT::v4i16 ||
644 LoadedVT == MVT::v2i32 || LoadedVT == MVT::v1i64 ||
645 LoadedVT == MVT::v2f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000646 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000647 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000648 intVT = MVT::i32;
649 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000650 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000651
652 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
653 SVOffset, LD->isVolatile(),
654 LD->getAlignment());
655 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesendc0ee192008-02-27 22:36:00 +0000656 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000657 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
658
659 SDOperand Ops[] = { Result, Chain };
660 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
661 Ops, 2);
662 }
Dale Johannesendc0ee192008-02-27 22:36:00 +0000663 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000664 "Unaligned load of unsupported type.");
665
Dale Johannesendc0ee192008-02-27 22:36:00 +0000666 // Compute the new VT that is half the size of the old one. This is an
667 // integer MVT.
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000668 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
669 MVT::ValueType NewLoadedVT;
Dale Johannesendc0ee192008-02-27 22:36:00 +0000670 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000671 NumBits >>= 1;
672
673 unsigned Alignment = LD->getAlignment();
674 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000675 ISD::LoadExtType HiExtType = LD->getExtensionType();
676
677 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
678 if (HiExtType == ISD::NON_EXTLOAD)
679 HiExtType = ISD::ZEXTLOAD;
680
681 // Load the value in two parts
682 SDOperand Lo, Hi;
683 if (TLI.isLittleEndian()) {
684 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
685 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
686 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
687 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
688 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
689 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000690 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000691 } else {
692 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
693 NewLoadedVT,LD->isVolatile(), Alignment);
694 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
695 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
696 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
697 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000698 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000699 }
700
701 // aggregate the two parts
702 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
703 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
704 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
705
706 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
707 Hi.getValue(1));
708
709 SDOperand Ops[] = { Result, TF };
710 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
711}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712
Dan Gohman6d05cac2007-10-11 23:57:53 +0000713/// UnrollVectorOp - We know that the given vector has a legal type, however
714/// the operation it performs is not legal and is an operation that we have
715/// no way of lowering. "Unroll" the vector, splitting out the scalars and
716/// operating on each element individually.
717SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
718 MVT::ValueType VT = Op.getValueType();
719 assert(isTypeLegal(VT) &&
720 "Caller should expand or promote operands that are not legal!");
721 assert(Op.Val->getNumValues() == 1 &&
722 "Can't unroll a vector with multiple results!");
723 unsigned NE = MVT::getVectorNumElements(VT);
724 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
725
726 SmallVector<SDOperand, 8> Scalars;
727 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
728 for (unsigned i = 0; i != NE; ++i) {
729 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
730 SDOperand Operand = Op.getOperand(j);
731 MVT::ValueType OperandVT = Operand.getValueType();
732 if (MVT::isVector(OperandVT)) {
733 // A vector operand; extract a single element.
734 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
735 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
736 OperandEltVT,
737 Operand,
738 DAG.getConstant(i, MVT::i32));
739 } else {
740 // A scalar operand; just use it as is.
741 Operands[j] = Operand;
742 }
743 }
744 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
745 &Operands[0], Operands.size()));
746 }
747
748 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
749}
750
Duncan Sands37a3f472008-01-10 10:28:30 +0000751/// GetFPLibCall - Return the right libcall for the given floating point type.
752static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
753 RTLIB::Libcall Call_F32,
754 RTLIB::Libcall Call_F64,
755 RTLIB::Libcall Call_F80,
756 RTLIB::Libcall Call_PPCF128) {
757 return
758 VT == MVT::f32 ? Call_F32 :
759 VT == MVT::f64 ? Call_F64 :
760 VT == MVT::f80 ? Call_F80 :
761 VT == MVT::ppcf128 ? Call_PPCF128 :
762 RTLIB::UNKNOWN_LIBCALL;
763}
764
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765/// LegalizeOp - We know that the specified value has a legal type, and
766/// that its operands are legal. Now ensure that the operation itself
767/// is legal, recursively ensuring that the operands' operations remain
768/// legal.
769SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000770 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
771 return Op;
772
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 assert(isTypeLegal(Op.getValueType()) &&
774 "Caller should expand or promote operands that are not legal!");
775 SDNode *Node = Op.Val;
776
777 // If this operation defines any values that cannot be represented in a
778 // register on this target, make sure to expand or promote them.
779 if (Node->getNumValues() > 1) {
780 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
781 if (getTypeAction(Node->getValueType(i)) != Legal) {
782 HandleOp(Op.getValue(i));
783 assert(LegalizedNodes.count(Op) &&
784 "Handling didn't add legal operands!");
785 return LegalizedNodes[Op];
786 }
787 }
788
789 // Note that LegalizeOp may be reentered even from single-use nodes, which
790 // means that we always must cache transformed nodes.
791 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
792 if (I != LegalizedNodes.end()) return I->second;
793
794 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
795 SDOperand Result = Op;
796 bool isCustom = false;
797
798 switch (Node->getOpcode()) {
799 case ISD::FrameIndex:
800 case ISD::EntryToken:
801 case ISD::Register:
802 case ISD::BasicBlock:
803 case ISD::TargetFrameIndex:
804 case ISD::TargetJumpTable:
805 case ISD::TargetConstant:
806 case ISD::TargetConstantFP:
807 case ISD::TargetConstantPool:
808 case ISD::TargetGlobalAddress:
809 case ISD::TargetGlobalTLSAddress:
810 case ISD::TargetExternalSymbol:
811 case ISD::VALUETYPE:
812 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000813 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 case ISD::STRING:
815 case ISD::CONDCODE:
816 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000817 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 "This must be legal!");
819 break;
820 default:
821 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
822 // If this is a target node, legalize it by legalizing the operands then
823 // passing it through.
824 SmallVector<SDOperand, 8> Ops;
825 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
826 Ops.push_back(LegalizeOp(Node->getOperand(i)));
827
828 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
829
830 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
831 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
832 return Result.getValue(Op.ResNo);
833 }
834 // Otherwise this is an unhandled builtin node. splat.
835#ifndef NDEBUG
836 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
837#endif
838 assert(0 && "Do not know how to legalize this operator!");
839 abort();
840 case ISD::GLOBAL_OFFSET_TABLE:
841 case ISD::GlobalAddress:
842 case ISD::GlobalTLSAddress:
843 case ISD::ExternalSymbol:
844 case ISD::ConstantPool:
845 case ISD::JumpTable: // Nothing to do.
846 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
847 default: assert(0 && "This action is not supported yet!");
848 case TargetLowering::Custom:
849 Tmp1 = TLI.LowerOperation(Op, DAG);
850 if (Tmp1.Val) Result = Tmp1;
851 // FALLTHROUGH if the target doesn't want to lower this op after all.
852 case TargetLowering::Legal:
853 break;
854 }
855 break;
856 case ISD::FRAMEADDR:
857 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 // The only option for these nodes is to custom lower them. If the target
859 // does not custom lower them, then return zero.
860 Tmp1 = TLI.LowerOperation(Op, DAG);
861 if (Tmp1.Val)
862 Result = Tmp1;
863 else
864 Result = DAG.getConstant(0, TLI.getPointerTy());
865 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000866 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000867 MVT::ValueType VT = Node->getValueType(0);
868 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
869 default: assert(0 && "This action is not supported yet!");
870 case TargetLowering::Custom:
871 Result = TLI.LowerOperation(Op, DAG);
872 if (Result.Val) break;
873 // Fall Thru
874 case TargetLowering::Legal:
875 Result = DAG.getConstant(0, VT);
876 break;
877 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000878 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000879 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 case ISD::EXCEPTIONADDR: {
881 Tmp1 = LegalizeOp(Node->getOperand(0));
882 MVT::ValueType VT = Node->getValueType(0);
883 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
884 default: assert(0 && "This action is not supported yet!");
885 case TargetLowering::Expand: {
886 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000887 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 }
889 break;
890 case TargetLowering::Custom:
891 Result = TLI.LowerOperation(Op, DAG);
892 if (Result.Val) break;
893 // Fall Thru
894 case TargetLowering::Legal: {
895 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
896 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000897 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 break;
899 }
900 }
901 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000902 if (Result.Val->getNumValues() == 1) break;
903
904 assert(Result.Val->getNumValues() == 2 &&
905 "Cannot return more than two values!");
906
907 // Since we produced two values, make sure to remember that we
908 // legalized both of them.
909 Tmp1 = LegalizeOp(Result);
910 Tmp2 = LegalizeOp(Result.getValue(1));
911 AddLegalizedOperand(Op.getValue(0), Tmp1);
912 AddLegalizedOperand(Op.getValue(1), Tmp2);
913 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 case ISD::EHSELECTION: {
915 Tmp1 = LegalizeOp(Node->getOperand(0));
916 Tmp2 = LegalizeOp(Node->getOperand(1));
917 MVT::ValueType VT = Node->getValueType(0);
918 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
919 default: assert(0 && "This action is not supported yet!");
920 case TargetLowering::Expand: {
921 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000922 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 }
924 break;
925 case TargetLowering::Custom:
926 Result = TLI.LowerOperation(Op, DAG);
927 if (Result.Val) break;
928 // Fall Thru
929 case TargetLowering::Legal: {
930 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
931 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000932 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 break;
934 }
935 }
936 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000937 if (Result.Val->getNumValues() == 1) break;
938
939 assert(Result.Val->getNumValues() == 2 &&
940 "Cannot return more than two values!");
941
942 // Since we produced two values, make sure to remember that we
943 // legalized both of them.
944 Tmp1 = LegalizeOp(Result);
945 Tmp2 = LegalizeOp(Result.getValue(1));
946 AddLegalizedOperand(Op.getValue(0), Tmp1);
947 AddLegalizedOperand(Op.getValue(1), Tmp2);
948 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 case ISD::EH_RETURN: {
950 MVT::ValueType VT = Node->getValueType(0);
951 // The only "good" option for this node is to custom lower it.
952 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
953 default: assert(0 && "This action is not supported at all!");
954 case TargetLowering::Custom:
955 Result = TLI.LowerOperation(Op, DAG);
956 if (Result.Val) break;
957 // Fall Thru
958 case TargetLowering::Legal:
959 // Target does not know, how to lower this, lower to noop
960 Result = LegalizeOp(Node->getOperand(0));
961 break;
962 }
963 }
964 break;
965 case ISD::AssertSext:
966 case ISD::AssertZext:
967 Tmp1 = LegalizeOp(Node->getOperand(0));
968 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
969 break;
970 case ISD::MERGE_VALUES:
971 // Legalize eliminates MERGE_VALUES nodes.
972 Result = Node->getOperand(Op.ResNo);
973 break;
974 case ISD::CopyFromReg:
975 Tmp1 = LegalizeOp(Node->getOperand(0));
976 Result = Op.getValue(0);
977 if (Node->getNumValues() == 2) {
978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
979 } else {
980 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
981 if (Node->getNumOperands() == 3) {
982 Tmp2 = LegalizeOp(Node->getOperand(2));
983 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
984 } else {
985 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
986 }
987 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
988 }
989 // Since CopyFromReg produces two values, make sure to remember that we
990 // legalized both of them.
991 AddLegalizedOperand(Op.getValue(0), Result);
992 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
993 return Result.getValue(Op.ResNo);
994 case ISD::UNDEF: {
995 MVT::ValueType VT = Op.getValueType();
996 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
997 default: assert(0 && "This action is not supported yet!");
998 case TargetLowering::Expand:
999 if (MVT::isInteger(VT))
1000 Result = DAG.getConstant(0, VT);
1001 else if (MVT::isFloatingPoint(VT))
Dale Johannesen20b76352007-09-26 17:26:49 +00001002 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
1003 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 else
1005 assert(0 && "Unknown value type!");
1006 break;
1007 case TargetLowering::Legal:
1008 break;
1009 }
1010 break;
1011 }
1012
1013 case ISD::INTRINSIC_W_CHAIN:
1014 case ISD::INTRINSIC_WO_CHAIN:
1015 case ISD::INTRINSIC_VOID: {
1016 SmallVector<SDOperand, 8> Ops;
1017 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1018 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1019 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1020
1021 // Allow the target to custom lower its intrinsics if it wants to.
1022 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1023 TargetLowering::Custom) {
1024 Tmp3 = TLI.LowerOperation(Result, DAG);
1025 if (Tmp3.Val) Result = Tmp3;
1026 }
1027
1028 if (Result.Val->getNumValues() == 1) break;
1029
1030 // Must have return value and chain result.
1031 assert(Result.Val->getNumValues() == 2 &&
1032 "Cannot return more than two values!");
1033
1034 // Since loads produce two values, make sure to remember that we
1035 // legalized both of them.
1036 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1037 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1038 return Result.getValue(Op.ResNo);
1039 }
1040
1041 case ISD::LOCATION:
1042 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1044
1045 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1046 case TargetLowering::Promote:
1047 default: assert(0 && "This action is not supported yet!");
1048 case TargetLowering::Expand: {
1049 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1050 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
1051 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
1052
1053 if (MMI && (useDEBUG_LOC || useLABEL)) {
1054 const std::string &FName =
1055 cast<StringSDNode>(Node->getOperand(3))->getValue();
1056 const std::string &DirName =
1057 cast<StringSDNode>(Node->getOperand(4))->getValue();
1058 unsigned SrcFile = MMI->RecordSource(DirName, FName);
1059
1060 SmallVector<SDOperand, 8> Ops;
1061 Ops.push_back(Tmp1); // chain
1062 SDOperand LineOp = Node->getOperand(1);
1063 SDOperand ColOp = Node->getOperand(2);
1064
1065 if (useDEBUG_LOC) {
1066 Ops.push_back(LineOp); // line #
1067 Ops.push_back(ColOp); // col #
1068 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
1069 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
1070 } else {
1071 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1072 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng69eda822008-02-01 02:05:57 +00001073 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng13d1c292008-01-31 09:59:15 +00001075 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1076 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 }
1078 } else {
1079 Result = Tmp1; // chain
1080 }
1081 break;
1082 }
1083 case TargetLowering::Legal:
1084 if (Tmp1 != Node->getOperand(0) ||
1085 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
1086 SmallVector<SDOperand, 8> Ops;
1087 Ops.push_back(Tmp1);
1088 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1089 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1090 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1091 } else {
1092 // Otherwise promote them.
1093 Ops.push_back(PromoteOp(Node->getOperand(1)));
1094 Ops.push_back(PromoteOp(Node->getOperand(2)));
1095 }
1096 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1097 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1098 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1099 }
1100 break;
1101 }
1102 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001103
1104 case ISD::DECLARE:
1105 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1106 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1107 default: assert(0 && "This action is not supported yet!");
1108 case TargetLowering::Legal:
1109 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1110 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1111 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1112 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1113 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001114 case TargetLowering::Expand:
1115 Result = LegalizeOp(Node->getOperand(0));
1116 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001117 }
1118 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119
1120 case ISD::DEBUG_LOC:
1121 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1122 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1123 default: assert(0 && "This action is not supported yet!");
1124 case TargetLowering::Legal:
1125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1126 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1127 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1128 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1129 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1130 break;
1131 }
1132 break;
1133
1134 case ISD::LABEL:
Evan Cheng13d1c292008-01-31 09:59:15 +00001135 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1137 default: assert(0 && "This action is not supported yet!");
1138 case TargetLowering::Legal:
1139 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1140 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng13d1c292008-01-31 09:59:15 +00001141 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1142 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 break;
1144 case TargetLowering::Expand:
1145 Result = LegalizeOp(Node->getOperand(0));
1146 break;
1147 }
1148 break;
1149
Andrew Lenharth785610d2008-02-16 01:24:58 +00001150 case ISD::MEMBARRIER: {
1151 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001152 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1153 default: assert(0 && "This action is not supported yet!");
1154 case TargetLowering::Legal: {
1155 SDOperand Ops[6];
1156 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001157 for (int x = 1; x < 6; ++x) {
1158 Ops[x] = Node->getOperand(x);
1159 if (!isTypeLegal(Ops[x].getValueType()))
1160 Ops[x] = PromoteOp(Ops[x]);
1161 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001162 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1163 break;
1164 }
1165 case TargetLowering::Expand:
1166 //There is no libgcc call for this op
1167 Result = Node->getOperand(0); // Noop
1168 break;
1169 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001170 break;
1171 }
1172
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001173 case ISD::ATOMIC_LCS:
1174 case ISD::ATOMIC_LAS:
1175 case ISD::ATOMIC_SWAP: {
1176 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1177 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1178 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
1179 "Invalid MemBarrier node!");
1180 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
1181 MVT::ValueType VT = Node->getValueType(0);
1182 switch (TLI.getOperationAction(ISD::ATOMIC_LCS, VT)) {
1183 default: assert(0 && "This action is not supported yet!");
1184 case TargetLowering::Legal: {
1185 SDOperand Ops[4];
1186 for (int x = 0; x < num; ++x)
1187 Ops[x] = LegalizeOp(Node->getOperand(x));
1188 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1191 return Result.getValue(Op.ResNo);
1192 break;
1193 }
1194 }
1195 break;
1196 }
1197
Scott Michelf2e2b702007-08-08 23:23:31 +00001198 case ISD::Constant: {
1199 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1200 unsigned opAction =
1201 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 // We know we don't need to expand constants here, constants only have one
1204 // value and we check that it is fine above.
1205
Scott Michelf2e2b702007-08-08 23:23:31 +00001206 if (opAction == TargetLowering::Custom) {
1207 Tmp1 = TLI.LowerOperation(Result, DAG);
1208 if (Tmp1.Val)
1209 Result = Tmp1;
1210 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001212 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 case ISD::ConstantFP: {
1214 // Spill FP immediates to the constant pool if the target cannot directly
1215 // codegen them. Targets often have some immediate values that can be
1216 // efficiently generated into an FP register without a load. We explicitly
1217 // leave these constants as ConstantFP nodes for the target to deal with.
1218 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1221 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001222 case TargetLowering::Legal:
1223 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 case TargetLowering::Custom:
1225 Tmp3 = TLI.LowerOperation(Result, DAG);
1226 if (Tmp3.Val) {
1227 Result = Tmp3;
1228 break;
1229 }
1230 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001231 case TargetLowering::Expand: {
1232 // Check to see if this FP immediate is already legal.
1233 bool isLegal = false;
1234 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1235 E = TLI.legal_fpimm_end(); I != E; ++I) {
1236 if (CFP->isExactlyValue(*I)) {
1237 isLegal = true;
1238 break;
1239 }
1240 }
1241 // If this is a legal constant, turn it into a TargetConstantFP node.
1242 if (isLegal)
1243 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1245 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001246 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 break;
1248 }
1249 case ISD::TokenFactor:
1250 if (Node->getNumOperands() == 2) {
1251 Tmp1 = LegalizeOp(Node->getOperand(0));
1252 Tmp2 = LegalizeOp(Node->getOperand(1));
1253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1254 } else if (Node->getNumOperands() == 3) {
1255 Tmp1 = LegalizeOp(Node->getOperand(0));
1256 Tmp2 = LegalizeOp(Node->getOperand(1));
1257 Tmp3 = LegalizeOp(Node->getOperand(2));
1258 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1259 } else {
1260 SmallVector<SDOperand, 8> Ops;
1261 // Legalize the operands.
1262 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1263 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1264 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1265 }
1266 break;
1267
1268 case ISD::FORMAL_ARGUMENTS:
1269 case ISD::CALL:
1270 // The only option for this is to custom lower it.
1271 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1272 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001273
1274 // The number of incoming and outgoing values should match; unless the final
1275 // outgoing value is a flag.
1276 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1277 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1278 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1279 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 "Lowering call/formal_arguments produced unexpected # results!");
1281
1282 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1283 // remember that we legalized all of them, so it doesn't get relegalized.
1284 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001285 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1286 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1288 if (Op.ResNo == i)
1289 Tmp2 = Tmp1;
1290 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1291 }
1292 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001293 case ISD::EXTRACT_SUBREG: {
1294 Tmp1 = LegalizeOp(Node->getOperand(0));
1295 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1296 assert(idx && "Operand must be a constant");
1297 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1298 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1299 }
1300 break;
1301 case ISD::INSERT_SUBREG: {
1302 Tmp1 = LegalizeOp(Node->getOperand(0));
1303 Tmp2 = LegalizeOp(Node->getOperand(1));
1304 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1305 assert(idx && "Operand must be a constant");
1306 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1307 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1308 }
1309 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 case ISD::BUILD_VECTOR:
1311 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1312 default: assert(0 && "This action is not supported yet!");
1313 case TargetLowering::Custom:
1314 Tmp3 = TLI.LowerOperation(Result, DAG);
1315 if (Tmp3.Val) {
1316 Result = Tmp3;
1317 break;
1318 }
1319 // FALLTHROUGH
1320 case TargetLowering::Expand:
1321 Result = ExpandBUILD_VECTOR(Result.Val);
1322 break;
1323 }
1324 break;
1325 case ISD::INSERT_VECTOR_ELT:
1326 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001328
1329 // The type of the value to insert may not be legal, even though the vector
1330 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1331 // here.
1332 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1333 default: assert(0 && "Cannot expand insert element operand");
1334 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1335 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1336 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1338
1339 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1340 Node->getValueType(0))) {
1341 default: assert(0 && "This action is not supported yet!");
1342 case TargetLowering::Legal:
1343 break;
1344 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001345 Tmp4 = TLI.LowerOperation(Result, DAG);
1346 if (Tmp4.Val) {
1347 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 break;
1349 }
1350 // FALLTHROUGH
1351 case TargetLowering::Expand: {
1352 // If the insert index is a constant, codegen this as a scalar_to_vector,
1353 // then a shuffle that inserts it into the right position in the vector.
1354 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001355 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1356 // match the element type of the vector being created.
1357 if (Tmp2.getValueType() ==
1358 MVT::getVectorElementType(Op.getValueType())) {
1359 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1360 Tmp1.getValueType(), Tmp2);
1361
1362 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1363 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1364 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1365
1366 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1367 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1368 // elt 0 of the RHS.
1369 SmallVector<SDOperand, 8> ShufOps;
1370 for (unsigned i = 0; i != NumElts; ++i) {
1371 if (i != InsertPos->getValue())
1372 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1373 else
1374 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1375 }
1376 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1377 &ShufOps[0], ShufOps.size());
1378
1379 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1380 Tmp1, ScVec, ShufMask);
1381 Result = LegalizeOp(Result);
1382 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 }
1385
1386 // If the target doesn't support this, we have to spill the input vector
1387 // to a temporary stack slot, update the element, then reload it. This is
1388 // badness. We could also load the value into a vector register (either
1389 // with a "move to register" or "extload into register" instruction, then
1390 // permute it into place, if the idx is a constant and if the idx is
1391 // supported by the target.
1392 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001393 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394 MVT::ValueType IdxVT = Tmp3.getValueType();
1395 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001396 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman12a9c082008-02-06 22:27:42 +00001397
Dan Gohman20e37962008-02-11 18:58:42 +00001398 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman12a9c082008-02-06 22:27:42 +00001399 int SPFI = StackPtrFI->getIndex();
1400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 // Store the vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001402 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001403 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001404 SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405
1406 // Truncate or zero extend offset to target pointer type.
1407 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1408 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1409 // Add the offset to the index.
1410 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1411 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1412 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1413 // Store the scalar value.
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001414 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1415 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 // Load the updated vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001417 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001418 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001419 break;
1420 }
1421 }
1422 break;
1423 case ISD::SCALAR_TO_VECTOR:
1424 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1425 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1426 break;
1427 }
1428
1429 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1430 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1431 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1432 Node->getValueType(0))) {
1433 default: assert(0 && "This action is not supported yet!");
1434 case TargetLowering::Legal:
1435 break;
1436 case TargetLowering::Custom:
1437 Tmp3 = TLI.LowerOperation(Result, DAG);
1438 if (Tmp3.Val) {
1439 Result = Tmp3;
1440 break;
1441 }
1442 // FALLTHROUGH
1443 case TargetLowering::Expand:
1444 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1445 break;
1446 }
1447 break;
1448 case ISD::VECTOR_SHUFFLE:
1449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1450 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1452
1453 // Allow targets to custom lower the SHUFFLEs they support.
1454 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1455 default: assert(0 && "Unknown operation action!");
1456 case TargetLowering::Legal:
1457 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1458 "vector shuffle should not be created if not legal!");
1459 break;
1460 case TargetLowering::Custom:
1461 Tmp3 = TLI.LowerOperation(Result, DAG);
1462 if (Tmp3.Val) {
1463 Result = Tmp3;
1464 break;
1465 }
1466 // FALLTHROUGH
1467 case TargetLowering::Expand: {
1468 MVT::ValueType VT = Node->getValueType(0);
1469 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1470 MVT::ValueType PtrVT = TLI.getPointerTy();
1471 SDOperand Mask = Node->getOperand(2);
1472 unsigned NumElems = Mask.getNumOperands();
1473 SmallVector<SDOperand,8> Ops;
1474 for (unsigned i = 0; i != NumElems; ++i) {
1475 SDOperand Arg = Mask.getOperand(i);
1476 if (Arg.getOpcode() == ISD::UNDEF) {
1477 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1478 } else {
1479 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1480 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1481 if (Idx < NumElems)
1482 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1483 DAG.getConstant(Idx, PtrVT)));
1484 else
1485 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1486 DAG.getConstant(Idx - NumElems, PtrVT)));
1487 }
1488 }
1489 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1490 break;
1491 }
1492 case TargetLowering::Promote: {
1493 // Change base type to a different vector type.
1494 MVT::ValueType OVT = Node->getValueType(0);
1495 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1496
1497 // Cast the two input vectors.
1498 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1499 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1500
1501 // Convert the shuffle mask to the right # elements.
1502 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1503 assert(Tmp3.Val && "Shuffle not legal?");
1504 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1505 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1506 break;
1507 }
1508 }
1509 break;
1510
1511 case ISD::EXTRACT_VECTOR_ELT:
1512 Tmp1 = Node->getOperand(0);
1513 Tmp2 = LegalizeOp(Node->getOperand(1));
1514 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1515 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1516 break;
1517
1518 case ISD::EXTRACT_SUBVECTOR:
1519 Tmp1 = Node->getOperand(0);
1520 Tmp2 = LegalizeOp(Node->getOperand(1));
1521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1522 Result = ExpandEXTRACT_SUBVECTOR(Result);
1523 break;
1524
1525 case ISD::CALLSEQ_START: {
1526 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1527
1528 // Recursively Legalize all of the inputs of the call end that do not lead
1529 // to this call start. This ensures that any libcalls that need be inserted
1530 // are inserted *before* the CALLSEQ_START.
1531 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1532 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1533 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1534 NodesLeadingTo);
1535 }
1536
1537 // Now that we legalized all of the inputs (which may have inserted
1538 // libcalls) create the new CALLSEQ_START node.
1539 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1540
1541 // Merge in the last call, to ensure that this call start after the last
1542 // call ended.
1543 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1544 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1545 Tmp1 = LegalizeOp(Tmp1);
1546 }
1547
1548 // Do not try to legalize the target-specific arguments (#1+).
1549 if (Tmp1 != Node->getOperand(0)) {
1550 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1551 Ops[0] = Tmp1;
1552 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1553 }
1554
1555 // Remember that the CALLSEQ_START is legalized.
1556 AddLegalizedOperand(Op.getValue(0), Result);
1557 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1558 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1559
1560 // Now that the callseq_start and all of the non-call nodes above this call
1561 // sequence have been legalized, legalize the call itself. During this
1562 // process, no libcalls can/will be inserted, guaranteeing that no calls
1563 // can overlap.
1564 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1565 SDOperand InCallSEQ = LastCALLSEQ_END;
1566 // Note that we are selecting this call!
1567 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1568 IsLegalizingCall = true;
1569
1570 // Legalize the call, starting from the CALLSEQ_END.
1571 LegalizeOp(LastCALLSEQ_END);
1572 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1573 return Result;
1574 }
1575 case ISD::CALLSEQ_END:
1576 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1577 // will cause this node to be legalized as well as handling libcalls right.
1578 if (LastCALLSEQ_END.Val != Node) {
1579 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1580 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1581 assert(I != LegalizedNodes.end() &&
1582 "Legalizing the call start should have legalized this node!");
1583 return I->second;
1584 }
1585
1586 // Otherwise, the call start has been legalized and everything is going
1587 // according to plan. Just legalize ourselves normally here.
1588 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1589 // Do not try to legalize the target-specific arguments (#1+), except for
1590 // an optional flag input.
1591 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1592 if (Tmp1 != Node->getOperand(0)) {
1593 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1594 Ops[0] = Tmp1;
1595 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1596 }
1597 } else {
1598 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1599 if (Tmp1 != Node->getOperand(0) ||
1600 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1601 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1602 Ops[0] = Tmp1;
1603 Ops.back() = Tmp2;
1604 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1605 }
1606 }
1607 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1608 // This finishes up call legalization.
1609 IsLegalizingCall = false;
1610
1611 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1612 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1613 if (Node->getNumValues() == 2)
1614 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1615 return Result.getValue(Op.ResNo);
1616 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001617 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001618 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1619 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1620 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1621 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1622
1623 Tmp1 = Result.getValue(0);
1624 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001625 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001626 default: assert(0 && "This action is not supported yet!");
1627 case TargetLowering::Expand: {
1628 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1629 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1630 " not tell us which reg is the stack pointer!");
1631 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001632
1633 // Chain the dynamic stack allocation so that it doesn't modify the stack
1634 // pointer when other instructions are using the stack.
1635 Chain = DAG.getCALLSEQ_START(Chain,
1636 DAG.getConstant(0, TLI.getPointerTy()));
1637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001639 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1640 Chain = SP.getValue(1);
1641 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1642 unsigned StackAlign =
1643 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1644 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001645 SP = DAG.getNode(ISD::AND, VT, SP,
1646 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001647 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001648 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1649
1650 Tmp2 =
1651 DAG.getCALLSEQ_END(Chain,
1652 DAG.getConstant(0, TLI.getPointerTy()),
1653 DAG.getConstant(0, TLI.getPointerTy()),
1654 SDOperand());
1655
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656 Tmp1 = LegalizeOp(Tmp1);
1657 Tmp2 = LegalizeOp(Tmp2);
1658 break;
1659 }
1660 case TargetLowering::Custom:
1661 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1662 if (Tmp3.Val) {
1663 Tmp1 = LegalizeOp(Tmp3);
1664 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1665 }
1666 break;
1667 case TargetLowering::Legal:
1668 break;
1669 }
1670 // Since this op produce two values, make sure to remember that we
1671 // legalized both of them.
1672 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1673 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1674 return Op.ResNo ? Tmp2 : Tmp1;
1675 }
1676 case ISD::INLINEASM: {
1677 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1678 bool Changed = false;
1679 // Legalize all of the operands of the inline asm, in case they are nodes
1680 // that need to be expanded or something. Note we skip the asm string and
1681 // all of the TargetConstant flags.
1682 SDOperand Op = LegalizeOp(Ops[0]);
1683 Changed = Op != Ops[0];
1684 Ops[0] = Op;
1685
1686 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1687 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1688 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1689 for (++i; NumVals; ++i, --NumVals) {
1690 SDOperand Op = LegalizeOp(Ops[i]);
1691 if (Op != Ops[i]) {
1692 Changed = true;
1693 Ops[i] = Op;
1694 }
1695 }
1696 }
1697
1698 if (HasInFlag) {
1699 Op = LegalizeOp(Ops.back());
1700 Changed |= Op != Ops.back();
1701 Ops.back() = Op;
1702 }
1703
1704 if (Changed)
1705 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1706
1707 // INLINE asm returns a chain and flag, make sure to add both to the map.
1708 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1709 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1710 return Result.getValue(Op.ResNo);
1711 }
1712 case ISD::BR:
1713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1714 // Ensure that libcalls are emitted before a branch.
1715 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1716 Tmp1 = LegalizeOp(Tmp1);
1717 LastCALLSEQ_END = DAG.getEntryNode();
1718
1719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1720 break;
1721 case ISD::BRIND:
1722 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1723 // Ensure that libcalls are emitted before a branch.
1724 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1725 Tmp1 = LegalizeOp(Tmp1);
1726 LastCALLSEQ_END = DAG.getEntryNode();
1727
1728 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1729 default: assert(0 && "Indirect target must be legal type (pointer)!");
1730 case Legal:
1731 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1732 break;
1733 }
1734 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1735 break;
1736 case ISD::BR_JT:
1737 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1738 // Ensure that libcalls are emitted before a branch.
1739 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1740 Tmp1 = LegalizeOp(Tmp1);
1741 LastCALLSEQ_END = DAG.getEntryNode();
1742
1743 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1744 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1745
1746 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1747 default: assert(0 && "This action is not supported yet!");
1748 case TargetLowering::Legal: break;
1749 case TargetLowering::Custom:
1750 Tmp1 = TLI.LowerOperation(Result, DAG);
1751 if (Tmp1.Val) Result = Tmp1;
1752 break;
1753 case TargetLowering::Expand: {
1754 SDOperand Chain = Result.getOperand(0);
1755 SDOperand Table = Result.getOperand(1);
1756 SDOperand Index = Result.getOperand(2);
1757
1758 MVT::ValueType PTy = TLI.getPointerTy();
1759 MachineFunction &MF = DAG.getMachineFunction();
1760 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1761 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1762 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1763
1764 SDOperand LD;
1765 switch (EntrySize) {
1766 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001767 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001768 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001769 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001770 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 }
1772
Evan Cheng6fb06762007-11-09 01:32:10 +00001773 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1775 // For PIC, the sequence is:
1776 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001777 // RelocBase can be JumpTable, GOT or some sort of global base.
1778 if (PTy != MVT::i32)
1779 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1780 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1781 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001783 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 }
1785 }
1786 break;
1787 case ISD::BRCOND:
1788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1789 // Ensure that libcalls are emitted before a return.
1790 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1791 Tmp1 = LegalizeOp(Tmp1);
1792 LastCALLSEQ_END = DAG.getEntryNode();
1793
1794 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1795 case Expand: assert(0 && "It's impossible to expand bools");
1796 case Legal:
1797 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1798 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001799 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001800 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1801
1802 // The top bits of the promoted condition are not necessarily zero, ensure
1803 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001804 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001805 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001806 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1808 break;
1809 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001810 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811
1812 // Basic block destination (Op#2) is always legal.
1813 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1814
1815 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1816 default: assert(0 && "This action is not supported yet!");
1817 case TargetLowering::Legal: break;
1818 case TargetLowering::Custom:
1819 Tmp1 = TLI.LowerOperation(Result, DAG);
1820 if (Tmp1.Val) Result = Tmp1;
1821 break;
1822 case TargetLowering::Expand:
1823 // Expand brcond's setcc into its constituent parts and create a BR_CC
1824 // Node.
1825 if (Tmp2.getOpcode() == ISD::SETCC) {
1826 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1827 Tmp2.getOperand(0), Tmp2.getOperand(1),
1828 Node->getOperand(2));
1829 } else {
1830 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1831 DAG.getCondCode(ISD::SETNE), Tmp2,
1832 DAG.getConstant(0, Tmp2.getValueType()),
1833 Node->getOperand(2));
1834 }
1835 break;
1836 }
1837 break;
1838 case ISD::BR_CC:
1839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1840 // Ensure that libcalls are emitted before a branch.
1841 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1842 Tmp1 = LegalizeOp(Tmp1);
1843 Tmp2 = Node->getOperand(2); // LHS
1844 Tmp3 = Node->getOperand(3); // RHS
1845 Tmp4 = Node->getOperand(1); // CC
1846
1847 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1848 LastCALLSEQ_END = DAG.getEntryNode();
1849
1850 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1851 // the LHS is a legal SETCC itself. In this case, we need to compare
1852 // the result against zero to select between true and false values.
1853 if (Tmp3.Val == 0) {
1854 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1855 Tmp4 = DAG.getCondCode(ISD::SETNE);
1856 }
1857
1858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1859 Node->getOperand(4));
1860
1861 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1862 default: assert(0 && "Unexpected action for BR_CC!");
1863 case TargetLowering::Legal: break;
1864 case TargetLowering::Custom:
1865 Tmp4 = TLI.LowerOperation(Result, DAG);
1866 if (Tmp4.Val) Result = Tmp4;
1867 break;
1868 }
1869 break;
1870 case ISD::LOAD: {
1871 LoadSDNode *LD = cast<LoadSDNode>(Node);
1872 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1873 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1874
1875 ISD::LoadExtType ExtType = LD->getExtensionType();
1876 if (ExtType == ISD::NON_EXTLOAD) {
1877 MVT::ValueType VT = Node->getValueType(0);
1878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1879 Tmp3 = Result.getValue(0);
1880 Tmp4 = Result.getValue(1);
1881
1882 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1883 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001884 case TargetLowering::Legal:
1885 // If this is an unaligned load and the target doesn't support it,
1886 // expand it.
1887 if (!TLI.allowsUnalignedMemoryAccesses()) {
1888 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001889 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001890 if (LD->getAlignment() < ABIAlignment){
1891 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1892 TLI);
1893 Tmp3 = Result.getOperand(0);
1894 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001895 Tmp3 = LegalizeOp(Tmp3);
1896 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001897 }
1898 }
1899 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900 case TargetLowering::Custom:
1901 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1902 if (Tmp1.Val) {
1903 Tmp3 = LegalizeOp(Tmp1);
1904 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1905 }
1906 break;
1907 case TargetLowering::Promote: {
1908 // Only promote a load of vector type to another.
1909 assert(MVT::isVector(VT) && "Cannot promote this load!");
1910 // Change base type to a different vector type.
1911 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1912
1913 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1914 LD->getSrcValueOffset(),
1915 LD->isVolatile(), LD->getAlignment());
1916 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1917 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1918 break;
1919 }
1920 }
1921 // Since loads produce two values, make sure to remember that we
1922 // legalized both of them.
1923 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1924 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1925 return Op.ResNo ? Tmp4 : Tmp3;
1926 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001927 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands082524c2008-01-23 20:39:46 +00001928 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1929 int SVOffset = LD->getSrcValueOffset();
1930 unsigned Alignment = LD->getAlignment();
1931 bool isVolatile = LD->isVolatile();
1932
1933 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1934 // Some targets pretend to have an i1 loading operation, and actually
1935 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1936 // bits are guaranteed to be zero; it helps the optimizers understand
1937 // that these bits are zero. It is also useful for EXTLOAD, since it
1938 // tells the optimizers that those bits are undefined. It would be
1939 // nice to have an effective generic way of getting these benefits...
1940 // Until such a way is found, don't insist on promoting i1 here.
1941 (SrcVT != MVT::i1 ||
1942 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1943 // Promote to a byte-sized load if not loading an integral number of
1944 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1945 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1946 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1947 SDOperand Ch;
1948
1949 // The extra bits are guaranteed to be zero, since we stored them that
1950 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1951
1952 ISD::LoadExtType NewExtType =
1953 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1954
1955 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1956 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1957 NVT, isVolatile, Alignment);
1958
1959 Ch = Result.getValue(1); // The chain.
1960
1961 if (ExtType == ISD::SEXTLOAD)
1962 // Having the top bits zero doesn't help when sign extending.
1963 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1964 Result, DAG.getValueType(SrcVT));
1965 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1966 // All the top bits are guaranteed to be zero - inform the optimizers.
1967 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1968 DAG.getValueType(SrcVT));
1969
1970 Tmp1 = LegalizeOp(Result);
1971 Tmp2 = LegalizeOp(Ch);
1972 } else if (SrcWidth & (SrcWidth - 1)) {
1973 // If not loading a power-of-2 number of bits, expand as two loads.
1974 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1975 "Unsupported extload!");
1976 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1977 assert(RoundWidth < SrcWidth);
1978 unsigned ExtraWidth = SrcWidth - RoundWidth;
1979 assert(ExtraWidth < RoundWidth);
1980 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1981 "Load size not an integral number of bytes!");
1982 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1983 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1984 SDOperand Lo, Hi, Ch;
1985 unsigned IncrementSize;
1986
1987 if (TLI.isLittleEndian()) {
1988 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1989 // Load the bottom RoundWidth bits.
1990 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1991 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1992 Alignment);
1993
1994 // Load the remaining ExtraWidth bits.
1995 IncrementSize = RoundWidth / 8;
1996 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1997 DAG.getIntPtrConstant(IncrementSize));
1998 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1999 LD->getSrcValue(), SVOffset + IncrementSize,
2000 ExtraVT, isVolatile,
2001 MinAlign(Alignment, IncrementSize));
2002
2003 // Build a factor node to remember that this load is independent of the
2004 // other one.
2005 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2006 Hi.getValue(1));
2007
2008 // Move the top bits to the right place.
2009 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2010 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2011
2012 // Join the hi and lo parts.
2013 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002014 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002015 // Big endian - avoid unaligned loads.
2016 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2017 // Load the top RoundWidth bits.
2018 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2019 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2020 Alignment);
2021
2022 // Load the remaining ExtraWidth bits.
2023 IncrementSize = RoundWidth / 8;
2024 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2025 DAG.getIntPtrConstant(IncrementSize));
2026 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2027 LD->getSrcValue(), SVOffset + IncrementSize,
2028 ExtraVT, isVolatile,
2029 MinAlign(Alignment, IncrementSize));
2030
2031 // Build a factor node to remember that this load is independent of the
2032 // other one.
2033 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2034 Hi.getValue(1));
2035
2036 // Move the top bits to the right place.
2037 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2038 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2039
2040 // Join the hi and lo parts.
2041 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2042 }
2043
2044 Tmp1 = LegalizeOp(Result);
2045 Tmp2 = LegalizeOp(Ch);
2046 } else {
2047 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2048 default: assert(0 && "This action is not supported yet!");
2049 case TargetLowering::Custom:
2050 isCustom = true;
2051 // FALLTHROUGH
2052 case TargetLowering::Legal:
2053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2054 Tmp1 = Result.getValue(0);
2055 Tmp2 = Result.getValue(1);
2056
2057 if (isCustom) {
2058 Tmp3 = TLI.LowerOperation(Result, DAG);
2059 if (Tmp3.Val) {
2060 Tmp1 = LegalizeOp(Tmp3);
2061 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2062 }
2063 } else {
2064 // If this is an unaligned load and the target doesn't support it,
2065 // expand it.
2066 if (!TLI.allowsUnalignedMemoryAccesses()) {
2067 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002068 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands082524c2008-01-23 20:39:46 +00002069 if (LD->getAlignment() < ABIAlignment){
2070 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2071 TLI);
2072 Tmp1 = Result.getOperand(0);
2073 Tmp2 = Result.getOperand(1);
2074 Tmp1 = LegalizeOp(Tmp1);
2075 Tmp2 = LegalizeOp(Tmp2);
2076 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002077 }
2078 }
Duncan Sands082524c2008-01-23 20:39:46 +00002079 break;
2080 case TargetLowering::Expand:
2081 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2082 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2083 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2084 LD->getSrcValueOffset(),
2085 LD->isVolatile(), LD->getAlignment());
2086 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2087 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2088 Tmp2 = LegalizeOp(Load.getValue(1));
2089 break;
2090 }
2091 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2092 // Turn the unsupported load into an EXTLOAD followed by an explicit
2093 // zero/sign extend inreg.
2094 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2095 Tmp1, Tmp2, LD->getSrcValue(),
2096 LD->getSrcValueOffset(), SrcVT,
2097 LD->isVolatile(), LD->getAlignment());
2098 SDOperand ValRes;
2099 if (ExtType == ISD::SEXTLOAD)
2100 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2101 Result, DAG.getValueType(SrcVT));
2102 else
2103 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2104 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2105 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 break;
2107 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 }
Duncan Sands082524c2008-01-23 20:39:46 +00002109
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 // Since loads produce two values, make sure to remember that we legalized
2111 // both of them.
2112 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2113 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2114 return Op.ResNo ? Tmp2 : Tmp1;
2115 }
2116 }
2117 case ISD::EXTRACT_ELEMENT: {
2118 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2119 switch (getTypeAction(OpTy)) {
2120 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2121 case Legal:
2122 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2123 // 1 -> Hi
2124 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2125 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2126 TLI.getShiftAmountTy()));
2127 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2128 } else {
2129 // 0 -> Lo
2130 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2131 Node->getOperand(0));
2132 }
2133 break;
2134 case Expand:
2135 // Get both the low and high parts.
2136 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2137 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2138 Result = Tmp2; // 1 -> Hi
2139 else
2140 Result = Tmp1; // 0 -> Lo
2141 break;
2142 }
2143 break;
2144 }
2145
2146 case ISD::CopyToReg:
2147 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2148
2149 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2150 "Register type must be legal!");
2151 // Legalize the incoming value (must be a legal type).
2152 Tmp2 = LegalizeOp(Node->getOperand(2));
2153 if (Node->getNumValues() == 1) {
2154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2155 } else {
2156 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2157 if (Node->getNumOperands() == 4) {
2158 Tmp3 = LegalizeOp(Node->getOperand(3));
2159 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2160 Tmp3);
2161 } else {
2162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2163 }
2164
2165 // Since this produces two values, make sure to remember that we legalized
2166 // both of them.
2167 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2168 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2169 return Result;
2170 }
2171 break;
2172
2173 case ISD::RET:
2174 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2175
2176 // Ensure that libcalls are emitted before a return.
2177 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2178 Tmp1 = LegalizeOp(Tmp1);
2179 LastCALLSEQ_END = DAG.getEntryNode();
2180
2181 switch (Node->getNumOperands()) {
2182 case 3: // ret val
2183 Tmp2 = Node->getOperand(1);
2184 Tmp3 = Node->getOperand(2); // Signness
2185 switch (getTypeAction(Tmp2.getValueType())) {
2186 case Legal:
2187 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2188 break;
2189 case Expand:
2190 if (!MVT::isVector(Tmp2.getValueType())) {
2191 SDOperand Lo, Hi;
2192 ExpandOp(Tmp2, Lo, Hi);
2193
2194 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002195 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002196 std::swap(Lo, Hi);
2197
2198 if (Hi.Val)
2199 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2200 else
2201 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2202 Result = LegalizeOp(Result);
2203 } else {
2204 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002205 int InIx = Tmp2.ResNo;
2206 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2207 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002208
2209 // Figure out if there is a simple type corresponding to this Vector
2210 // type. If so, convert to the vector type.
2211 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2212 if (TLI.isTypeLegal(TVT)) {
2213 // Turn this into a return of the vector type.
2214 Tmp2 = LegalizeOp(Tmp2);
2215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2216 } else if (NumElems == 1) {
2217 // Turn this into a return of the scalar type.
2218 Tmp2 = ScalarizeVectorOp(Tmp2);
2219 Tmp2 = LegalizeOp(Tmp2);
2220 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2221
2222 // FIXME: Returns of gcc generic vectors smaller than a legal type
2223 // should be returned in integer registers!
2224
2225 // The scalarized value type may not be legal, e.g. it might require
2226 // promotion or expansion. Relegalize the return.
2227 Result = LegalizeOp(Result);
2228 } else {
2229 // FIXME: Returns of gcc generic vectors larger than a legal vector
2230 // type should be returned by reference!
2231 SDOperand Lo, Hi;
2232 SplitVectorOp(Tmp2, Lo, Hi);
2233 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2234 Result = LegalizeOp(Result);
2235 }
2236 }
2237 break;
2238 case Promote:
2239 Tmp2 = PromoteOp(Node->getOperand(1));
2240 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2241 Result = LegalizeOp(Result);
2242 break;
2243 }
2244 break;
2245 case 1: // ret void
2246 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2247 break;
2248 default: { // ret <values>
2249 SmallVector<SDOperand, 8> NewValues;
2250 NewValues.push_back(Tmp1);
2251 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2252 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2253 case Legal:
2254 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2255 NewValues.push_back(Node->getOperand(i+1));
2256 break;
2257 case Expand: {
2258 SDOperand Lo, Hi;
2259 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
2260 "FIXME: TODO: implement returning non-legal vector types!");
2261 ExpandOp(Node->getOperand(i), Lo, Hi);
2262 NewValues.push_back(Lo);
2263 NewValues.push_back(Node->getOperand(i+1));
2264 if (Hi.Val) {
2265 NewValues.push_back(Hi);
2266 NewValues.push_back(Node->getOperand(i+1));
2267 }
2268 break;
2269 }
2270 case Promote:
2271 assert(0 && "Can't promote multiple return value yet!");
2272 }
2273
2274 if (NewValues.size() == Node->getNumOperands())
2275 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2276 else
2277 Result = DAG.getNode(ISD::RET, MVT::Other,
2278 &NewValues[0], NewValues.size());
2279 break;
2280 }
2281 }
2282
2283 if (Result.getOpcode() == ISD::RET) {
2284 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2285 default: assert(0 && "This action is not supported yet!");
2286 case TargetLowering::Legal: break;
2287 case TargetLowering::Custom:
2288 Tmp1 = TLI.LowerOperation(Result, DAG);
2289 if (Tmp1.Val) Result = Tmp1;
2290 break;
2291 }
2292 }
2293 break;
2294 case ISD::STORE: {
2295 StoreSDNode *ST = cast<StoreSDNode>(Node);
2296 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2297 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2298 int SVOffset = ST->getSrcValueOffset();
2299 unsigned Alignment = ST->getAlignment();
2300 bool isVolatile = ST->isVolatile();
2301
2302 if (!ST->isTruncatingStore()) {
2303 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2304 // FIXME: We shouldn't do this for TargetConstantFP's.
2305 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2306 // to phase ordering between legalized code and the dag combiner. This
2307 // probably means that we need to integrate dag combiner and legalizer
2308 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002309 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002310 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002311 if (CFP->getValueType(0) == MVT::f32 &&
2312 getTypeAction(MVT::i32) == Legal) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00002313 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2314 convertToAPInt().getZExtValue(),
Dale Johannesen1616e902007-09-11 18:32:33 +00002315 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002316 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2317 SVOffset, isVolatile, Alignment);
2318 break;
2319 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002320 // If this target supports 64-bit registers, do a single 64-bit store.
2321 if (getTypeAction(MVT::i64) == Legal) {
2322 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2323 getZExtValue(), MVT::i64);
2324 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2325 SVOffset, isVolatile, Alignment);
2326 break;
2327 } else if (getTypeAction(MVT::i32) == Legal) {
2328 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2329 // stores. If the target supports neither 32- nor 64-bits, this
2330 // xform is certainly not worth it.
2331 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2332 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2333 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002334 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002335
2336 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2337 SVOffset, isVolatile, Alignment);
2338 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002339 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002340 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002341 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002342
2343 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2344 break;
2345 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 }
2348
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002349 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002350 case Legal: {
2351 Tmp3 = LegalizeOp(ST->getValue());
2352 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2353 ST->getOffset());
2354
2355 MVT::ValueType VT = Tmp3.getValueType();
2356 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2357 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002358 case TargetLowering::Legal:
2359 // If this is an unaligned store and the target doesn't support it,
2360 // expand it.
2361 if (!TLI.allowsUnalignedMemoryAccesses()) {
2362 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002363 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002364 if (ST->getAlignment() < ABIAlignment)
2365 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2366 TLI);
2367 }
2368 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 case TargetLowering::Custom:
2370 Tmp1 = TLI.LowerOperation(Result, DAG);
2371 if (Tmp1.Val) Result = Tmp1;
2372 break;
2373 case TargetLowering::Promote:
2374 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2375 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2376 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2377 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2378 ST->getSrcValue(), SVOffset, isVolatile,
2379 Alignment);
2380 break;
2381 }
2382 break;
2383 }
2384 case Promote:
2385 // Truncate the value and store the result.
2386 Tmp3 = PromoteOp(ST->getValue());
2387 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002388 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 isVolatile, Alignment);
2390 break;
2391
2392 case Expand:
2393 unsigned IncrementSize = 0;
2394 SDOperand Lo, Hi;
2395
2396 // If this is a vector type, then we have to calculate the increment as
2397 // the product of the element size in bytes, and the number of elements
2398 // in the high half of the vector.
2399 if (MVT::isVector(ST->getValue().getValueType())) {
2400 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002401 int InIx = ST->getValue().ResNo;
Chris Lattner5872a362008-01-17 07:00:52 +00002402 MVT::ValueType InVT = InVal->getValueType(InIx);
2403 unsigned NumElems = MVT::getVectorNumElements(InVT);
2404 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002405
2406 // Figure out if there is a simple type corresponding to this Vector
2407 // type. If so, convert to the vector type.
2408 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2409 if (TLI.isTypeLegal(TVT)) {
2410 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002411 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2413 SVOffset, isVolatile, Alignment);
2414 Result = LegalizeOp(Result);
2415 break;
2416 } else if (NumElems == 1) {
2417 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002418 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2420 SVOffset, isVolatile, Alignment);
2421 // The scalarized value type may not be legal, e.g. it might require
2422 // promotion or expansion. Relegalize the scalar store.
2423 Result = LegalizeOp(Result);
2424 break;
2425 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002426 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00002427 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2428 MVT::getSizeInBits(EVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 }
2430 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002431 ExpandOp(ST->getValue(), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002432 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2433
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002434 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 std::swap(Lo, Hi);
2436 }
2437
2438 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2439 SVOffset, isVolatile, Alignment);
2440
2441 if (Hi.Val == NULL) {
2442 // Must be int <-> float one-to-one expansion.
2443 Result = Lo;
2444 break;
2445 }
2446
2447 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002448 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 assert(isTypeLegal(Tmp2.getValueType()) &&
2450 "Pointers must be legal!");
2451 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002452 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2454 SVOffset, isVolatile, Alignment);
2455 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2456 break;
2457 }
2458 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002459 switch (getTypeAction(ST->getValue().getValueType())) {
2460 case Legal:
2461 Tmp3 = LegalizeOp(ST->getValue());
2462 break;
2463 case Promote:
2464 // We can promote the value, the truncstore will still take care of it.
2465 Tmp3 = PromoteOp(ST->getValue());
2466 break;
2467 case Expand:
2468 // Just store the low part. This may become a non-trunc store, so make
2469 // sure to use getTruncStore, not UpdateNodeOperands below.
2470 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2471 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2472 SVOffset, MVT::i8, isVolatile, Alignment);
2473 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002475 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands40676662008-01-22 07:17:34 +00002476 unsigned StWidth = MVT::getSizeInBits(StVT);
2477
2478 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2479 // Promote to a byte-sized store with upper bits zero if not
2480 // storing an integral number of bytes. For example, promote
2481 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2482 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2483 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2484 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2485 SVOffset, NVT, isVolatile, Alignment);
2486 } else if (StWidth & (StWidth - 1)) {
2487 // If not storing a power-of-2 number of bits, expand as two stores.
2488 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2489 "Unsupported truncstore!");
2490 unsigned RoundWidth = 1 << Log2_32(StWidth);
2491 assert(RoundWidth < StWidth);
2492 unsigned ExtraWidth = StWidth - RoundWidth;
2493 assert(ExtraWidth < RoundWidth);
2494 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2495 "Store size not an integral number of bytes!");
2496 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2497 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2498 SDOperand Lo, Hi;
2499 unsigned IncrementSize;
2500
2501 if (TLI.isLittleEndian()) {
2502 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2503 // Store the bottom RoundWidth bits.
2504 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2505 SVOffset, RoundVT,
2506 isVolatile, Alignment);
2507
2508 // Store the remaining ExtraWidth bits.
2509 IncrementSize = RoundWidth / 8;
2510 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2511 DAG.getIntPtrConstant(IncrementSize));
2512 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2513 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2514 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2515 SVOffset + IncrementSize, ExtraVT, isVolatile,
2516 MinAlign(Alignment, IncrementSize));
2517 } else {
2518 // Big endian - avoid unaligned stores.
2519 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2520 // Store the top RoundWidth bits.
2521 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2522 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2523 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2524 RoundVT, isVolatile, Alignment);
2525
2526 // Store the remaining ExtraWidth bits.
2527 IncrementSize = RoundWidth / 8;
2528 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2529 DAG.getIntPtrConstant(IncrementSize));
2530 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2531 SVOffset + IncrementSize, ExtraVT, isVolatile,
2532 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002533 }
Duncan Sands40676662008-01-22 07:17:34 +00002534
2535 // The order of the stores doesn't matter.
2536 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2537 } else {
2538 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2539 Tmp2 != ST->getBasePtr())
2540 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2541 ST->getOffset());
2542
2543 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2544 default: assert(0 && "This action is not supported yet!");
2545 case TargetLowering::Legal:
2546 // If this is an unaligned store and the target doesn't support it,
2547 // expand it.
2548 if (!TLI.allowsUnalignedMemoryAccesses()) {
2549 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002550 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands40676662008-01-22 07:17:34 +00002551 if (ST->getAlignment() < ABIAlignment)
2552 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2553 TLI);
2554 }
2555 break;
2556 case TargetLowering::Custom:
2557 Result = TLI.LowerOperation(Result, DAG);
2558 break;
2559 case Expand:
2560 // TRUNCSTORE:i16 i32 -> STORE i16
2561 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2562 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2563 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2564 isVolatile, Alignment);
2565 break;
2566 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 }
2568 }
2569 break;
2570 }
2571 case ISD::PCMARKER:
2572 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2574 break;
2575 case ISD::STACKSAVE:
2576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2577 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2578 Tmp1 = Result.getValue(0);
2579 Tmp2 = Result.getValue(1);
2580
2581 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2582 default: assert(0 && "This action is not supported yet!");
2583 case TargetLowering::Legal: break;
2584 case TargetLowering::Custom:
2585 Tmp3 = TLI.LowerOperation(Result, DAG);
2586 if (Tmp3.Val) {
2587 Tmp1 = LegalizeOp(Tmp3);
2588 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2589 }
2590 break;
2591 case TargetLowering::Expand:
2592 // Expand to CopyFromReg if the target set
2593 // StackPointerRegisterToSaveRestore.
2594 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2595 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2596 Node->getValueType(0));
2597 Tmp2 = Tmp1.getValue(1);
2598 } else {
2599 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2600 Tmp2 = Node->getOperand(0);
2601 }
2602 break;
2603 }
2604
2605 // Since stacksave produce two values, make sure to remember that we
2606 // legalized both of them.
2607 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2608 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2609 return Op.ResNo ? Tmp2 : Tmp1;
2610
2611 case ISD::STACKRESTORE:
2612 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2613 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2614 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2615
2616 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2617 default: assert(0 && "This action is not supported yet!");
2618 case TargetLowering::Legal: break;
2619 case TargetLowering::Custom:
2620 Tmp1 = TLI.LowerOperation(Result, DAG);
2621 if (Tmp1.Val) Result = Tmp1;
2622 break;
2623 case TargetLowering::Expand:
2624 // Expand to CopyToReg if the target set
2625 // StackPointerRegisterToSaveRestore.
2626 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2627 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2628 } else {
2629 Result = Tmp1;
2630 }
2631 break;
2632 }
2633 break;
2634
2635 case ISD::READCYCLECOUNTER:
2636 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2637 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2638 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2639 Node->getValueType(0))) {
2640 default: assert(0 && "This action is not supported yet!");
2641 case TargetLowering::Legal:
2642 Tmp1 = Result.getValue(0);
2643 Tmp2 = Result.getValue(1);
2644 break;
2645 case TargetLowering::Custom:
2646 Result = TLI.LowerOperation(Result, DAG);
2647 Tmp1 = LegalizeOp(Result.getValue(0));
2648 Tmp2 = LegalizeOp(Result.getValue(1));
2649 break;
2650 }
2651
2652 // Since rdcc produce two values, make sure to remember that we legalized
2653 // both of them.
2654 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2655 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2656 return Result;
2657
2658 case ISD::SELECT:
2659 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2660 case Expand: assert(0 && "It's impossible to expand bools");
2661 case Legal:
2662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2663 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002664 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2666 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002667 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002669 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2671 break;
2672 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002673 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2675 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2676
2677 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2678
2679 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2680 default: assert(0 && "This action is not supported yet!");
2681 case TargetLowering::Legal: break;
2682 case TargetLowering::Custom: {
2683 Tmp1 = TLI.LowerOperation(Result, DAG);
2684 if (Tmp1.Val) Result = Tmp1;
2685 break;
2686 }
2687 case TargetLowering::Expand:
2688 if (Tmp1.getOpcode() == ISD::SETCC) {
2689 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2690 Tmp2, Tmp3,
2691 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2692 } else {
2693 Result = DAG.getSelectCC(Tmp1,
2694 DAG.getConstant(0, Tmp1.getValueType()),
2695 Tmp2, Tmp3, ISD::SETNE);
2696 }
2697 break;
2698 case TargetLowering::Promote: {
2699 MVT::ValueType NVT =
2700 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2701 unsigned ExtOp, TruncOp;
2702 if (MVT::isVector(Tmp2.getValueType())) {
2703 ExtOp = ISD::BIT_CONVERT;
2704 TruncOp = ISD::BIT_CONVERT;
2705 } else if (MVT::isInteger(Tmp2.getValueType())) {
2706 ExtOp = ISD::ANY_EXTEND;
2707 TruncOp = ISD::TRUNCATE;
2708 } else {
2709 ExtOp = ISD::FP_EXTEND;
2710 TruncOp = ISD::FP_ROUND;
2711 }
2712 // Promote each of the values to the new type.
2713 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2714 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2715 // Perform the larger operation, then round down.
2716 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002717 if (TruncOp != ISD::FP_ROUND)
2718 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2719 else
2720 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2721 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 break;
2723 }
2724 }
2725 break;
2726 case ISD::SELECT_CC: {
2727 Tmp1 = Node->getOperand(0); // LHS
2728 Tmp2 = Node->getOperand(1); // RHS
2729 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2730 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2731 SDOperand CC = Node->getOperand(4);
2732
2733 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2734
2735 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2736 // the LHS is a legal SETCC itself. In this case, we need to compare
2737 // the result against zero to select between true and false values.
2738 if (Tmp2.Val == 0) {
2739 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2740 CC = DAG.getCondCode(ISD::SETNE);
2741 }
2742 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2743
2744 // Everything is legal, see if we should expand this op or something.
2745 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2746 default: assert(0 && "This action is not supported yet!");
2747 case TargetLowering::Legal: break;
2748 case TargetLowering::Custom:
2749 Tmp1 = TLI.LowerOperation(Result, DAG);
2750 if (Tmp1.Val) Result = Tmp1;
2751 break;
2752 }
2753 break;
2754 }
2755 case ISD::SETCC:
2756 Tmp1 = Node->getOperand(0);
2757 Tmp2 = Node->getOperand(1);
2758 Tmp3 = Node->getOperand(2);
2759 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2760
2761 // If we had to Expand the SetCC operands into a SELECT node, then it may
2762 // not always be possible to return a true LHS & RHS. In this case, just
2763 // return the value we legalized, returned in the LHS
2764 if (Tmp2.Val == 0) {
2765 Result = Tmp1;
2766 break;
2767 }
2768
2769 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2770 default: assert(0 && "Cannot handle this action for SETCC yet!");
2771 case TargetLowering::Custom:
2772 isCustom = true;
2773 // FALLTHROUGH.
2774 case TargetLowering::Legal:
2775 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2776 if (isCustom) {
2777 Tmp4 = TLI.LowerOperation(Result, DAG);
2778 if (Tmp4.Val) Result = Tmp4;
2779 }
2780 break;
2781 case TargetLowering::Promote: {
2782 // First step, figure out the appropriate operation to use.
2783 // Allow SETCC to not be supported for all legal data types
2784 // Mostly this targets FP
2785 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2786 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2787
2788 // Scan for the appropriate larger type to use.
2789 while (1) {
2790 NewInTy = (MVT::ValueType)(NewInTy+1);
2791
2792 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2793 "Fell off of the edge of the integer world");
2794 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2795 "Fell off of the edge of the floating point world");
2796
2797 // If the target supports SETCC of this type, use it.
2798 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2799 break;
2800 }
2801 if (MVT::isInteger(NewInTy))
2802 assert(0 && "Cannot promote Legal Integer SETCC yet");
2803 else {
2804 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2805 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2806 }
2807 Tmp1 = LegalizeOp(Tmp1);
2808 Tmp2 = LegalizeOp(Tmp2);
2809 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2810 Result = LegalizeOp(Result);
2811 break;
2812 }
2813 case TargetLowering::Expand:
2814 // Expand a setcc node into a select_cc of the same condition, lhs, and
2815 // rhs that selects between const 1 (true) and const 0 (false).
2816 MVT::ValueType VT = Node->getValueType(0);
2817 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2818 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2819 Tmp3);
2820 break;
2821 }
2822 break;
2823 case ISD::MEMSET:
2824 case ISD::MEMCPY:
2825 case ISD::MEMMOVE: {
2826 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2827 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2828
2829 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2830 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2831 case Expand: assert(0 && "Cannot expand a byte!");
2832 case Legal:
2833 Tmp3 = LegalizeOp(Node->getOperand(2));
2834 break;
2835 case Promote:
2836 Tmp3 = PromoteOp(Node->getOperand(2));
2837 break;
2838 }
2839 } else {
2840 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2841 }
2842
2843 SDOperand Tmp4;
2844 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2845 case Expand: {
2846 // Length is too big, just take the lo-part of the length.
2847 SDOperand HiPart;
2848 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2849 break;
2850 }
2851 case Legal:
2852 Tmp4 = LegalizeOp(Node->getOperand(3));
2853 break;
2854 case Promote:
2855 Tmp4 = PromoteOp(Node->getOperand(3));
2856 break;
2857 }
2858
2859 SDOperand Tmp5;
2860 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2861 case Expand: assert(0 && "Cannot expand this yet!");
2862 case Legal:
2863 Tmp5 = LegalizeOp(Node->getOperand(4));
2864 break;
2865 case Promote:
2866 Tmp5 = PromoteOp(Node->getOperand(4));
2867 break;
2868 }
2869
Rafael Espindola80825902007-10-19 10:41:11 +00002870 SDOperand Tmp6;
2871 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2872 case Expand: assert(0 && "Cannot expand this yet!");
2873 case Legal:
2874 Tmp6 = LegalizeOp(Node->getOperand(5));
2875 break;
2876 case Promote:
2877 Tmp6 = PromoteOp(Node->getOperand(5));
2878 break;
2879 }
2880
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2882 default: assert(0 && "This action not implemented for this operation!");
2883 case TargetLowering::Custom:
2884 isCustom = true;
2885 // FALLTHROUGH
Rafael Espindola80825902007-10-19 10:41:11 +00002886 case TargetLowering::Legal: {
2887 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2888 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889 if (isCustom) {
2890 Tmp1 = TLI.LowerOperation(Result, DAG);
2891 if (Tmp1.Val) Result = Tmp1;
2892 }
2893 break;
Rafael Espindola80825902007-10-19 10:41:11 +00002894 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 case TargetLowering::Expand: {
2896 // Otherwise, the target does not support this operation. Lower the
2897 // operation to an explicit libcall as appropriate.
2898 MVT::ValueType IntPtr = TLI.getPointerTy();
2899 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2900 TargetLowering::ArgListTy Args;
2901 TargetLowering::ArgListEntry Entry;
2902
2903 const char *FnName = 0;
2904 if (Node->getOpcode() == ISD::MEMSET) {
2905 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2906 Args.push_back(Entry);
2907 // Extend the (previously legalized) ubyte argument to be an int value
2908 // for the call.
2909 if (Tmp3.getValueType() > MVT::i32)
2910 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2911 else
2912 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2913 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2914 Args.push_back(Entry);
2915 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2916 Args.push_back(Entry);
2917
2918 FnName = "memset";
2919 } else if (Node->getOpcode() == ISD::MEMCPY ||
2920 Node->getOpcode() == ISD::MEMMOVE) {
2921 Entry.Ty = IntPtrTy;
2922 Entry.Node = Tmp2; Args.push_back(Entry);
2923 Entry.Node = Tmp3; Args.push_back(Entry);
2924 Entry.Node = Tmp4; Args.push_back(Entry);
2925 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2926 } else {
2927 assert(0 && "Unknown op!");
2928 }
2929
2930 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00002931 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2932 false, false, false, CallingConv::C, false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002933 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2934 Result = CallResult.second;
2935 break;
2936 }
2937 }
2938 break;
2939 }
2940
2941 case ISD::SHL_PARTS:
2942 case ISD::SRA_PARTS:
2943 case ISD::SRL_PARTS: {
2944 SmallVector<SDOperand, 8> Ops;
2945 bool Changed = false;
2946 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2947 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2948 Changed |= Ops.back() != Node->getOperand(i);
2949 }
2950 if (Changed)
2951 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2952
2953 switch (TLI.getOperationAction(Node->getOpcode(),
2954 Node->getValueType(0))) {
2955 default: assert(0 && "This action is not supported yet!");
2956 case TargetLowering::Legal: break;
2957 case TargetLowering::Custom:
2958 Tmp1 = TLI.LowerOperation(Result, DAG);
2959 if (Tmp1.Val) {
2960 SDOperand Tmp2, RetVal(0, 0);
2961 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2962 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2963 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2964 if (i == Op.ResNo)
2965 RetVal = Tmp2;
2966 }
2967 assert(RetVal.Val && "Illegal result number");
2968 return RetVal;
2969 }
2970 break;
2971 }
2972
2973 // Since these produce multiple values, make sure to remember that we
2974 // legalized all of them.
2975 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2976 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2977 return Result.getValue(Op.ResNo);
2978 }
2979
2980 // Binary operators
2981 case ISD::ADD:
2982 case ISD::SUB:
2983 case ISD::MUL:
2984 case ISD::MULHS:
2985 case ISD::MULHU:
2986 case ISD::UDIV:
2987 case ISD::SDIV:
2988 case ISD::AND:
2989 case ISD::OR:
2990 case ISD::XOR:
2991 case ISD::SHL:
2992 case ISD::SRL:
2993 case ISD::SRA:
2994 case ISD::FADD:
2995 case ISD::FSUB:
2996 case ISD::FMUL:
2997 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002998 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3000 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3001 case Expand: assert(0 && "Not possible");
3002 case Legal:
3003 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3004 break;
3005 case Promote:
3006 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3007 break;
3008 }
3009
3010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3011
3012 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3013 default: assert(0 && "BinOp legalize operation not supported");
3014 case TargetLowering::Legal: break;
3015 case TargetLowering::Custom:
3016 Tmp1 = TLI.LowerOperation(Result, DAG);
3017 if (Tmp1.Val) Result = Tmp1;
3018 break;
3019 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00003020 MVT::ValueType VT = Op.getValueType();
3021
3022 // See if multiply or divide can be lowered using two-result operations.
3023 SDVTList VTs = DAG.getVTList(VT, VT);
3024 if (Node->getOpcode() == ISD::MUL) {
3025 // We just need the low half of the multiply; try both the signed
3026 // and unsigned forms. If the target supports both SMUL_LOHI and
3027 // UMUL_LOHI, form a preference by checking which forms of plain
3028 // MULH it supports.
3029 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3030 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3031 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3032 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3033 unsigned OpToUse = 0;
3034 if (HasSMUL_LOHI && !HasMULHS) {
3035 OpToUse = ISD::SMUL_LOHI;
3036 } else if (HasUMUL_LOHI && !HasMULHU) {
3037 OpToUse = ISD::UMUL_LOHI;
3038 } else if (HasSMUL_LOHI) {
3039 OpToUse = ISD::SMUL_LOHI;
3040 } else if (HasUMUL_LOHI) {
3041 OpToUse = ISD::UMUL_LOHI;
3042 }
3043 if (OpToUse) {
3044 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3045 break;
3046 }
3047 }
3048 if (Node->getOpcode() == ISD::MULHS &&
3049 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3050 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3051 break;
3052 }
3053 if (Node->getOpcode() == ISD::MULHU &&
3054 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3055 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3056 break;
3057 }
3058 if (Node->getOpcode() == ISD::SDIV &&
3059 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3060 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3061 break;
3062 }
3063 if (Node->getOpcode() == ISD::UDIV &&
3064 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3065 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3066 break;
3067 }
3068
Dan Gohman6d05cac2007-10-11 23:57:53 +00003069 // Check to see if we have a libcall for this operator.
3070 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3071 bool isSigned = false;
3072 switch (Node->getOpcode()) {
3073 case ISD::UDIV:
3074 case ISD::SDIV:
3075 if (VT == MVT::i32) {
3076 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003078 isSigned = Node->getOpcode() == ISD::SDIV;
3079 }
3080 break;
3081 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003082 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3083 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003084 break;
3085 default: break;
3086 }
3087 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3088 SDOperand Dummy;
3089 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 break;
3091 }
3092
3093 assert(MVT::isVector(Node->getValueType(0)) &&
3094 "Cannot expand this binary operator!");
3095 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003096 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 break;
3098 }
3099 case TargetLowering::Promote: {
3100 switch (Node->getOpcode()) {
3101 default: assert(0 && "Do not know how to promote this BinOp!");
3102 case ISD::AND:
3103 case ISD::OR:
3104 case ISD::XOR: {
3105 MVT::ValueType OVT = Node->getValueType(0);
3106 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3107 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3108 // Bit convert each of the values to the new type.
3109 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3110 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3111 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3112 // Bit convert the result back the original type.
3113 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3114 break;
3115 }
3116 }
3117 }
3118 }
3119 break;
3120
Dan Gohman475cd732007-10-05 14:17:22 +00003121 case ISD::SMUL_LOHI:
3122 case ISD::UMUL_LOHI:
3123 case ISD::SDIVREM:
3124 case ISD::UDIVREM:
3125 // These nodes will only be produced by target-specific lowering, so
3126 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003127 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003128 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003129
3130 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3131 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3132 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003133 break;
3134
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3136 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3137 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3138 case Expand: assert(0 && "Not possible");
3139 case Legal:
3140 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3141 break;
3142 case Promote:
3143 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3144 break;
3145 }
3146
3147 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3148
3149 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3150 default: assert(0 && "Operation not supported");
3151 case TargetLowering::Custom:
3152 Tmp1 = TLI.LowerOperation(Result, DAG);
3153 if (Tmp1.Val) Result = Tmp1;
3154 break;
3155 case TargetLowering::Legal: break;
3156 case TargetLowering::Expand: {
3157 // If this target supports fabs/fneg natively and select is cheap,
3158 // do this efficiently.
3159 if (!TLI.isSelectExpensive() &&
3160 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3161 TargetLowering::Legal &&
3162 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3163 TargetLowering::Legal) {
3164 // Get the sign bit of the RHS.
3165 MVT::ValueType IVT =
3166 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3167 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3168 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3169 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3170 // Get the absolute value of the result.
3171 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3172 // Select between the nabs and abs value based on the sign bit of
3173 // the input.
3174 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3175 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3176 AbsVal),
3177 AbsVal);
3178 Result = LegalizeOp(Result);
3179 break;
3180 }
3181
3182 // Otherwise, do bitwise ops!
3183 MVT::ValueType NVT =
3184 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3185 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3186 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3187 Result = LegalizeOp(Result);
3188 break;
3189 }
3190 }
3191 break;
3192
3193 case ISD::ADDC:
3194 case ISD::SUBC:
3195 Tmp1 = LegalizeOp(Node->getOperand(0));
3196 Tmp2 = LegalizeOp(Node->getOperand(1));
3197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3198 // Since this produces two values, make sure to remember that we legalized
3199 // both of them.
3200 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3201 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3202 return Result;
3203
3204 case ISD::ADDE:
3205 case ISD::SUBE:
3206 Tmp1 = LegalizeOp(Node->getOperand(0));
3207 Tmp2 = LegalizeOp(Node->getOperand(1));
3208 Tmp3 = LegalizeOp(Node->getOperand(2));
3209 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3210 // Since this produces two values, make sure to remember that we legalized
3211 // both of them.
3212 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3213 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3214 return Result;
3215
3216 case ISD::BUILD_PAIR: {
3217 MVT::ValueType PairTy = Node->getValueType(0);
3218 // TODO: handle the case where the Lo and Hi operands are not of legal type
3219 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3220 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3221 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3222 case TargetLowering::Promote:
3223 case TargetLowering::Custom:
3224 assert(0 && "Cannot promote/custom this yet!");
3225 case TargetLowering::Legal:
3226 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3227 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3228 break;
3229 case TargetLowering::Expand:
3230 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3231 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3232 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3233 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3234 TLI.getShiftAmountTy()));
3235 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3236 break;
3237 }
3238 break;
3239 }
3240
3241 case ISD::UREM:
3242 case ISD::SREM:
3243 case ISD::FREM:
3244 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3245 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3246
3247 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3248 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3249 case TargetLowering::Custom:
3250 isCustom = true;
3251 // FALLTHROUGH
3252 case TargetLowering::Legal:
3253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3254 if (isCustom) {
3255 Tmp1 = TLI.LowerOperation(Result, DAG);
3256 if (Tmp1.Val) Result = Tmp1;
3257 }
3258 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003259 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3261 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00003262 MVT::ValueType VT = Node->getValueType(0);
3263
3264 // See if remainder can be lowered using two-result operations.
3265 SDVTList VTs = DAG.getVTList(VT, VT);
3266 if (Node->getOpcode() == ISD::SREM &&
3267 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3268 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3269 break;
3270 }
3271 if (Node->getOpcode() == ISD::UREM &&
3272 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3273 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3274 break;
3275 }
3276
3277 if (MVT::isInteger(VT)) {
3278 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003279 TargetLowering::Legal) {
3280 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003281 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3282 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3283 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003284 } else if (MVT::isVector(VT)) {
3285 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003287 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 "Cannot expand this binary operator!");
3289 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3290 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3291 SDOperand Dummy;
3292 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
3293 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003294 } else {
3295 assert(MVT::isFloatingPoint(VT) &&
3296 "remainder op must have integer or floating-point type");
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003297 if (MVT::isVector(VT)) {
3298 Result = LegalizeOp(UnrollVectorOp(Op));
3299 } else {
3300 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003301 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3302 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003303 SDOperand Dummy;
3304 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3305 false/*sign irrelevant*/, Dummy);
3306 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307 }
3308 break;
3309 }
Dan Gohman5a199552007-10-08 18:33:35 +00003310 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 break;
3312 case ISD::VAARG: {
3313 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3314 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3315
3316 MVT::ValueType VT = Node->getValueType(0);
3317 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3318 default: assert(0 && "This action is not supported yet!");
3319 case TargetLowering::Custom:
3320 isCustom = true;
3321 // FALLTHROUGH
3322 case TargetLowering::Legal:
3323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3324 Result = Result.getValue(0);
3325 Tmp1 = Result.getValue(1);
3326
3327 if (isCustom) {
3328 Tmp2 = TLI.LowerOperation(Result, DAG);
3329 if (Tmp2.Val) {
3330 Result = LegalizeOp(Tmp2);
3331 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3332 }
3333 }
3334 break;
3335 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003336 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3337 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338 // Increment the pointer, VAList, to the next vaarg
3339 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3340 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3341 TLI.getPointerTy()));
3342 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003343 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344 // Load the actual argument out of the pointer VAList
3345 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3346 Tmp1 = LegalizeOp(Result.getValue(1));
3347 Result = LegalizeOp(Result);
3348 break;
3349 }
3350 }
3351 // Since VAARG produces two values, make sure to remember that we
3352 // legalized both of them.
3353 AddLegalizedOperand(SDOperand(Node, 0), Result);
3354 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3355 return Op.ResNo ? Tmp1 : Result;
3356 }
3357
3358 case ISD::VACOPY:
3359 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3360 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3361 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3362
3363 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3364 default: assert(0 && "This action is not supported yet!");
3365 case TargetLowering::Custom:
3366 isCustom = true;
3367 // FALLTHROUGH
3368 case TargetLowering::Legal:
3369 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3370 Node->getOperand(3), Node->getOperand(4));
3371 if (isCustom) {
3372 Tmp1 = TLI.LowerOperation(Result, DAG);
3373 if (Tmp1.Val) Result = Tmp1;
3374 }
3375 break;
3376 case TargetLowering::Expand:
3377 // This defaults to loading a pointer from the input and storing it to the
3378 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003379 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3380 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3381 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3382 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 break;
3384 }
3385 break;
3386
3387 case ISD::VAEND:
3388 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3389 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3390
3391 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3392 default: assert(0 && "This action is not supported yet!");
3393 case TargetLowering::Custom:
3394 isCustom = true;
3395 // FALLTHROUGH
3396 case TargetLowering::Legal:
3397 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3398 if (isCustom) {
3399 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3400 if (Tmp1.Val) Result = Tmp1;
3401 }
3402 break;
3403 case TargetLowering::Expand:
3404 Result = Tmp1; // Default to a no-op, return the chain
3405 break;
3406 }
3407 break;
3408
3409 case ISD::VASTART:
3410 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3411 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3412
3413 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3414
3415 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3416 default: assert(0 && "This action is not supported yet!");
3417 case TargetLowering::Legal: break;
3418 case TargetLowering::Custom:
3419 Tmp1 = TLI.LowerOperation(Result, DAG);
3420 if (Tmp1.Val) Result = Tmp1;
3421 break;
3422 }
3423 break;
3424
3425 case ISD::ROTL:
3426 case ISD::ROTR:
3427 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3428 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3429 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3430 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3431 default:
3432 assert(0 && "ROTL/ROTR legalize operation not supported");
3433 break;
3434 case TargetLowering::Legal:
3435 break;
3436 case TargetLowering::Custom:
3437 Tmp1 = TLI.LowerOperation(Result, DAG);
3438 if (Tmp1.Val) Result = Tmp1;
3439 break;
3440 case TargetLowering::Promote:
3441 assert(0 && "Do not know how to promote ROTL/ROTR");
3442 break;
3443 case TargetLowering::Expand:
3444 assert(0 && "Do not know how to expand ROTL/ROTR");
3445 break;
3446 }
3447 break;
3448
3449 case ISD::BSWAP:
3450 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3451 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3452 case TargetLowering::Custom:
3453 assert(0 && "Cannot custom legalize this yet!");
3454 case TargetLowering::Legal:
3455 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3456 break;
3457 case TargetLowering::Promote: {
3458 MVT::ValueType OVT = Tmp1.getValueType();
3459 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3460 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3461
3462 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3463 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3464 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3465 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3466 break;
3467 }
3468 case TargetLowering::Expand:
3469 Result = ExpandBSWAP(Tmp1);
3470 break;
3471 }
3472 break;
3473
3474 case ISD::CTPOP:
3475 case ISD::CTTZ:
3476 case ISD::CTLZ:
3477 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3478 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003479 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480 case TargetLowering::Legal:
3481 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003482 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003483 TargetLowering::Custom) {
3484 Tmp1 = TLI.LowerOperation(Result, DAG);
3485 if (Tmp1.Val) {
3486 Result = Tmp1;
3487 }
Scott Michel48b63e62007-07-30 21:00:31 +00003488 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003489 break;
3490 case TargetLowering::Promote: {
3491 MVT::ValueType OVT = Tmp1.getValueType();
3492 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3493
3494 // Zero extend the argument.
3495 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3496 // Perform the larger operation, then subtract if needed.
3497 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3498 switch (Node->getOpcode()) {
3499 case ISD::CTPOP:
3500 Result = Tmp1;
3501 break;
3502 case ISD::CTTZ:
3503 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3504 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3505 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3506 ISD::SETEQ);
3507 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003508 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003509 break;
3510 case ISD::CTLZ:
3511 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3512 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3513 DAG.getConstant(MVT::getSizeInBits(NVT) -
3514 MVT::getSizeInBits(OVT), NVT));
3515 break;
3516 }
3517 break;
3518 }
3519 case TargetLowering::Expand:
3520 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3521 break;
3522 }
3523 break;
3524
3525 // Unary operators
3526 case ISD::FABS:
3527 case ISD::FNEG:
3528 case ISD::FSQRT:
3529 case ISD::FSIN:
3530 case ISD::FCOS:
3531 Tmp1 = LegalizeOp(Node->getOperand(0));
3532 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3533 case TargetLowering::Promote:
3534 case TargetLowering::Custom:
3535 isCustom = true;
3536 // FALLTHROUGH
3537 case TargetLowering::Legal:
3538 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3539 if (isCustom) {
3540 Tmp1 = TLI.LowerOperation(Result, DAG);
3541 if (Tmp1.Val) Result = Tmp1;
3542 }
3543 break;
3544 case TargetLowering::Expand:
3545 switch (Node->getOpcode()) {
3546 default: assert(0 && "Unreachable!");
3547 case ISD::FNEG:
3548 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3549 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3550 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3551 break;
3552 case ISD::FABS: {
3553 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3554 MVT::ValueType VT = Node->getValueType(0);
3555 Tmp2 = DAG.getConstantFP(0.0, VT);
3556 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
3557 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3558 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3559 break;
3560 }
3561 case ISD::FSQRT:
3562 case ISD::FSIN:
3563 case ISD::FCOS: {
3564 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003565
3566 // Expand unsupported unary vector operators by unrolling them.
3567 if (MVT::isVector(VT)) {
3568 Result = LegalizeOp(UnrollVectorOp(Op));
3569 break;
3570 }
3571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003572 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3573 switch(Node->getOpcode()) {
3574 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003575 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3576 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577 break;
3578 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003579 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3580 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 break;
3582 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003583 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3584 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003585 break;
3586 default: assert(0 && "Unreachable!");
3587 }
3588 SDOperand Dummy;
3589 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3590 false/*sign irrelevant*/, Dummy);
3591 break;
3592 }
3593 }
3594 break;
3595 }
3596 break;
3597 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003598 MVT::ValueType VT = Node->getValueType(0);
3599
3600 // Expand unsupported unary vector operators by unrolling them.
3601 if (MVT::isVector(VT)) {
3602 Result = LegalizeOp(UnrollVectorOp(Op));
3603 break;
3604 }
3605
3606 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003607 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3608 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 SDOperand Dummy;
3610 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3611 false/*sign irrelevant*/, Dummy);
3612 break;
3613 }
3614 case ISD::BIT_CONVERT:
3615 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003616 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3617 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003618 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3619 // The input has to be a vector type, we have to either scalarize it, pack
3620 // it, or convert it based on whether the input vector type is legal.
3621 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003622 int InIx = Node->getOperand(0).ResNo;
3623 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3624 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625
3626 // Figure out if there is a simple type corresponding to this Vector
3627 // type. If so, convert to the vector type.
3628 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3629 if (TLI.isTypeLegal(TVT)) {
3630 // Turn this into a bit convert of the vector input.
3631 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3632 LegalizeOp(Node->getOperand(0)));
3633 break;
3634 } else if (NumElems == 1) {
3635 // Turn this into a bit convert of the scalar input.
3636 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3637 ScalarizeVectorOp(Node->getOperand(0)));
3638 break;
3639 } else {
3640 // FIXME: UNIMP! Store then reload
3641 assert(0 && "Cast from unsupported vector type not implemented yet!");
3642 }
3643 } else {
3644 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3645 Node->getOperand(0).getValueType())) {
3646 default: assert(0 && "Unknown operation action!");
3647 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003648 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3649 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003650 break;
3651 case TargetLowering::Legal:
3652 Tmp1 = LegalizeOp(Node->getOperand(0));
3653 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3654 break;
3655 }
3656 }
3657 break;
3658
3659 // Conversion operators. The source and destination have different types.
3660 case ISD::SINT_TO_FP:
3661 case ISD::UINT_TO_FP: {
3662 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3663 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3664 case Legal:
3665 switch (TLI.getOperationAction(Node->getOpcode(),
3666 Node->getOperand(0).getValueType())) {
3667 default: assert(0 && "Unknown operation action!");
3668 case TargetLowering::Custom:
3669 isCustom = true;
3670 // FALLTHROUGH
3671 case TargetLowering::Legal:
3672 Tmp1 = LegalizeOp(Node->getOperand(0));
3673 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3674 if (isCustom) {
3675 Tmp1 = TLI.LowerOperation(Result, DAG);
3676 if (Tmp1.Val) Result = Tmp1;
3677 }
3678 break;
3679 case TargetLowering::Expand:
3680 Result = ExpandLegalINT_TO_FP(isSigned,
3681 LegalizeOp(Node->getOperand(0)),
3682 Node->getValueType(0));
3683 break;
3684 case TargetLowering::Promote:
3685 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3686 Node->getValueType(0),
3687 isSigned);
3688 break;
3689 }
3690 break;
3691 case Expand:
3692 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3693 Node->getValueType(0), Node->getOperand(0));
3694 break;
3695 case Promote:
3696 Tmp1 = PromoteOp(Node->getOperand(0));
3697 if (isSigned) {
3698 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3699 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3700 } else {
3701 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3702 Node->getOperand(0).getValueType());
3703 }
3704 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3705 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3706 break;
3707 }
3708 break;
3709 }
3710 case ISD::TRUNCATE:
3711 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3712 case Legal:
3713 Tmp1 = LegalizeOp(Node->getOperand(0));
3714 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3715 break;
3716 case Expand:
3717 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3718
3719 // Since the result is legal, we should just be able to truncate the low
3720 // part of the source.
3721 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3722 break;
3723 case Promote:
3724 Result = PromoteOp(Node->getOperand(0));
3725 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3726 break;
3727 }
3728 break;
3729
3730 case ISD::FP_TO_SINT:
3731 case ISD::FP_TO_UINT:
3732 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3733 case Legal:
3734 Tmp1 = LegalizeOp(Node->getOperand(0));
3735
3736 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3737 default: assert(0 && "Unknown operation action!");
3738 case TargetLowering::Custom:
3739 isCustom = true;
3740 // FALLTHROUGH
3741 case TargetLowering::Legal:
3742 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3743 if (isCustom) {
3744 Tmp1 = TLI.LowerOperation(Result, DAG);
3745 if (Tmp1.Val) Result = Tmp1;
3746 }
3747 break;
3748 case TargetLowering::Promote:
3749 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3750 Node->getOpcode() == ISD::FP_TO_SINT);
3751 break;
3752 case TargetLowering::Expand:
3753 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3754 SDOperand True, False;
3755 MVT::ValueType VT = Node->getOperand(0).getValueType();
3756 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen280620d2007-09-19 17:53:26 +00003757 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003758 const uint64_t zero[] = {0, 0};
3759 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3760 uint64_t x = 1ULL << ShiftAmt;
Neil Booth4bdd45a2007-10-07 11:45:55 +00003761 (void)apf.convertFromZeroExtendedInteger
3762 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003763 Tmp2 = DAG.getConstantFP(apf, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003764 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3765 Node->getOperand(0), Tmp2, ISD::SETLT);
3766 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3767 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3768 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3769 Tmp2));
3770 False = DAG.getNode(ISD::XOR, NVT, False,
3771 DAG.getConstant(1ULL << ShiftAmt, NVT));
3772 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3773 break;
3774 } else {
3775 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3776 }
3777 break;
3778 }
3779 break;
3780 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003782 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003783 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003784 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003785 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3786 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3787 Node->getOperand(0), DAG.getValueType(MVT::f64));
3788 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3789 DAG.getIntPtrConstant(1));
3790 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3791 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003792 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3793 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3794 Tmp2 = DAG.getConstantFP(apf, OVT);
3795 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3796 // FIXME: generated code sucks.
3797 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3798 DAG.getNode(ISD::ADD, MVT::i32,
3799 DAG.getNode(ISD::FP_TO_SINT, VT,
3800 DAG.getNode(ISD::FSUB, OVT,
3801 Node->getOperand(0), Tmp2)),
3802 DAG.getConstant(0x80000000, MVT::i32)),
3803 DAG.getNode(ISD::FP_TO_SINT, VT,
3804 Node->getOperand(0)),
3805 DAG.getCondCode(ISD::SETGE));
3806 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003807 break;
3808 }
Dale Johannesend3b6af32007-10-11 23:32:15 +00003809 // Convert f32 / f64 to i32 / i64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3811 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003812 case ISD::FP_TO_SINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003813 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003814 LC = (VT == MVT::i32)
3815 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003816 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 LC = (VT == MVT::i32)
3818 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003819 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003820 assert(VT == MVT::i64);
Dale Johannesenac77b272007-10-05 20:04:43 +00003821 LC = RTLIB::FPTOSINT_F80_I64;
3822 }
3823 else if (OVT == MVT::ppcf128) {
3824 assert(VT == MVT::i64);
3825 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003828 }
3829 case ISD::FP_TO_UINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003830 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831 LC = (VT == MVT::i32)
3832 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003833 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 LC = (VT == MVT::i32)
3835 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003836 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003837 LC = (VT == MVT::i32)
Dale Johannesenac77b272007-10-05 20:04:43 +00003838 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3839 }
3840 else if (OVT == MVT::ppcf128) {
3841 assert(VT == MVT::i64);
3842 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003843 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003845 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003846 default: assert(0 && "Unreachable!");
3847 }
3848 SDOperand Dummy;
3849 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3850 false/*sign irrelevant*/, Dummy);
3851 break;
3852 }
3853 case Promote:
3854 Tmp1 = PromoteOp(Node->getOperand(0));
3855 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3856 Result = LegalizeOp(Result);
3857 break;
3858 }
3859 break;
3860
Chris Lattner56ecde32008-01-16 06:57:07 +00003861 case ISD::FP_EXTEND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003862 MVT::ValueType DstVT = Op.getValueType();
3863 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3864 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3865 // The only other way we can lower this is to turn it into a STORE,
3866 // LOAD pair, targetting a temporary location (a stack slot).
3867 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3868 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003869 }
3870 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3871 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3872 case Legal:
3873 Tmp1 = LegalizeOp(Node->getOperand(0));
3874 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3875 break;
3876 case Promote:
3877 Tmp1 = PromoteOp(Node->getOperand(0));
3878 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3879 break;
3880 }
3881 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003882 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003883 case ISD::FP_ROUND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003884 MVT::ValueType DstVT = Op.getValueType();
3885 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3886 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3887 if (SrcVT == MVT::ppcf128) {
Dale Johannesena0d36082008-01-20 01:18:38 +00003888 SDOperand Lo;
3889 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003890 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003891 if (DstVT!=MVT::f64)
3892 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003893 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003894 }
Chris Lattner5872a362008-01-17 07:00:52 +00003895 // The only other way we can lower this is to turn it into a STORE,
3896 // LOAD pair, targetting a temporary location (a stack slot).
3897 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3898 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003900 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3901 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3902 case Legal:
3903 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003904 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003905 break;
3906 case Promote:
3907 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003908 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3909 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003910 break;
3911 }
3912 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003913 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914 case ISD::ANY_EXTEND:
3915 case ISD::ZERO_EXTEND:
3916 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3918 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3919 case Legal:
3920 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac7091c2008-02-15 23:05:48 +00003921 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3922 TargetLowering::Custom) {
3923 Tmp2 = TLI.LowerOperation(Result, DAG);
3924 if (Tmp2.Val) {
3925 Tmp1 = Tmp2;
3926 }
3927 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003928 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3929 break;
3930 case Promote:
3931 switch (Node->getOpcode()) {
3932 case ISD::ANY_EXTEND:
3933 Tmp1 = PromoteOp(Node->getOperand(0));
3934 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3935 break;
3936 case ISD::ZERO_EXTEND:
3937 Result = PromoteOp(Node->getOperand(0));
3938 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3939 Result = DAG.getZeroExtendInReg(Result,
3940 Node->getOperand(0).getValueType());
3941 break;
3942 case ISD::SIGN_EXTEND:
3943 Result = PromoteOp(Node->getOperand(0));
3944 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3945 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3946 Result,
3947 DAG.getValueType(Node->getOperand(0).getValueType()));
3948 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949 }
3950 }
3951 break;
3952 case ISD::FP_ROUND_INREG:
3953 case ISD::SIGN_EXTEND_INREG: {
3954 Tmp1 = LegalizeOp(Node->getOperand(0));
3955 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3956
3957 // If this operation is not supported, convert it to a shl/shr or load/store
3958 // pair.
3959 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3960 default: assert(0 && "This action not supported for this op yet!");
3961 case TargetLowering::Legal:
3962 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3963 break;
3964 case TargetLowering::Expand:
3965 // If this is an integer extend and shifts are supported, do that.
3966 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3967 // NOTE: we could fall back on load/store here too for targets without
3968 // SAR. However, it is doubtful that any exist.
3969 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3970 MVT::getSizeInBits(ExtraVT);
3971 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3972 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3973 Node->getOperand(0), ShiftCst);
3974 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3975 Result, ShiftCst);
3976 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3977 // The only way we can lower this is to turn it into a TRUNCSTORE,
3978 // EXTLOAD pair, targetting a temporary location (a stack slot).
3979
3980 // NOTE: there is a choice here between constantly creating new stack
3981 // slots and always reusing the same one. We currently always create
3982 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003983 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3984 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 } else {
3986 assert(0 && "Unknown op");
3987 }
3988 break;
3989 }
3990 break;
3991 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003992 case ISD::TRAMPOLINE: {
3993 SDOperand Ops[6];
3994 for (unsigned i = 0; i != 6; ++i)
3995 Ops[i] = LegalizeOp(Node->getOperand(i));
3996 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3997 // The only option for this node is to custom lower it.
3998 Result = TLI.LowerOperation(Result, DAG);
3999 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00004000
4001 // Since trampoline produces two values, make sure to remember that we
4002 // legalized both of them.
4003 Tmp1 = LegalizeOp(Result.getValue(1));
4004 Result = LegalizeOp(Result);
4005 AddLegalizedOperand(SDOperand(Node, 0), Result);
4006 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
4007 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004008 }
Dan Gohman819574c2008-01-31 00:41:03 +00004009 case ISD::FLT_ROUNDS_: {
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004010 MVT::ValueType VT = Node->getValueType(0);
4011 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4012 default: assert(0 && "This action not supported for this op yet!");
4013 case TargetLowering::Custom:
4014 Result = TLI.LowerOperation(Op, DAG);
4015 if (Result.Val) break;
4016 // Fall Thru
4017 case TargetLowering::Legal:
4018 // If this operation is not supported, lower it to constant 1
4019 Result = DAG.getConstant(1, VT);
4020 break;
4021 }
4022 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004023 case ISD::TRAP: {
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004024 MVT::ValueType VT = Node->getValueType(0);
4025 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4026 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004027 case TargetLowering::Legal:
4028 Tmp1 = LegalizeOp(Node->getOperand(0));
4029 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4030 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004031 case TargetLowering::Custom:
4032 Result = TLI.LowerOperation(Op, DAG);
4033 if (Result.Val) break;
4034 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004035 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004036 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004037 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004038 TargetLowering::ArgListTy Args;
4039 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004040 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4041 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00004042 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4043 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004044 Result = CallResult.second;
4045 break;
4046 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004047 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004048 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 }
4050
4051 assert(Result.getValueType() == Op.getValueType() &&
4052 "Bad legalization!");
4053
4054 // Make sure that the generated code is itself legal.
4055 if (Result != Op)
4056 Result = LegalizeOp(Result);
4057
4058 // Note that LegalizeOp may be reentered even from single-use nodes, which
4059 // means that we always must cache transformed nodes.
4060 AddLegalizedOperand(Op, Result);
4061 return Result;
4062}
4063
4064/// PromoteOp - Given an operation that produces a value in an invalid type,
4065/// promote it to compute the value into a larger type. The produced value will
4066/// have the correct bits for the low portion of the register, but no guarantee
4067/// is made about the top bits: it may be zero, sign-extended, or garbage.
4068SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4069 MVT::ValueType VT = Op.getValueType();
4070 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4071 assert(getTypeAction(VT) == Promote &&
4072 "Caller should expand or legalize operands that are not promotable!");
4073 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4074 "Cannot promote to smaller type!");
4075
4076 SDOperand Tmp1, Tmp2, Tmp3;
4077 SDOperand Result;
4078 SDNode *Node = Op.Val;
4079
4080 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
4081 if (I != PromotedNodes.end()) return I->second;
4082
4083 switch (Node->getOpcode()) {
4084 case ISD::CopyFromReg:
4085 assert(0 && "CopyFromReg must be legal!");
4086 default:
4087#ifndef NDEBUG
4088 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4089#endif
4090 assert(0 && "Do not know how to promote this operator!");
4091 abort();
4092 case ISD::UNDEF:
4093 Result = DAG.getNode(ISD::UNDEF, NVT);
4094 break;
4095 case ISD::Constant:
4096 if (VT != MVT::i1)
4097 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4098 else
4099 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4100 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4101 break;
4102 case ISD::ConstantFP:
4103 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4104 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4105 break;
4106
4107 case ISD::SETCC:
4108 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
4109 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4110 Node->getOperand(1), Node->getOperand(2));
4111 break;
4112
4113 case ISD::TRUNCATE:
4114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4115 case Legal:
4116 Result = LegalizeOp(Node->getOperand(0));
4117 assert(Result.getValueType() >= NVT &&
4118 "This truncation doesn't make sense!");
4119 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4120 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4121 break;
4122 case Promote:
4123 // The truncation is not required, because we don't guarantee anything
4124 // about high bits anyway.
4125 Result = PromoteOp(Node->getOperand(0));
4126 break;
4127 case Expand:
4128 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4129 // Truncate the low part of the expanded value to the result type
4130 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4131 }
4132 break;
4133 case ISD::SIGN_EXTEND:
4134 case ISD::ZERO_EXTEND:
4135 case ISD::ANY_EXTEND:
4136 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4137 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4138 case Legal:
4139 // Input is legal? Just do extend all the way to the larger type.
4140 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4141 break;
4142 case Promote:
4143 // Promote the reg if it's smaller.
4144 Result = PromoteOp(Node->getOperand(0));
4145 // The high bits are not guaranteed to be anything. Insert an extend.
4146 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4147 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4148 DAG.getValueType(Node->getOperand(0).getValueType()));
4149 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4150 Result = DAG.getZeroExtendInReg(Result,
4151 Node->getOperand(0).getValueType());
4152 break;
4153 }
4154 break;
4155 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004156 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4157 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158 Result = PromoteOp(Result);
4159 break;
4160
4161 case ISD::FP_EXTEND:
4162 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4163 case ISD::FP_ROUND:
4164 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4165 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4166 case Promote: assert(0 && "Unreachable with 2 FP types!");
4167 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004168 if (Node->getConstantOperandVal(1) == 0) {
4169 // Input is legal? Do an FP_ROUND_INREG.
4170 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4171 DAG.getValueType(VT));
4172 } else {
4173 // Just remove the truncate, it isn't affecting the value.
4174 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4175 Node->getOperand(1));
4176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 break;
4178 }
4179 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 case ISD::SINT_TO_FP:
4181 case ISD::UINT_TO_FP:
4182 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4183 case Legal:
4184 // No extra round required here.
4185 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4186 break;
4187
4188 case Promote:
4189 Result = PromoteOp(Node->getOperand(0));
4190 if (Node->getOpcode() == ISD::SINT_TO_FP)
4191 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4192 Result,
4193 DAG.getValueType(Node->getOperand(0).getValueType()));
4194 else
4195 Result = DAG.getZeroExtendInReg(Result,
4196 Node->getOperand(0).getValueType());
4197 // No extra round required here.
4198 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4199 break;
4200 case Expand:
4201 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4202 Node->getOperand(0));
4203 // Round if we cannot tolerate excess precision.
4204 if (NoExcessFPPrecision)
4205 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4206 DAG.getValueType(VT));
4207 break;
4208 }
4209 break;
4210
4211 case ISD::SIGN_EXTEND_INREG:
4212 Result = PromoteOp(Node->getOperand(0));
4213 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4214 Node->getOperand(1));
4215 break;
4216 case ISD::FP_TO_SINT:
4217 case ISD::FP_TO_UINT:
4218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4219 case Legal:
4220 case Expand:
4221 Tmp1 = Node->getOperand(0);
4222 break;
4223 case Promote:
4224 // The input result is prerounded, so we don't have to do anything
4225 // special.
4226 Tmp1 = PromoteOp(Node->getOperand(0));
4227 break;
4228 }
4229 // If we're promoting a UINT to a larger size, check to see if the new node
4230 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4231 // we can use that instead. This allows us to generate better code for
4232 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4233 // legal, such as PowerPC.
4234 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4235 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4236 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4237 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4238 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4239 } else {
4240 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4241 }
4242 break;
4243
4244 case ISD::FABS:
4245 case ISD::FNEG:
4246 Tmp1 = PromoteOp(Node->getOperand(0));
4247 assert(Tmp1.getValueType() == NVT);
4248 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4249 // NOTE: we do not have to do any extra rounding here for
4250 // NoExcessFPPrecision, because we know the input will have the appropriate
4251 // precision, and these operations don't modify precision at all.
4252 break;
4253
4254 case ISD::FSQRT:
4255 case ISD::FSIN:
4256 case ISD::FCOS:
4257 Tmp1 = PromoteOp(Node->getOperand(0));
4258 assert(Tmp1.getValueType() == NVT);
4259 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4260 if (NoExcessFPPrecision)
4261 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4262 DAG.getValueType(VT));
4263 break;
4264
4265 case ISD::FPOWI: {
4266 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4267 // directly as well, which may be better.
4268 Tmp1 = PromoteOp(Node->getOperand(0));
4269 assert(Tmp1.getValueType() == NVT);
4270 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4271 if (NoExcessFPPrecision)
4272 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4273 DAG.getValueType(VT));
4274 break;
4275 }
4276
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004277 case ISD::ATOMIC_LCS: {
4278 Tmp2 = PromoteOp(Node->getOperand(2));
4279 Tmp3 = PromoteOp(Node->getOperand(3));
4280 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4281 Node->getOperand(1), Tmp2, Tmp3,
4282 cast<AtomicSDNode>(Node)->getVT());
4283 // Remember that we legalized the chain.
4284 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4285 break;
4286 }
4287 case ISD::ATOMIC_LAS:
4288 case ISD::ATOMIC_SWAP: {
4289 Tmp2 = PromoteOp(Node->getOperand(2));
4290 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4291 Node->getOperand(1), Tmp2,
4292 cast<AtomicSDNode>(Node)->getVT());
4293 // Remember that we legalized the chain.
4294 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4295 break;
4296 }
4297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298 case ISD::AND:
4299 case ISD::OR:
4300 case ISD::XOR:
4301 case ISD::ADD:
4302 case ISD::SUB:
4303 case ISD::MUL:
4304 // The input may have strange things in the top bits of the registers, but
4305 // these operations don't care. They may have weird bits going out, but
4306 // that too is okay if they are integer operations.
4307 Tmp1 = PromoteOp(Node->getOperand(0));
4308 Tmp2 = PromoteOp(Node->getOperand(1));
4309 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4310 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4311 break;
4312 case ISD::FADD:
4313 case ISD::FSUB:
4314 case ISD::FMUL:
4315 Tmp1 = PromoteOp(Node->getOperand(0));
4316 Tmp2 = PromoteOp(Node->getOperand(1));
4317 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4318 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4319
4320 // Floating point operations will give excess precision that we may not be
4321 // able to tolerate. If we DO allow excess precision, just leave it,
4322 // otherwise excise it.
4323 // FIXME: Why would we need to round FP ops more than integer ones?
4324 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4325 if (NoExcessFPPrecision)
4326 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4327 DAG.getValueType(VT));
4328 break;
4329
4330 case ISD::SDIV:
4331 case ISD::SREM:
4332 // These operators require that their input be sign extended.
4333 Tmp1 = PromoteOp(Node->getOperand(0));
4334 Tmp2 = PromoteOp(Node->getOperand(1));
4335 if (MVT::isInteger(NVT)) {
4336 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4337 DAG.getValueType(VT));
4338 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4339 DAG.getValueType(VT));
4340 }
4341 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4342
4343 // Perform FP_ROUND: this is probably overly pessimistic.
4344 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
4345 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4346 DAG.getValueType(VT));
4347 break;
4348 case ISD::FDIV:
4349 case ISD::FREM:
4350 case ISD::FCOPYSIGN:
4351 // These operators require that their input be fp extended.
4352 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004353 case Expand: assert(0 && "not implemented");
4354 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4355 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 }
4357 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004358 case Expand: assert(0 && "not implemented");
4359 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4360 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361 }
4362 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4363
4364 // Perform FP_ROUND: this is probably overly pessimistic.
4365 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4366 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4367 DAG.getValueType(VT));
4368 break;
4369
4370 case ISD::UDIV:
4371 case ISD::UREM:
4372 // These operators require that their input be zero extended.
4373 Tmp1 = PromoteOp(Node->getOperand(0));
4374 Tmp2 = PromoteOp(Node->getOperand(1));
4375 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
4376 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4377 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4378 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4379 break;
4380
4381 case ISD::SHL:
4382 Tmp1 = PromoteOp(Node->getOperand(0));
4383 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4384 break;
4385 case ISD::SRA:
4386 // The input value must be properly sign extended.
4387 Tmp1 = PromoteOp(Node->getOperand(0));
4388 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4389 DAG.getValueType(VT));
4390 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4391 break;
4392 case ISD::SRL:
4393 // The input value must be properly zero extended.
4394 Tmp1 = PromoteOp(Node->getOperand(0));
4395 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4396 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4397 break;
4398
4399 case ISD::VAARG:
4400 Tmp1 = Node->getOperand(0); // Get the chain.
4401 Tmp2 = Node->getOperand(1); // Get the pointer.
4402 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4403 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4404 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4405 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004406 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4407 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 // Increment the pointer, VAList, to the next vaarg
4409 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4410 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4411 TLI.getPointerTy()));
4412 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004413 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 // Load the actual argument out of the pointer VAList
4415 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4416 }
4417 // Remember that we legalized the chain.
4418 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4419 break;
4420
4421 case ISD::LOAD: {
4422 LoadSDNode *LD = cast<LoadSDNode>(Node);
4423 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4424 ? ISD::EXTLOAD : LD->getExtensionType();
4425 Result = DAG.getExtLoad(ExtType, NVT,
4426 LD->getChain(), LD->getBasePtr(),
4427 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004428 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004429 LD->isVolatile(),
4430 LD->getAlignment());
4431 // Remember that we legalized the chain.
4432 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4433 break;
4434 }
4435 case ISD::SELECT:
4436 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4437 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4438 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4439 break;
4440 case ISD::SELECT_CC:
4441 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4442 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4443 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4444 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4445 break;
4446 case ISD::BSWAP:
4447 Tmp1 = Node->getOperand(0);
4448 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4449 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4450 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4451 DAG.getConstant(MVT::getSizeInBits(NVT) -
4452 MVT::getSizeInBits(VT),
4453 TLI.getShiftAmountTy()));
4454 break;
4455 case ISD::CTPOP:
4456 case ISD::CTTZ:
4457 case ISD::CTLZ:
4458 // Zero extend the argument
4459 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4460 // Perform the larger operation, then subtract if needed.
4461 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4462 switch(Node->getOpcode()) {
4463 case ISD::CTPOP:
4464 Result = Tmp1;
4465 break;
4466 case ISD::CTTZ:
4467 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4468 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
4469 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4470 ISD::SETEQ);
4471 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4472 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4473 break;
4474 case ISD::CTLZ:
4475 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4476 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4477 DAG.getConstant(MVT::getSizeInBits(NVT) -
4478 MVT::getSizeInBits(VT), NVT));
4479 break;
4480 }
4481 break;
4482 case ISD::EXTRACT_SUBVECTOR:
4483 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4484 break;
4485 case ISD::EXTRACT_VECTOR_ELT:
4486 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4487 break;
4488 }
4489
4490 assert(Result.Val && "Didn't set a result!");
4491
4492 // Make sure the result is itself legal.
4493 Result = LegalizeOp(Result);
4494
4495 // Remember that we promoted this!
4496 AddPromotedOperand(Op, Result);
4497 return Result;
4498}
4499
4500/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4501/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4502/// based on the vector type. The return type of this matches the element type
4503/// of the vector, which may not be legal for the target.
4504SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4505 // We know that operand #0 is the Vec vector. If the index is a constant
4506 // or if the invec is a supported hardware type, we can use it. Otherwise,
4507 // lower to a store then an indexed load.
4508 SDOperand Vec = Op.getOperand(0);
4509 SDOperand Idx = Op.getOperand(1);
4510
Dan Gohmana0763d92007-09-24 15:54:53 +00004511 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 unsigned NumElems = MVT::getVectorNumElements(TVT);
4513
4514 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4515 default: assert(0 && "This action is not supported yet!");
4516 case TargetLowering::Custom: {
4517 Vec = LegalizeOp(Vec);
4518 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4519 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4520 if (Tmp3.Val)
4521 return Tmp3;
4522 break;
4523 }
4524 case TargetLowering::Legal:
4525 if (isTypeLegal(TVT)) {
4526 Vec = LegalizeOp(Vec);
4527 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004528 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 }
4530 break;
4531 case TargetLowering::Expand:
4532 break;
4533 }
4534
4535 if (NumElems == 1) {
4536 // This must be an access of the only element. Return it.
4537 Op = ScalarizeVectorOp(Vec);
4538 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004539 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4541 SDOperand Lo, Hi;
4542 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004543 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 Vec = Lo;
4545 } else {
4546 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004547 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548 Idx.getValueType());
4549 }
4550
4551 // It's now an extract from the appropriate high or low part. Recurse.
4552 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4553 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4554 } else {
4555 // Store the value to a temporary stack slot, then LOAD the scalar
4556 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004557 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4559
4560 // Add the offset to the index.
4561 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4562 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4563 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004564
4565 if (MVT::getSizeInBits(Idx.getValueType()) >
4566 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004567 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004568 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004569 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4572
4573 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4574 }
4575 return Op;
4576}
4577
4578/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4579/// we assume the operation can be split if it is not already legal.
4580SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4581 // We know that operand #0 is the Vec vector. For now we assume the index
4582 // is a constant and that the extracted result is a supported hardware type.
4583 SDOperand Vec = Op.getOperand(0);
4584 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4585
4586 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4587
4588 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4589 // This must be an access of the desired vector length. Return it.
4590 return Vec;
4591 }
4592
4593 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4594 SDOperand Lo, Hi;
4595 SplitVectorOp(Vec, Lo, Hi);
4596 if (CIdx->getValue() < NumElems/2) {
4597 Vec = Lo;
4598 } else {
4599 Vec = Hi;
4600 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4601 }
4602
4603 // It's now an extract from the appropriate high or low part. Recurse.
4604 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4605 return ExpandEXTRACT_SUBVECTOR(Op);
4606}
4607
4608/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4609/// with condition CC on the current target. This usually involves legalizing
4610/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4611/// there may be no choice but to create a new SetCC node to represent the
4612/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4613/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4614void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4615 SDOperand &RHS,
4616 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004617 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004618
4619 switch (getTypeAction(LHS.getValueType())) {
4620 case Legal:
4621 Tmp1 = LegalizeOp(LHS); // LHS
4622 Tmp2 = LegalizeOp(RHS); // RHS
4623 break;
4624 case Promote:
4625 Tmp1 = PromoteOp(LHS); // LHS
4626 Tmp2 = PromoteOp(RHS); // RHS
4627
4628 // If this is an FP compare, the operands have already been extended.
4629 if (MVT::isInteger(LHS.getValueType())) {
4630 MVT::ValueType VT = LHS.getValueType();
4631 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4632
4633 // Otherwise, we have to insert explicit sign or zero extends. Note
4634 // that we could insert sign extends for ALL conditions, but zero extend
4635 // is cheaper on many machines (an AND instead of two shifts), so prefer
4636 // it.
4637 switch (cast<CondCodeSDNode>(CC)->get()) {
4638 default: assert(0 && "Unknown integer comparison!");
4639 case ISD::SETEQ:
4640 case ISD::SETNE:
4641 case ISD::SETUGE:
4642 case ISD::SETUGT:
4643 case ISD::SETULE:
4644 case ISD::SETULT:
4645 // ALL of these operations will work if we either sign or zero extend
4646 // the operands (including the unsigned comparisons!). Zero extend is
4647 // usually a simpler/cheaper operation, so prefer it.
4648 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4649 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4650 break;
4651 case ISD::SETGE:
4652 case ISD::SETGT:
4653 case ISD::SETLT:
4654 case ISD::SETLE:
4655 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4656 DAG.getValueType(VT));
4657 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4658 DAG.getValueType(VT));
4659 break;
4660 }
4661 }
4662 break;
4663 case Expand: {
4664 MVT::ValueType VT = LHS.getValueType();
4665 if (VT == MVT::f32 || VT == MVT::f64) {
4666 // Expand into one or more soft-fp libcall(s).
4667 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4668 switch (cast<CondCodeSDNode>(CC)->get()) {
4669 case ISD::SETEQ:
4670 case ISD::SETOEQ:
4671 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4672 break;
4673 case ISD::SETNE:
4674 case ISD::SETUNE:
4675 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4676 break;
4677 case ISD::SETGE:
4678 case ISD::SETOGE:
4679 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4680 break;
4681 case ISD::SETLT:
4682 case ISD::SETOLT:
4683 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4684 break;
4685 case ISD::SETLE:
4686 case ISD::SETOLE:
4687 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4688 break;
4689 case ISD::SETGT:
4690 case ISD::SETOGT:
4691 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4692 break;
4693 case ISD::SETUO:
4694 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4695 break;
4696 case ISD::SETO:
4697 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4698 break;
4699 default:
4700 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4701 switch (cast<CondCodeSDNode>(CC)->get()) {
4702 case ISD::SETONE:
4703 // SETONE = SETOLT | SETOGT
4704 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4705 // Fallthrough
4706 case ISD::SETUGT:
4707 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4708 break;
4709 case ISD::SETUGE:
4710 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4711 break;
4712 case ISD::SETULT:
4713 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4714 break;
4715 case ISD::SETULE:
4716 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4717 break;
4718 case ISD::SETUEQ:
4719 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4720 break;
4721 default: assert(0 && "Unsupported FP setcc!");
4722 }
4723 }
4724
4725 SDOperand Dummy;
4726 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
4727 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4728 false /*sign irrelevant*/, Dummy);
4729 Tmp2 = DAG.getConstant(0, MVT::i32);
4730 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4731 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
4732 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
4733 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
4734 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4735 false /*sign irrelevant*/, Dummy);
4736 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4737 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4738 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4739 Tmp2 = SDOperand();
4740 }
4741 LHS = Tmp1;
4742 RHS = Tmp2;
4743 return;
4744 }
4745
4746 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4747 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004748 ExpandOp(RHS, RHSLo, RHSHi);
4749 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4750
4751 if (VT==MVT::ppcf128) {
4752 // FIXME: This generated code sucks. We want to generate
4753 // FCMP crN, hi1, hi2
4754 // BNE crN, L:
4755 // FCMP crN, lo1, lo2
4756 // The following can be improved, but not that much.
4757 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4758 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4759 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4760 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4761 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4762 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4763 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4764 Tmp2 = SDOperand();
4765 break;
4766 }
4767
4768 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 case ISD::SETEQ:
4770 case ISD::SETNE:
4771 if (RHSLo == RHSHi)
4772 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4773 if (RHSCST->isAllOnesValue()) {
4774 // Comparison to -1.
4775 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4776 Tmp2 = RHSLo;
4777 break;
4778 }
4779
4780 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4781 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4782 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4783 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4784 break;
4785 default:
4786 // If this is a comparison of the sign bit, just look at the top part.
4787 // X > -1, x < 0
4788 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4789 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4790 CST->getValue() == 0) || // X < 0
4791 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4792 CST->isAllOnesValue())) { // X > -1
4793 Tmp1 = LHSHi;
4794 Tmp2 = RHSHi;
4795 break;
4796 }
4797
4798 // FIXME: This generated code sucks.
4799 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004800 switch (CCCode) {
4801 default: assert(0 && "Unknown integer setcc!");
4802 case ISD::SETLT:
4803 case ISD::SETULT: LowCC = ISD::SETULT; break;
4804 case ISD::SETGT:
4805 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4806 case ISD::SETLE:
4807 case ISD::SETULE: LowCC = ISD::SETULE; break;
4808 case ISD::SETGE:
4809 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4810 }
4811
4812 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4813 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4814 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4815
4816 // NOTE: on targets without efficient SELECT of bools, we can always use
4817 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4818 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4819 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4820 false, DagCombineInfo);
4821 if (!Tmp1.Val)
4822 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4823 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4824 CCCode, false, DagCombineInfo);
4825 if (!Tmp2.Val)
Chris Lattner6fb53da2007-10-15 17:48:57 +00004826 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827
4828 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4829 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4830 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4831 (Tmp2C && Tmp2C->getValue() == 0 &&
4832 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4833 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4834 (Tmp2C && Tmp2C->getValue() == 1 &&
4835 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4836 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4837 // low part is known false, returns high part.
4838 // For LE / GE, if high part is known false, ignore the low part.
4839 // For LT / GT, if high part is known true, ignore the low part.
4840 Tmp1 = Tmp2;
4841 Tmp2 = SDOperand();
4842 } else {
4843 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4844 ISD::SETEQ, false, DagCombineInfo);
4845 if (!Result.Val)
4846 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4847 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4848 Result, Tmp1, Tmp2));
4849 Tmp1 = Result;
4850 Tmp2 = SDOperand();
4851 }
4852 }
4853 }
4854 }
4855 LHS = Tmp1;
4856 RHS = Tmp2;
4857}
4858
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004859/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4860/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4861/// a load from the stack slot to DestVT, extending it if needed.
4862/// The resultant code need not be legal.
4863SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4864 MVT::ValueType SlotVT,
4865 MVT::ValueType DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866 // Create the stack frame object.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004867 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4868
Dan Gohman20e37962008-02-11 18:58:42 +00004869 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004870 int SPFI = StackPtrFI->getIndex();
4871
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004872 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4873 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4874 unsigned DestSize = MVT::getSizeInBits(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004876 // Emit a store to the stack slot. Use a truncstore if the input value is
4877 // later than DestVT.
4878 SDOperand Store;
4879 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004880 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004881 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004882 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004883 else {
4884 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004885 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004886 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004887 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004888 }
4889
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004891 if (SlotSize == DestSize)
4892 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4893
4894 assert(SlotSize < DestSize && "Unknown extension!");
4895 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004896}
4897
4898SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4899 // Create a vector sized/aligned stack slot, store the value to element #0,
4900 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004901 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00004902
Dan Gohman20e37962008-02-11 18:58:42 +00004903 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004904 int SPFI = StackPtrFI->getIndex();
4905
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004907 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman12a9c082008-02-06 22:27:42 +00004908 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004909 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004910}
4911
4912
4913/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4914/// support the operation, but do support the resultant vector type.
4915SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4916
4917 // If the only non-undef value is the low element, turn this into a
4918 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4919 unsigned NumElems = Node->getNumOperands();
4920 bool isOnlyLowElement = true;
4921 SDOperand SplatValue = Node->getOperand(0);
4922 std::map<SDOperand, std::vector<unsigned> > Values;
4923 Values[SplatValue].push_back(0);
4924 bool isConstant = true;
4925 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4926 SplatValue.getOpcode() != ISD::UNDEF)
4927 isConstant = false;
4928
4929 for (unsigned i = 1; i < NumElems; ++i) {
4930 SDOperand V = Node->getOperand(i);
4931 Values[V].push_back(i);
4932 if (V.getOpcode() != ISD::UNDEF)
4933 isOnlyLowElement = false;
4934 if (SplatValue != V)
4935 SplatValue = SDOperand(0,0);
4936
4937 // If this isn't a constant element or an undef, we can't use a constant
4938 // pool load.
4939 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4940 V.getOpcode() != ISD::UNDEF)
4941 isConstant = false;
4942 }
4943
4944 if (isOnlyLowElement) {
4945 // If the low element is an undef too, then this whole things is an undef.
4946 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4947 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4948 // Otherwise, turn this into a scalar_to_vector node.
4949 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4950 Node->getOperand(0));
4951 }
4952
4953 // If all elements are constants, create a load from the constant pool.
4954 if (isConstant) {
4955 MVT::ValueType VT = Node->getValueType(0);
4956 const Type *OpNTy =
4957 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4958 std::vector<Constant*> CV;
4959 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4960 if (ConstantFPSDNode *V =
4961 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00004962 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 } else if (ConstantSDNode *V =
4964 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4965 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4966 } else {
4967 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4968 CV.push_back(UndefValue::get(OpNTy));
4969 }
4970 }
4971 Constant *CP = ConstantVector::get(CV);
4972 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004973 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004974 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004975 }
4976
4977 if (SplatValue.Val) { // Splat of one value?
4978 // Build the shuffle constant vector: <0, 0, 0, 0>
4979 MVT::ValueType MaskVT =
4980 MVT::getIntVectorWithNumElements(NumElems);
4981 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4982 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4983 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4984 &ZeroVec[0], ZeroVec.size());
4985
4986 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4987 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4988 // Get the splatted value into the low element of a vector register.
4989 SDOperand LowValVec =
4990 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4991
4992 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4993 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4994 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4995 SplatMask);
4996 }
4997 }
4998
4999 // If there are only two unique elements, we may be able to turn this into a
5000 // vector shuffle.
5001 if (Values.size() == 2) {
5002 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
5003 MVT::ValueType MaskVT =
5004 MVT::getIntVectorWithNumElements(NumElems);
5005 std::vector<SDOperand> MaskVec(NumElems);
5006 unsigned i = 0;
5007 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5008 E = Values.end(); I != E; ++I) {
5009 for (std::vector<unsigned>::iterator II = I->second.begin(),
5010 EE = I->second.end(); II != EE; ++II)
5011 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
5012 i += NumElems;
5013 }
5014 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5015 &MaskVec[0], MaskVec.size());
5016
5017 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5018 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5019 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
5020 SmallVector<SDOperand, 8> Ops;
5021 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5022 E = Values.end(); I != E; ++I) {
5023 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5024 I->first);
5025 Ops.push_back(Op);
5026 }
5027 Ops.push_back(ShuffleMask);
5028
5029 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
5030 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5031 &Ops[0], Ops.size());
5032 }
5033 }
5034
5035 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5036 // aligned object on the stack, store each element into it, then load
5037 // the result as a vector.
5038 MVT::ValueType VT = Node->getValueType(0);
5039 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00005040 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005041
5042 // Emit a store of each element to the stack slot.
5043 SmallVector<SDOperand, 8> Stores;
5044 unsigned TypeByteSize =
5045 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
5046 // Store (in the right endianness) the elements to memory.
5047 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5048 // Ignore undef elements.
5049 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5050
5051 unsigned Offset = TypeByteSize*i;
5052
5053 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5054 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5055
5056 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5057 NULL, 0));
5058 }
5059
5060 SDOperand StoreChain;
5061 if (!Stores.empty()) // Not all undef elements?
5062 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5063 &Stores[0], Stores.size());
5064 else
5065 StoreChain = DAG.getEntryNode();
5066
5067 // Result is a load from the stack slot.
5068 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5069}
5070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005071void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5072 SDOperand Op, SDOperand Amt,
5073 SDOperand &Lo, SDOperand &Hi) {
5074 // Expand the subcomponents.
5075 SDOperand LHSL, LHSH;
5076 ExpandOp(Op, LHSL, LHSH);
5077
5078 SDOperand Ops[] = { LHSL, LHSH, Amt };
5079 MVT::ValueType VT = LHSL.getValueType();
5080 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5081 Hi = Lo.getValue(1);
5082}
5083
5084
5085/// ExpandShift - Try to find a clever way to expand this shift operation out to
5086/// smaller elements. If we can't find a way that is more efficient than a
5087/// libcall on this target, return false. Otherwise, return true with the
5088/// low-parts expanded into Lo and Hi.
5089bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5090 SDOperand &Lo, SDOperand &Hi) {
5091 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5092 "This is not a shift!");
5093
5094 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
5095 SDOperand ShAmt = LegalizeOp(Amt);
5096 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohmanece0a882008-02-20 16:57:27 +00005097 unsigned ShBits = MVT::getSizeInBits(ShTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005098 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5099 unsigned NVTBits = MVT::getSizeInBits(NVT);
5100
Chris Lattner8c931452007-10-14 20:35:12 +00005101 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005102 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5103 unsigned Cst = CN->getValue();
5104 // Expand the incoming operand to be shifted, so that we have its parts
5105 SDOperand InL, InH;
5106 ExpandOp(Op, InL, InH);
5107 switch(Opc) {
5108 case ISD::SHL:
5109 if (Cst > VTBits) {
5110 Lo = DAG.getConstant(0, NVT);
5111 Hi = DAG.getConstant(0, NVT);
5112 } else if (Cst > NVTBits) {
5113 Lo = DAG.getConstant(0, NVT);
5114 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5115 } else if (Cst == NVTBits) {
5116 Lo = DAG.getConstant(0, NVT);
5117 Hi = InL;
5118 } else {
5119 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5120 Hi = DAG.getNode(ISD::OR, NVT,
5121 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5122 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5123 }
5124 return true;
5125 case ISD::SRL:
5126 if (Cst > VTBits) {
5127 Lo = DAG.getConstant(0, NVT);
5128 Hi = DAG.getConstant(0, NVT);
5129 } else if (Cst > NVTBits) {
5130 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5131 Hi = DAG.getConstant(0, NVT);
5132 } else if (Cst == NVTBits) {
5133 Lo = InH;
5134 Hi = DAG.getConstant(0, NVT);
5135 } else {
5136 Lo = DAG.getNode(ISD::OR, NVT,
5137 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5138 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5139 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5140 }
5141 return true;
5142 case ISD::SRA:
5143 if (Cst > VTBits) {
5144 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5145 DAG.getConstant(NVTBits-1, ShTy));
5146 } else if (Cst > NVTBits) {
5147 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5148 DAG.getConstant(Cst-NVTBits, ShTy));
5149 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5150 DAG.getConstant(NVTBits-1, ShTy));
5151 } else if (Cst == NVTBits) {
5152 Lo = InH;
5153 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5154 DAG.getConstant(NVTBits-1, ShTy));
5155 } else {
5156 Lo = DAG.getNode(ISD::OR, NVT,
5157 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5158 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5159 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5160 }
5161 return true;
5162 }
5163 }
5164
5165 // Okay, the shift amount isn't constant. However, if we can tell that it is
5166 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005167 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5168 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005169 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5170
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005171 // If we know that if any of the high bits of the shift amount are one, then
5172 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005173 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174 // Mask out the high bit, which we know is set.
5175 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005176 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177
5178 // Expand the incoming operand to be shifted, so that we have its parts
5179 SDOperand InL, InH;
5180 ExpandOp(Op, InL, InH);
5181 switch(Opc) {
5182 case ISD::SHL:
5183 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5184 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5185 return true;
5186 case ISD::SRL:
5187 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5188 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5189 return true;
5190 case ISD::SRA:
5191 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5192 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5193 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5194 return true;
5195 }
5196 }
5197
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005198 // If we know that the high bits of the shift amount are all zero, then we can
5199 // do this as a couple of simple shifts.
5200 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005201 // Compute 32-amt.
5202 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5203 DAG.getConstant(NVTBits, Amt.getValueType()),
5204 Amt);
5205
5206 // Expand the incoming operand to be shifted, so that we have its parts
5207 SDOperand InL, InH;
5208 ExpandOp(Op, InL, InH);
5209 switch(Opc) {
5210 case ISD::SHL:
5211 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5212 Hi = DAG.getNode(ISD::OR, NVT,
5213 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5214 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5215 return true;
5216 case ISD::SRL:
5217 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5218 Lo = DAG.getNode(ISD::OR, NVT,
5219 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5220 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5221 return true;
5222 case ISD::SRA:
5223 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5224 Lo = DAG.getNode(ISD::OR, NVT,
5225 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5226 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5227 return true;
5228 }
5229 }
5230
5231 return false;
5232}
5233
5234
5235// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5236// does not fit into a register, return the lo part and set the hi part to the
5237// by-reg argument. If it does fit into a single register, return the result
5238// and leave the Hi part unset.
5239SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
5240 bool isSigned, SDOperand &Hi) {
5241 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5242 // The input chain to this libcall is the entry node of the function.
5243 // Legalizing the call will automatically add the previous call to the
5244 // dependence.
5245 SDOperand InChain = DAG.getEntryNode();
5246
5247 TargetLowering::ArgListTy Args;
5248 TargetLowering::ArgListEntry Entry;
5249 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5250 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5251 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
5252 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5253 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005254 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255 Args.push_back(Entry);
5256 }
5257 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
5258
5259 // Splice the libcall in wherever FindInputOutputChains tells us to.
5260 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
5261 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005262 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5263 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005264
5265 // Legalize the call sequence, starting with the chain. This will advance
5266 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5267 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5268 LegalizeOp(CallInfo.second);
5269 SDOperand Result;
5270 switch (getTypeAction(CallInfo.first.getValueType())) {
5271 default: assert(0 && "Unknown thing");
5272 case Legal:
5273 Result = CallInfo.first;
5274 break;
5275 case Expand:
5276 ExpandOp(CallInfo.first, Result, Hi);
5277 break;
5278 }
5279 return Result;
5280}
5281
5282
5283/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5284///
5285SDOperand SelectionDAGLegalize::
5286ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
5287 assert(getTypeAction(Source.getValueType()) == Expand &&
5288 "This is not an expansion!");
5289 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5290
5291 if (!isSigned) {
5292 assert(Source.getValueType() == MVT::i64 &&
5293 "This only works for 64-bit -> FP");
5294 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5295 // incoming integer is set. To handle this, we dynamically test to see if
5296 // it is set, and, if so, add a fudge factor.
5297 SDOperand Lo, Hi;
5298 ExpandOp(Source, Lo, Hi);
5299
5300 // If this is unsigned, and not supported, first perform the conversion to
5301 // signed, then adjust the result if the sign bit is set.
5302 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5303 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5304
5305 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5306 DAG.getConstant(0, Hi.getValueType()),
5307 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005308 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5310 SignSet, Four, Zero);
5311 uint64_t FF = 0x5f800000ULL;
5312 if (TLI.isLittleEndian()) FF <<= 32;
5313 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5314
5315 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5316 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5317 SDOperand FudgeInReg;
5318 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005319 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005320 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005321 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005323 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005324 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005325 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005326 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005327 else
5328 assert(0 && "Unexpected conversion");
5329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330 MVT::ValueType SCVT = SignedConv.getValueType();
5331 if (SCVT != DestTy) {
5332 // Destination type needs to be expanded as well. The FADD now we are
5333 // constructing will be expanded into a libcall.
5334 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5335 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5336 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5337 SignedConv, SignedConv.getValue(1));
5338 }
5339 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5340 }
5341 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5342 }
5343
5344 // Check to see if the target has a custom way to lower this. If so, use it.
5345 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5346 default: assert(0 && "This action not implemented for this operation!");
5347 case TargetLowering::Legal:
5348 case TargetLowering::Expand:
5349 break; // This case is handled below.
5350 case TargetLowering::Custom: {
5351 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5352 Source), DAG);
5353 if (NV.Val)
5354 return LegalizeOp(NV);
5355 break; // The target decided this was legal after all
5356 }
5357 }
5358
5359 // Expand the source, then glue it back together for the call. We must expand
5360 // the source in case it is shared (this pass of legalize must traverse it).
5361 SDOperand SrcLo, SrcHi;
5362 ExpandOp(Source, SrcLo, SrcHi);
5363 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5364
5365 RTLIB::Libcall LC;
5366 if (DestTy == MVT::f32)
5367 LC = RTLIB::SINTTOFP_I64_F32;
5368 else {
5369 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5370 LC = RTLIB::SINTTOFP_I64_F64;
5371 }
5372
5373 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
5374 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5375 SDOperand UnusedHiPart;
5376 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5377 UnusedHiPart);
5378}
5379
5380/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5381/// INT_TO_FP operation of the specified operand when the target requests that
5382/// we expand it. At this point, we know that the result and operand types are
5383/// legal for the target.
5384SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5385 SDOperand Op0,
5386 MVT::ValueType DestVT) {
5387 if (Op0.getValueType() == MVT::i32) {
5388 // simple 32-bit [signed|unsigned] integer to float/double expansion
5389
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005390 // Get the stack frame index of a 8 byte buffer.
5391 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005393 // word offset constant for Hi/Lo address computation
5394 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5395 // set up Hi and Lo (into buffer) address based on endian
5396 SDOperand Hi = StackSlot;
5397 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5398 if (TLI.isLittleEndian())
5399 std::swap(Hi, Lo);
5400
5401 // if signed map to unsigned space
5402 SDOperand Op0Mapped;
5403 if (isSigned) {
5404 // constant used to invert sign bit (signed to unsigned mapping)
5405 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5406 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5407 } else {
5408 Op0Mapped = Op0;
5409 }
5410 // store the lo of the constructed double - based on integer input
5411 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5412 Op0Mapped, Lo, NULL, 0);
5413 // initial hi portion of constructed double
5414 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5415 // store the hi of the constructed double - biased exponent
5416 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5417 // load the constructed double
5418 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5419 // FP constant to bias correct the final result
5420 SDOperand Bias = DAG.getConstantFP(isSigned ?
5421 BitsToDouble(0x4330000080000000ULL)
5422 : BitsToDouble(0x4330000000000000ULL),
5423 MVT::f64);
5424 // subtract the bias
5425 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5426 // final result
5427 SDOperand Result;
5428 // handle final rounding
5429 if (DestVT == MVT::f64) {
5430 // do nothing
5431 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005432 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005433 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5434 DAG.getIntPtrConstant(0));
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005435 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5436 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005437 }
5438 return Result;
5439 }
5440 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5441 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5442
5443 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5444 DAG.getConstant(0, Op0.getValueType()),
5445 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005446 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005447 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5448 SignSet, Four, Zero);
5449
5450 // If the sign bit of the integer is set, the large number will be treated
5451 // as a negative number. To counteract this, the dynamic code adds an
5452 // offset depending on the data type.
5453 uint64_t FF;
5454 switch (Op0.getValueType()) {
5455 default: assert(0 && "Unsupported integer type!");
5456 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5457 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5458 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5459 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5460 }
5461 if (TLI.isLittleEndian()) FF <<= 32;
5462 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5463
5464 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5465 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5466 SDOperand FudgeInReg;
5467 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005468 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005469 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005470 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005471 FudgeInReg =
5472 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5473 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005474 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005475 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 }
5477
5478 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5479}
5480
5481/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5482/// *INT_TO_FP operation of the specified operand when the target requests that
5483/// we promote it. At this point, we know that the result and operand types are
5484/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5485/// operation that takes a larger input.
5486SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5487 MVT::ValueType DestVT,
5488 bool isSigned) {
5489 // First step, figure out the appropriate *INT_TO_FP operation to use.
5490 MVT::ValueType NewInTy = LegalOp.getValueType();
5491
5492 unsigned OpToUse = 0;
5493
5494 // Scan for the appropriate larger type to use.
5495 while (1) {
5496 NewInTy = (MVT::ValueType)(NewInTy+1);
5497 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5498
5499 // If the target supports SINT_TO_FP of this type, use it.
5500 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5501 default: break;
5502 case TargetLowering::Legal:
5503 if (!TLI.isTypeLegal(NewInTy))
5504 break; // Can't use this datatype.
5505 // FALL THROUGH.
5506 case TargetLowering::Custom:
5507 OpToUse = ISD::SINT_TO_FP;
5508 break;
5509 }
5510 if (OpToUse) break;
5511 if (isSigned) continue;
5512
5513 // If the target supports UINT_TO_FP of this type, use it.
5514 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5515 default: break;
5516 case TargetLowering::Legal:
5517 if (!TLI.isTypeLegal(NewInTy))
5518 break; // Can't use this datatype.
5519 // FALL THROUGH.
5520 case TargetLowering::Custom:
5521 OpToUse = ISD::UINT_TO_FP;
5522 break;
5523 }
5524 if (OpToUse) break;
5525
5526 // Otherwise, try a larger type.
5527 }
5528
5529 // Okay, we found the operation and type to use. Zero extend our input to the
5530 // desired type then run the operation on it.
5531 return DAG.getNode(OpToUse, DestVT,
5532 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5533 NewInTy, LegalOp));
5534}
5535
5536/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5537/// FP_TO_*INT operation of the specified operand when the target requests that
5538/// we promote it. At this point, we know that the result and operand types are
5539/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5540/// operation that returns a larger result.
5541SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5542 MVT::ValueType DestVT,
5543 bool isSigned) {
5544 // First step, figure out the appropriate FP_TO*INT operation to use.
5545 MVT::ValueType NewOutTy = DestVT;
5546
5547 unsigned OpToUse = 0;
5548
5549 // Scan for the appropriate larger type to use.
5550 while (1) {
5551 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5552 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5553
5554 // If the target supports FP_TO_SINT returning this type, use it.
5555 switch (TLI.getOperationAction(ISD::FP_TO_SINT, 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_SINT;
5563 break;
5564 }
5565 if (OpToUse) break;
5566
5567 // If the target supports FP_TO_UINT of this type, use it.
5568 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5569 default: break;
5570 case TargetLowering::Legal:
5571 if (!TLI.isTypeLegal(NewOutTy))
5572 break; // Can't use this datatype.
5573 // FALL THROUGH.
5574 case TargetLowering::Custom:
5575 OpToUse = ISD::FP_TO_UINT;
5576 break;
5577 }
5578 if (OpToUse) break;
5579
5580 // Otherwise, try a larger type.
5581 }
5582
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005583
5584 // Okay, we found the operation and type to use.
5585 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5586
5587 // If the operation produces an invalid type, it must be custom lowered. Use
5588 // the target lowering hooks to expand it. Just keep the low part of the
5589 // expanded operation, we know that we're truncating anyway.
5590 if (getTypeAction(NewOutTy) == Expand) {
5591 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5592 assert(Operation.Val && "Didn't return anything");
5593 }
5594
5595 // Truncate the result of the extended FP_TO_*INT operation to the desired
5596 // size.
5597 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598}
5599
5600/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5601///
5602SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5603 MVT::ValueType VT = Op.getValueType();
5604 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5605 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5606 switch (VT) {
5607 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5608 case MVT::i16:
5609 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5610 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5611 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5612 case MVT::i32:
5613 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5614 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5615 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5616 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5617 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5618 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5619 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5620 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5621 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5622 case MVT::i64:
5623 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5624 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5625 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5626 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5627 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5628 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5629 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5630 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5631 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5632 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5633 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5634 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5635 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5636 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5637 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5638 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5639 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5640 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5641 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5642 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5643 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5644 }
5645}
5646
5647/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5648///
5649SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5650 switch (Opc) {
5651 default: assert(0 && "Cannot expand this yet!");
5652 case ISD::CTPOP: {
5653 static const uint64_t mask[6] = {
5654 0x5555555555555555ULL, 0x3333333333333333ULL,
5655 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5656 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5657 };
5658 MVT::ValueType VT = Op.getValueType();
5659 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5660 unsigned len = MVT::getSizeInBits(VT);
5661 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5662 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5663 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5664 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5665 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5666 DAG.getNode(ISD::AND, VT,
5667 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5668 }
5669 return Op;
5670 }
5671 case ISD::CTLZ: {
5672 // for now, we do this:
5673 // x = x | (x >> 1);
5674 // x = x | (x >> 2);
5675 // ...
5676 // x = x | (x >>16);
5677 // x = x | (x >>32); // for 64-bit input
5678 // return popcount(~x);
5679 //
5680 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5681 MVT::ValueType VT = Op.getValueType();
5682 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5683 unsigned len = MVT::getSizeInBits(VT);
5684 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5685 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5686 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5687 }
5688 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5689 return DAG.getNode(ISD::CTPOP, VT, Op);
5690 }
5691 case ISD::CTTZ: {
5692 // for now, we use: { return popcount(~x & (x - 1)); }
5693 // unless the target has ctlz but not ctpop, in which case we use:
5694 // { return 32 - nlz(~x & (x-1)); }
5695 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5696 MVT::ValueType VT = Op.getValueType();
5697 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5698 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5699 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5700 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5701 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5702 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5703 TLI.isOperationLegal(ISD::CTLZ, VT))
5704 return DAG.getNode(ISD::SUB, VT,
5705 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5706 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5707 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5708 }
5709 }
5710}
5711
5712/// ExpandOp - Expand the specified SDOperand into its two component pieces
5713/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5714/// LegalizeNodes map is filled in for any results that are not expanded, the
5715/// ExpandedNodes map is filled in for any results that are expanded, and the
5716/// Lo/Hi values are returned.
5717void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5718 MVT::ValueType VT = Op.getValueType();
5719 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5720 SDNode *Node = Op.Val;
5721 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5722 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5723 MVT::isVector(VT)) &&
5724 "Cannot expand to FP value or to larger int value!");
5725
5726 // See if we already expanded it.
5727 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5728 = ExpandedNodes.find(Op);
5729 if (I != ExpandedNodes.end()) {
5730 Lo = I->second.first;
5731 Hi = I->second.second;
5732 return;
5733 }
5734
5735 switch (Node->getOpcode()) {
5736 case ISD::CopyFromReg:
5737 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005738 case ISD::FP_ROUND_INREG:
5739 if (VT == MVT::ppcf128 &&
5740 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5741 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005742 SDOperand SrcLo, SrcHi, Src;
5743 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5744 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5745 SDOperand Result = TLI.LowerOperation(
5746 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005747 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5748 Lo = Result.Val->getOperand(0);
5749 Hi = Result.Val->getOperand(1);
5750 break;
5751 }
5752 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005753 default:
5754#ifndef NDEBUG
5755 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5756#endif
5757 assert(0 && "Do not know how to expand this operator!");
5758 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00005759 case ISD::EXTRACT_ELEMENT:
5760 ExpandOp(Node->getOperand(0), Lo, Hi);
5761 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5762 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00005763 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005764 case ISD::EXTRACT_VECTOR_ELT:
5765 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5766 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5767 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5768 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005769 case ISD::UNDEF:
5770 NVT = TLI.getTypeToExpandTo(VT);
5771 Lo = DAG.getNode(ISD::UNDEF, NVT);
5772 Hi = DAG.getNode(ISD::UNDEF, NVT);
5773 break;
5774 case ISD::Constant: {
5775 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5776 Lo = DAG.getConstant(Cst, NVT);
5777 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5778 break;
5779 }
5780 case ISD::ConstantFP: {
5781 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005782 if (CFP->getValueType(0) == MVT::ppcf128) {
5783 APInt api = CFP->getValueAPF().convertToAPInt();
5784 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5785 MVT::f64);
5786 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5787 MVT::f64);
5788 break;
5789 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005790 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5791 if (getTypeAction(Lo.getValueType()) == Expand)
5792 ExpandOp(Lo, Lo, Hi);
5793 break;
5794 }
5795 case ISD::BUILD_PAIR:
5796 // Return the operands.
5797 Lo = Node->getOperand(0);
5798 Hi = Node->getOperand(1);
5799 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005800
5801 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005802 if (Node->getNumValues() == 1) {
5803 ExpandOp(Op.getOperand(0), Lo, Hi);
5804 break;
5805 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005806 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5807 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5808 Op.getValue(1).getValueType() == MVT::Other &&
5809 "unhandled MERGE_VALUES");
5810 ExpandOp(Op.getOperand(0), Lo, Hi);
5811 // Remember that we legalized the chain.
5812 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5813 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005814
5815 case ISD::SIGN_EXTEND_INREG:
5816 ExpandOp(Node->getOperand(0), Lo, Hi);
5817 // sext_inreg the low part if needed.
5818 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5819
5820 // The high part gets the sign extension from the lo-part. This handles
5821 // things like sextinreg V:i64 from i8.
5822 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5823 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5824 TLI.getShiftAmountTy()));
5825 break;
5826
5827 case ISD::BSWAP: {
5828 ExpandOp(Node->getOperand(0), Lo, Hi);
5829 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5830 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5831 Lo = TempLo;
5832 break;
5833 }
5834
5835 case ISD::CTPOP:
5836 ExpandOp(Node->getOperand(0), Lo, Hi);
5837 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5838 DAG.getNode(ISD::CTPOP, NVT, Lo),
5839 DAG.getNode(ISD::CTPOP, NVT, Hi));
5840 Hi = DAG.getConstant(0, NVT);
5841 break;
5842
5843 case ISD::CTLZ: {
5844 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5845 ExpandOp(Node->getOperand(0), Lo, Hi);
5846 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5847 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5848 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5849 ISD::SETNE);
5850 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5851 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5852
5853 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5854 Hi = DAG.getConstant(0, NVT);
5855 break;
5856 }
5857
5858 case ISD::CTTZ: {
5859 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5860 ExpandOp(Node->getOperand(0), Lo, Hi);
5861 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5862 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5863 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5864 ISD::SETNE);
5865 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5866 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5867
5868 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5869 Hi = DAG.getConstant(0, NVT);
5870 break;
5871 }
5872
5873 case ISD::VAARG: {
5874 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5875 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5876 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5877 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5878
5879 // Remember that we legalized the chain.
5880 Hi = LegalizeOp(Hi);
5881 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005882 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005883 std::swap(Lo, Hi);
5884 break;
5885 }
5886
5887 case ISD::LOAD: {
5888 LoadSDNode *LD = cast<LoadSDNode>(Node);
5889 SDOperand Ch = LD->getChain(); // Legalize the chain.
5890 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5891 ISD::LoadExtType ExtType = LD->getExtensionType();
5892 int SVOffset = LD->getSrcValueOffset();
5893 unsigned Alignment = LD->getAlignment();
5894 bool isVolatile = LD->isVolatile();
5895
5896 if (ExtType == ISD::NON_EXTLOAD) {
5897 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5898 isVolatile, Alignment);
5899 if (VT == MVT::f32 || VT == MVT::f64) {
5900 // f32->i32 or f64->i64 one to one expansion.
5901 // Remember that we legalized the chain.
5902 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5903 // Recursively expand the new load.
5904 if (getTypeAction(NVT) == Expand)
5905 ExpandOp(Lo, Lo, Hi);
5906 break;
5907 }
5908
5909 // Increment the pointer to the other half.
5910 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5911 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005912 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005913 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005914 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005915 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5916 isVolatile, Alignment);
5917
5918 // Build a factor node to remember that this load is independent of the
5919 // other one.
5920 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5921 Hi.getValue(1));
5922
5923 // Remember that we legalized the chain.
5924 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005925 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005926 std::swap(Lo, Hi);
5927 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005928 MVT::ValueType EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005929
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005930 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5931 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005932 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5933 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5934 SVOffset, isVolatile, Alignment);
5935 // Remember that we legalized the chain.
5936 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5937 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5938 break;
5939 }
5940
5941 if (EVT == NVT)
5942 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5943 SVOffset, isVolatile, Alignment);
5944 else
5945 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5946 SVOffset, EVT, isVolatile,
5947 Alignment);
5948
5949 // Remember that we legalized the chain.
5950 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5951
5952 if (ExtType == ISD::SEXTLOAD) {
5953 // The high part is obtained by SRA'ing all but one of the bits of the
5954 // lo part.
5955 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5956 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5957 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5958 } else if (ExtType == ISD::ZEXTLOAD) {
5959 // The high part is just a zero.
5960 Hi = DAG.getConstant(0, NVT);
5961 } else /* if (ExtType == ISD::EXTLOAD) */ {
5962 // The high part is undefined.
5963 Hi = DAG.getNode(ISD::UNDEF, NVT);
5964 }
5965 }
5966 break;
5967 }
5968 case ISD::AND:
5969 case ISD::OR:
5970 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5971 SDOperand LL, LH, RL, RH;
5972 ExpandOp(Node->getOperand(0), LL, LH);
5973 ExpandOp(Node->getOperand(1), RL, RH);
5974 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5975 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5976 break;
5977 }
5978 case ISD::SELECT: {
5979 SDOperand LL, LH, RL, RH;
5980 ExpandOp(Node->getOperand(1), LL, LH);
5981 ExpandOp(Node->getOperand(2), RL, RH);
5982 if (getTypeAction(NVT) == Expand)
5983 NVT = TLI.getTypeToExpandTo(NVT);
5984 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5985 if (VT != MVT::f32)
5986 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5987 break;
5988 }
5989 case ISD::SELECT_CC: {
5990 SDOperand TL, TH, FL, FH;
5991 ExpandOp(Node->getOperand(2), TL, TH);
5992 ExpandOp(Node->getOperand(3), FL, FH);
5993 if (getTypeAction(NVT) == Expand)
5994 NVT = TLI.getTypeToExpandTo(NVT);
5995 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5996 Node->getOperand(1), TL, FL, Node->getOperand(4));
5997 if (VT != MVT::f32)
5998 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5999 Node->getOperand(1), TH, FH, Node->getOperand(4));
6000 break;
6001 }
6002 case ISD::ANY_EXTEND:
6003 // The low part is any extension of the input (which degenerates to a copy).
6004 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6005 // The high part is undefined.
6006 Hi = DAG.getNode(ISD::UNDEF, NVT);
6007 break;
6008 case ISD::SIGN_EXTEND: {
6009 // The low part is just a sign extension of the input (which degenerates to
6010 // a copy).
6011 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6012
6013 // The high part is obtained by SRA'ing all but one of the bits of the lo
6014 // part.
6015 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
6016 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6017 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6018 break;
6019 }
6020 case ISD::ZERO_EXTEND:
6021 // The low part is just a zero extension of the input (which degenerates to
6022 // a copy).
6023 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6024
6025 // The high part is just a zero.
6026 Hi = DAG.getConstant(0, NVT);
6027 break;
6028
6029 case ISD::TRUNCATE: {
6030 // The input value must be larger than this value. Expand *it*.
6031 SDOperand NewLo;
6032 ExpandOp(Node->getOperand(0), NewLo, Hi);
6033
6034 // The low part is now either the right size, or it is closer. If not the
6035 // right size, make an illegal truncate so we recursively expand it.
6036 if (NewLo.getValueType() != Node->getValueType(0))
6037 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6038 ExpandOp(NewLo, Lo, Hi);
6039 break;
6040 }
6041
6042 case ISD::BIT_CONVERT: {
6043 SDOperand Tmp;
6044 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6045 // If the target wants to, allow it to lower this itself.
6046 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6047 case Expand: assert(0 && "cannot expand FP!");
6048 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6049 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6050 }
6051 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6052 }
6053
6054 // f32 / f64 must be expanded to i32 / i64.
6055 if (VT == MVT::f32 || VT == MVT::f64) {
6056 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6057 if (getTypeAction(NVT) == Expand)
6058 ExpandOp(Lo, Lo, Hi);
6059 break;
6060 }
6061
6062 // If source operand will be expanded to the same type as VT, i.e.
6063 // i64 <- f64, i32 <- f32, expand the source operand instead.
6064 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6065 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6066 ExpandOp(Node->getOperand(0), Lo, Hi);
6067 break;
6068 }
6069
6070 // Turn this into a load/store pair by default.
6071 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006072 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006073
6074 ExpandOp(Tmp, Lo, Hi);
6075 break;
6076 }
6077
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006078 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006079 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6080 TargetLowering::Custom &&
6081 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006082 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6083 assert(Tmp.Val && "Node must be custom expanded!");
6084 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006085 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006086 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006087 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006089
6090 // These operators cannot be expanded directly, emit them as calls to
6091 // library functions.
6092 case ISD::FP_TO_SINT: {
6093 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6094 SDOperand Op;
6095 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6096 case Expand: assert(0 && "cannot expand FP!");
6097 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6098 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6099 }
6100
6101 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6102
6103 // Now that the custom expander is done, expand the result, which is still
6104 // VT.
6105 if (Op.Val) {
6106 ExpandOp(Op, Lo, Hi);
6107 break;
6108 }
6109 }
6110
Dale Johannesenac77b272007-10-05 20:04:43 +00006111 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112 if (Node->getOperand(0).getValueType() == MVT::f32)
6113 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006114 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006116 else if (Node->getOperand(0).getValueType() == MVT::f80)
6117 LC = RTLIB::FPTOSINT_F80_I64;
6118 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6119 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006120 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6121 false/*sign irrelevant*/, Hi);
6122 break;
6123 }
6124
6125 case ISD::FP_TO_UINT: {
6126 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6127 SDOperand Op;
6128 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6129 case Expand: assert(0 && "cannot expand FP!");
6130 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6131 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6132 }
6133
6134 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6135
6136 // Now that the custom expander is done, expand the result.
6137 if (Op.Val) {
6138 ExpandOp(Op, Lo, Hi);
6139 break;
6140 }
6141 }
6142
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00006143 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006144 if (Node->getOperand(0).getValueType() == MVT::f32)
6145 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen4e1cf5d2007-09-28 18:44:17 +00006146 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006147 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006148 else if (Node->getOperand(0).getValueType() == MVT::f80)
6149 LC = RTLIB::FPTOUINT_F80_I64;
6150 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6151 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006152 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6153 false/*sign irrelevant*/, Hi);
6154 break;
6155 }
6156
6157 case ISD::SHL: {
6158 // If the target wants custom lowering, do so.
6159 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6160 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6161 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6162 Op = TLI.LowerOperation(Op, DAG);
6163 if (Op.Val) {
6164 // Now that the custom expander is done, expand the result, which is
6165 // still VT.
6166 ExpandOp(Op, Lo, Hi);
6167 break;
6168 }
6169 }
6170
6171 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6172 // this X << 1 as X+X.
6173 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6174 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6175 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6176 SDOperand LoOps[2], HiOps[3];
6177 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6178 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6179 LoOps[1] = LoOps[0];
6180 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6181
6182 HiOps[1] = HiOps[0];
6183 HiOps[2] = Lo.getValue(1);
6184 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6185 break;
6186 }
6187 }
6188
6189 // If we can emit an efficient shift operation, do so now.
6190 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6191 break;
6192
6193 // If this target supports SHL_PARTS, use it.
6194 TargetLowering::LegalizeAction Action =
6195 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6196 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6197 Action == TargetLowering::Custom) {
6198 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6199 break;
6200 }
6201
6202 // Otherwise, emit a libcall.
6203 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6204 false/*left shift=unsigned*/, Hi);
6205 break;
6206 }
6207
6208 case ISD::SRA: {
6209 // If the target wants custom lowering, do so.
6210 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6211 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6212 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6213 Op = TLI.LowerOperation(Op, DAG);
6214 if (Op.Val) {
6215 // Now that the custom expander is done, expand the result, which is
6216 // still VT.
6217 ExpandOp(Op, Lo, Hi);
6218 break;
6219 }
6220 }
6221
6222 // If we can emit an efficient shift operation, do so now.
6223 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6224 break;
6225
6226 // If this target supports SRA_PARTS, use it.
6227 TargetLowering::LegalizeAction Action =
6228 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6229 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6230 Action == TargetLowering::Custom) {
6231 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6232 break;
6233 }
6234
6235 // Otherwise, emit a libcall.
6236 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6237 true/*ashr is signed*/, Hi);
6238 break;
6239 }
6240
6241 case ISD::SRL: {
6242 // If the target wants custom lowering, do so.
6243 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6244 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6245 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6246 Op = TLI.LowerOperation(Op, DAG);
6247 if (Op.Val) {
6248 // Now that the custom expander is done, expand the result, which is
6249 // still VT.
6250 ExpandOp(Op, Lo, Hi);
6251 break;
6252 }
6253 }
6254
6255 // If we can emit an efficient shift operation, do so now.
6256 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6257 break;
6258
6259 // If this target supports SRL_PARTS, use it.
6260 TargetLowering::LegalizeAction Action =
6261 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6262 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6263 Action == TargetLowering::Custom) {
6264 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6265 break;
6266 }
6267
6268 // Otherwise, emit a libcall.
6269 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6270 false/*lshr is unsigned*/, Hi);
6271 break;
6272 }
6273
6274 case ISD::ADD:
6275 case ISD::SUB: {
6276 // If the target wants to custom expand this, let them.
6277 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6278 TargetLowering::Custom) {
6279 Op = TLI.LowerOperation(Op, DAG);
6280 if (Op.Val) {
6281 ExpandOp(Op, Lo, Hi);
6282 break;
6283 }
6284 }
6285
6286 // Expand the subcomponents.
6287 SDOperand LHSL, LHSH, RHSL, RHSH;
6288 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6289 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6290 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6291 SDOperand LoOps[2], HiOps[3];
6292 LoOps[0] = LHSL;
6293 LoOps[1] = RHSL;
6294 HiOps[0] = LHSH;
6295 HiOps[1] = RHSH;
6296 if (Node->getOpcode() == ISD::ADD) {
6297 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6298 HiOps[2] = Lo.getValue(1);
6299 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6300 } else {
6301 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6302 HiOps[2] = Lo.getValue(1);
6303 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6304 }
6305 break;
6306 }
6307
6308 case ISD::ADDC:
6309 case ISD::SUBC: {
6310 // Expand the subcomponents.
6311 SDOperand LHSL, LHSH, RHSL, RHSH;
6312 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6313 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6314 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6315 SDOperand LoOps[2] = { LHSL, RHSL };
6316 SDOperand HiOps[3] = { LHSH, RHSH };
6317
6318 if (Node->getOpcode() == ISD::ADDC) {
6319 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6320 HiOps[2] = Lo.getValue(1);
6321 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6322 } else {
6323 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6324 HiOps[2] = Lo.getValue(1);
6325 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6326 }
6327 // Remember that we legalized the flag.
6328 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6329 break;
6330 }
6331 case ISD::ADDE:
6332 case ISD::SUBE: {
6333 // Expand the subcomponents.
6334 SDOperand LHSL, LHSH, RHSL, RHSH;
6335 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6336 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6337 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6338 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6339 SDOperand HiOps[3] = { LHSH, RHSH };
6340
6341 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6342 HiOps[2] = Lo.getValue(1);
6343 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6344
6345 // Remember that we legalized the flag.
6346 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6347 break;
6348 }
6349 case ISD::MUL: {
6350 // If the target wants to custom expand this, let them.
6351 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6352 SDOperand New = TLI.LowerOperation(Op, DAG);
6353 if (New.Val) {
6354 ExpandOp(New, Lo, Hi);
6355 break;
6356 }
6357 }
6358
6359 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6360 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006361 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6362 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6363 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006364 SDOperand LL, LH, RL, RH;
6365 ExpandOp(Node->getOperand(0), LL, LH);
6366 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006367 unsigned OuterBitSize = Op.getValueSizeInBits();
6368 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006369 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6370 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman07961cd2008-02-25 21:11:39 +00006371 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6372 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6373 DAG.MaskedValueIsZero(Op.getOperand(1),
6374 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohman5a199552007-10-08 18:33:35 +00006375 // The inputs are both zero-extended.
6376 if (HasUMUL_LOHI) {
6377 // We can emit a umul_lohi.
6378 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6379 Hi = SDOperand(Lo.Val, 1);
6380 break;
6381 }
6382 if (HasMULHU) {
6383 // We can emit a mulhu+mul.
6384 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6385 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6386 break;
6387 }
Dan Gohman5a199552007-10-08 18:33:35 +00006388 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006389 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006390 // The input values are both sign-extended.
6391 if (HasSMUL_LOHI) {
6392 // We can emit a smul_lohi.
6393 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6394 Hi = SDOperand(Lo.Val, 1);
6395 break;
6396 }
6397 if (HasMULHS) {
6398 // We can emit a mulhs+mul.
6399 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6400 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6401 break;
6402 }
6403 }
6404 if (HasUMUL_LOHI) {
6405 // Lo,Hi = umul LHS, RHS.
6406 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6407 DAG.getVTList(NVT, NVT), LL, RL);
6408 Lo = UMulLOHI;
6409 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006410 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6411 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6412 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6413 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6414 break;
6415 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006416 if (HasMULHU) {
6417 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6418 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6419 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6420 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6421 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6422 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6423 break;
6424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006425 }
6426
Dan Gohman5a199552007-10-08 18:33:35 +00006427 // If nothing else, we can make a libcall.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6429 false/*sign irrelevant*/, Hi);
6430 break;
6431 }
6432 case ISD::SDIV:
6433 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6434 break;
6435 case ISD::UDIV:
6436 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6437 break;
6438 case ISD::SREM:
6439 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6440 break;
6441 case ISD::UREM:
6442 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6443 break;
6444
6445 case ISD::FADD:
Duncan Sands37a3f472008-01-10 10:28:30 +00006446 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6447 RTLIB::ADD_F64,
6448 RTLIB::ADD_F80,
6449 RTLIB::ADD_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 Node, false, Hi);
6451 break;
6452 case ISD::FSUB:
Duncan Sands37a3f472008-01-10 10:28:30 +00006453 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6454 RTLIB::SUB_F64,
6455 RTLIB::SUB_F80,
6456 RTLIB::SUB_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006457 Node, false, Hi);
6458 break;
6459 case ISD::FMUL:
Duncan Sands37a3f472008-01-10 10:28:30 +00006460 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6461 RTLIB::MUL_F64,
6462 RTLIB::MUL_F80,
6463 RTLIB::MUL_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006464 Node, false, Hi);
6465 break;
6466 case ISD::FDIV:
Duncan Sands37a3f472008-01-10 10:28:30 +00006467 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6468 RTLIB::DIV_F64,
6469 RTLIB::DIV_F80,
6470 RTLIB::DIV_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 Node, false, Hi);
6472 break;
6473 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006474 if (VT == MVT::ppcf128) {
6475 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6476 Node->getOperand(0).getValueType()==MVT::f64);
6477 const uint64_t zero = 0;
6478 if (Node->getOperand(0).getValueType()==MVT::f32)
6479 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6480 else
6481 Hi = Node->getOperand(0);
6482 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6483 break;
6484 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006485 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
6486 break;
6487 case ISD::FP_ROUND:
6488 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
6489 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006490 case ISD::FPOWI:
Duncan Sands37a3f472008-01-10 10:28:30 +00006491 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6492 RTLIB::POWI_F64,
6493 RTLIB::POWI_F80,
6494 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006495 Node, false, Hi);
6496 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497 case ISD::FSQRT:
6498 case ISD::FSIN:
6499 case ISD::FCOS: {
6500 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6501 switch(Node->getOpcode()) {
6502 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006503 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6504 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 break;
6506 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006507 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6508 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006509 break;
6510 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006511 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6512 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006513 break;
6514 default: assert(0 && "Unreachable!");
6515 }
6516 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
6517 break;
6518 }
6519 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006520 if (VT == MVT::ppcf128) {
6521 SDOperand Tmp;
6522 ExpandOp(Node->getOperand(0), Lo, Tmp);
6523 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6524 // lo = hi==fabs(hi) ? lo : -lo;
6525 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6526 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6527 DAG.getCondCode(ISD::SETEQ));
6528 break;
6529 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006530 SDOperand Mask = (VT == MVT::f64)
6531 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6532 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6533 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6534 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6535 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6536 if (getTypeAction(NVT) == Expand)
6537 ExpandOp(Lo, Lo, Hi);
6538 break;
6539 }
6540 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006541 if (VT == MVT::ppcf128) {
6542 ExpandOp(Node->getOperand(0), Lo, Hi);
6543 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6544 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6545 break;
6546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006547 SDOperand Mask = (VT == MVT::f64)
6548 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6549 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6550 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6551 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6552 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6553 if (getTypeAction(NVT) == Expand)
6554 ExpandOp(Lo, Lo, Hi);
6555 break;
6556 }
6557 case ISD::FCOPYSIGN: {
6558 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6559 if (getTypeAction(NVT) == Expand)
6560 ExpandOp(Lo, Lo, Hi);
6561 break;
6562 }
6563 case ISD::SINT_TO_FP:
6564 case ISD::UINT_TO_FP: {
6565 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6566 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006567 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohman84d00962008-02-25 21:39:34 +00006568 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00006569 if (isSigned) {
6570 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6571 Node->getOperand(0)));
6572 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6573 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00006574 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00006575 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6576 Node->getOperand(0)));
6577 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6578 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006579 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006580 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6581 DAG.getConstant(0, MVT::i32),
6582 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6583 DAG.getConstantFP(
6584 APFloat(APInt(128, 2, TwoE32)),
6585 MVT::ppcf128)),
6586 Hi,
6587 DAG.getCondCode(ISD::SETLT)),
6588 Lo, Hi);
6589 }
6590 break;
6591 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006592 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6593 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00006594 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006595 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6596 Lo, Hi);
6597 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6598 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6599 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6600 DAG.getConstant(0, MVT::i64),
6601 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6602 DAG.getConstantFP(
6603 APFloat(APInt(128, 2, TwoE64)),
6604 MVT::ppcf128)),
6605 Hi,
6606 DAG.getCondCode(ISD::SETLT)),
6607 Lo, Hi);
6608 break;
6609 }
Evan Cheng20186812007-09-27 07:35:39 +00006610 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006611 if (Node->getOperand(0).getValueType() == MVT::i64) {
6612 if (VT == MVT::f32)
6613 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006614 else if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006615 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006616 else if (VT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00006617 assert(isSigned);
Dale Johannesenac77b272007-10-05 20:04:43 +00006618 LC = RTLIB::SINTTOFP_I64_F80;
6619 }
6620 else if (VT == MVT::ppcf128) {
6621 assert(isSigned);
6622 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006623 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006624 } else {
6625 if (VT == MVT::f32)
6626 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
6627 else
6628 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
6629 }
6630
6631 // Promote the operand if needed.
6632 if (getTypeAction(SrcVT) == Promote) {
6633 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6634 Tmp = isSigned
6635 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6636 DAG.getValueType(SrcVT))
6637 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6638 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6639 }
6640
6641 const char *LibCall = TLI.getLibcallName(LC);
6642 if (LibCall)
6643 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6644 else {
6645 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6646 Node->getOperand(0));
6647 if (getTypeAction(Lo.getValueType()) == Expand)
6648 ExpandOp(Lo, Lo, Hi);
6649 }
6650 break;
6651 }
6652 }
6653
6654 // Make sure the resultant values have been legalized themselves, unless this
6655 // is a type that requires multi-step expansion.
6656 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6657 Lo = LegalizeOp(Lo);
6658 if (Hi.Val)
6659 // Don't legalize the high part if it is expanded to a single node.
6660 Hi = LegalizeOp(Hi);
6661 }
6662
6663 // Remember in a map if the values will be reused later.
6664 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6665 assert(isNew && "Value already expanded?!?");
6666}
6667
6668/// SplitVectorOp - Given an operand of vector type, break it down into
6669/// two smaller values, still of vector type.
6670void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6671 SDOperand &Hi) {
6672 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6673 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006674 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006675 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006676
Dan Gohmana0763d92007-09-24 15:54:53 +00006677 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006678
6679 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6680 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6681
6682 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6683 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6684
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006685 // See if we already split it.
6686 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6687 = SplitNodes.find(Op);
6688 if (I != SplitNodes.end()) {
6689 Lo = I->second.first;
6690 Hi = I->second.second;
6691 return;
6692 }
6693
6694 switch (Node->getOpcode()) {
6695 default:
6696#ifndef NDEBUG
6697 Node->dump(&DAG);
6698#endif
6699 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006700 case ISD::UNDEF:
6701 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6702 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6703 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006704 case ISD::BUILD_PAIR:
6705 Lo = Node->getOperand(0);
6706 Hi = Node->getOperand(1);
6707 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006708 case ISD::INSERT_VECTOR_ELT: {
6709 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6710 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6711 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006712 if (Index < NewNumElts_Lo)
6713 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006714 DAG.getConstant(Index, TLI.getPointerTy()));
6715 else
Nate Begeman4a365ad2007-11-15 21:15:26 +00006716 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6717 DAG.getConstant(Index - NewNumElts_Lo,
6718 TLI.getPointerTy()));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006719 break;
6720 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006721 case ISD::VECTOR_SHUFFLE: {
6722 // Build the low part.
6723 SDOperand Mask = Node->getOperand(2);
6724 SmallVector<SDOperand, 8> Ops;
6725 MVT::ValueType PtrVT = TLI.getPointerTy();
6726
6727 // Insert all of the elements from the input that are needed. We use
6728 // buildvector of extractelement here because the input vectors will have
6729 // to be legalized, so this makes the code simpler.
6730 for (unsigned i = 0; i != NewNumElts_Lo; ++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 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6741 Ops.clear();
6742
6743 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6744 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6745 SDOperand InVec = Node->getOperand(0);
6746 if (Idx >= NumElements) {
6747 InVec = Node->getOperand(1);
6748 Idx -= NumElements;
6749 }
6750 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6751 DAG.getConstant(Idx, PtrVT)));
6752 }
6753 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6754 break;
6755 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006756 case ISD::BUILD_VECTOR: {
6757 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006758 Node->op_begin()+NewNumElts_Lo);
6759 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760
Nate Begeman4a365ad2007-11-15 21:15:26 +00006761 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006763 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006764 break;
6765 }
6766 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006767 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6769 if (NewNumSubvectors == 1) {
6770 Lo = Node->getOperand(0);
6771 Hi = Node->getOperand(1);
6772 } else {
6773 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6774 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006775 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006776
6777 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6778 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006779 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006780 }
6781 break;
6782 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006783 case ISD::SELECT: {
6784 SDOperand Cond = Node->getOperand(0);
6785
6786 SDOperand LL, LH, RL, RH;
6787 SplitVectorOp(Node->getOperand(1), LL, LH);
6788 SplitVectorOp(Node->getOperand(2), RL, RH);
6789
6790 if (MVT::isVector(Cond.getValueType())) {
6791 // Handle a vector merge.
6792 SDOperand CL, CH;
6793 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006794 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6795 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006796 } else {
6797 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006798 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6799 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006800 }
6801 break;
6802 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006803 case ISD::ADD:
6804 case ISD::SUB:
6805 case ISD::MUL:
6806 case ISD::FADD:
6807 case ISD::FSUB:
6808 case ISD::FMUL:
6809 case ISD::SDIV:
6810 case ISD::UDIV:
6811 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006812 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006813 case ISD::AND:
6814 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006815 case ISD::XOR:
6816 case ISD::UREM:
6817 case ISD::SREM:
6818 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006819 SDOperand LL, LH, RL, RH;
6820 SplitVectorOp(Node->getOperand(0), LL, LH);
6821 SplitVectorOp(Node->getOperand(1), RL, RH);
6822
Nate Begeman4a365ad2007-11-15 21:15:26 +00006823 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6824 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006825 break;
6826 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006827 case ISD::FPOWI: {
6828 SDOperand L, H;
6829 SplitVectorOp(Node->getOperand(0), L, H);
6830
Nate Begeman4a365ad2007-11-15 21:15:26 +00006831 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6832 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006833 break;
6834 }
6835 case ISD::CTTZ:
6836 case ISD::CTLZ:
6837 case ISD::CTPOP:
6838 case ISD::FNEG:
6839 case ISD::FABS:
6840 case ISD::FSQRT:
6841 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006842 case ISD::FCOS:
6843 case ISD::FP_TO_SINT:
6844 case ISD::FP_TO_UINT:
6845 case ISD::SINT_TO_FP:
6846 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006847 SDOperand L, H;
6848 SplitVectorOp(Node->getOperand(0), L, H);
6849
Nate Begeman4a365ad2007-11-15 21:15:26 +00006850 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6851 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006852 break;
6853 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006854 case ISD::LOAD: {
6855 LoadSDNode *LD = cast<LoadSDNode>(Node);
6856 SDOperand Ch = LD->getChain();
6857 SDOperand Ptr = LD->getBasePtr();
6858 const Value *SV = LD->getSrcValue();
6859 int SVOffset = LD->getSrcValueOffset();
6860 unsigned Alignment = LD->getAlignment();
6861 bool isVolatile = LD->isVolatile();
6862
Nate Begeman4a365ad2007-11-15 21:15:26 +00006863 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6864 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006865 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006866 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006867 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006868 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006869 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006870
6871 // Build a factor node to remember that this load is independent of the
6872 // other one.
6873 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6874 Hi.getValue(1));
6875
6876 // Remember that we legalized the chain.
6877 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6878 break;
6879 }
6880 case ISD::BIT_CONVERT: {
6881 // We know the result is a vector. The input may be either a vector or a
6882 // scalar value.
6883 SDOperand InOp = Node->getOperand(0);
6884 if (!MVT::isVector(InOp.getValueType()) ||
6885 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6886 // The input is a scalar or single-element vector.
6887 // Lower to a store/load so that it can be split.
6888 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006889 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman20e37962008-02-11 18:58:42 +00006890 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006891
6892 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006893 InOp, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006894 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006895 FI->getIndex());
6896 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006897 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006898 FI->getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006899 }
6900 // Split the vector and convert each of the pieces now.
6901 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006902 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6903 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006904 break;
6905 }
6906 }
6907
6908 // Remember in a map if the values will be reused later.
6909 bool isNew =
6910 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6911 assert(isNew && "Value already split?!?");
6912}
6913
6914
6915/// ScalarizeVectorOp - Given an operand of single-element vector type
6916/// (e.g. v1f32), convert it into the equivalent operation that returns a
6917/// scalar (e.g. f32) value.
6918SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6919 assert(MVT::isVector(Op.getValueType()) &&
6920 "Bad ScalarizeVectorOp invocation!");
6921 SDNode *Node = Op.Val;
6922 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6923 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6924
6925 // See if we already scalarized it.
6926 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6927 if (I != ScalarizedNodes.end()) return I->second;
6928
6929 SDOperand Result;
6930 switch (Node->getOpcode()) {
6931 default:
6932#ifndef NDEBUG
6933 Node->dump(&DAG); cerr << "\n";
6934#endif
6935 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6936 case ISD::ADD:
6937 case ISD::FADD:
6938 case ISD::SUB:
6939 case ISD::FSUB:
6940 case ISD::MUL:
6941 case ISD::FMUL:
6942 case ISD::SDIV:
6943 case ISD::UDIV:
6944 case ISD::FDIV:
6945 case ISD::SREM:
6946 case ISD::UREM:
6947 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006948 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006949 case ISD::AND:
6950 case ISD::OR:
6951 case ISD::XOR:
6952 Result = DAG.getNode(Node->getOpcode(),
6953 NewVT,
6954 ScalarizeVectorOp(Node->getOperand(0)),
6955 ScalarizeVectorOp(Node->getOperand(1)));
6956 break;
6957 case ISD::FNEG:
6958 case ISD::FABS:
6959 case ISD::FSQRT:
6960 case ISD::FSIN:
6961 case ISD::FCOS:
6962 Result = DAG.getNode(Node->getOpcode(),
6963 NewVT,
6964 ScalarizeVectorOp(Node->getOperand(0)));
6965 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006966 case ISD::FPOWI:
6967 Result = DAG.getNode(Node->getOpcode(),
6968 NewVT,
6969 ScalarizeVectorOp(Node->getOperand(0)),
6970 Node->getOperand(1));
6971 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006972 case ISD::LOAD: {
6973 LoadSDNode *LD = cast<LoadSDNode>(Node);
6974 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6975 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6976
6977 const Value *SV = LD->getSrcValue();
6978 int SVOffset = LD->getSrcValueOffset();
6979 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6980 LD->isVolatile(), LD->getAlignment());
6981
6982 // Remember that we legalized the chain.
6983 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6984 break;
6985 }
6986 case ISD::BUILD_VECTOR:
6987 Result = Node->getOperand(0);
6988 break;
6989 case ISD::INSERT_VECTOR_ELT:
6990 // Returning the inserted scalar element.
6991 Result = Node->getOperand(1);
6992 break;
6993 case ISD::CONCAT_VECTORS:
6994 assert(Node->getOperand(0).getValueType() == NewVT &&
6995 "Concat of non-legal vectors not yet supported!");
6996 Result = Node->getOperand(0);
6997 break;
6998 case ISD::VECTOR_SHUFFLE: {
6999 // Figure out if the scalar is the LHS or RHS and return it.
7000 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7001 if (cast<ConstantSDNode>(EltNum)->getValue())
7002 Result = ScalarizeVectorOp(Node->getOperand(1));
7003 else
7004 Result = ScalarizeVectorOp(Node->getOperand(0));
7005 break;
7006 }
7007 case ISD::EXTRACT_SUBVECTOR:
7008 Result = Node->getOperand(0);
7009 assert(Result.getValueType() == NewVT);
7010 break;
7011 case ISD::BIT_CONVERT:
7012 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
7013 break;
7014 case ISD::SELECT:
7015 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7016 ScalarizeVectorOp(Op.getOperand(1)),
7017 ScalarizeVectorOp(Op.getOperand(2)));
7018 break;
7019 }
7020
7021 if (TLI.isTypeLegal(NewVT))
7022 Result = LegalizeOp(Result);
7023 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7024 assert(isNew && "Value already scalarized?");
7025 return Result;
7026}
7027
7028
7029// SelectionDAG::Legalize - This is the entry point for the file.
7030//
7031void SelectionDAG::Legalize() {
7032 if (ViewLegalizeDAGs) viewGraph();
7033
7034 /// run - This is the main entry point to this class.
7035 ///
7036 SelectionDAGLegalize(*this).LegalizeDAG();
7037}
7038