blob: 82324cd5b992007d38765efbd3683cd952665485 [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
Chris Lattnere718cc52008-03-05 06:46:58 +0000489 // double. This shrinks FP constants and canonicalizes them for targets where
490 // an FP extending load is the same cost as a normal load (such as on the x87
491 // fp stack or PPC FP unit).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 MVT::ValueType VT = CFP->getValueType(0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +0000493 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen2fc20782007-09-14 22:26:36 +0000494 CFP->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
Evan Cheng354be062008-03-04 08:05:30 +0000499 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 }
501
Evan Cheng354be062008-03-04 08:05:30 +0000502 MVT::ValueType OrigVT = VT;
503 MVT::ValueType SVT = VT;
504 while (SVT != MVT::f32) {
505 SVT = (unsigned)SVT - 1;
506 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
507 // Only do this if the target has a native EXTLOAD instruction from
508 // smaller type.
Evan Cheng35190fd2008-03-05 01:30:59 +0000509 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000510 TLI.ShouldShrinkFPConstant(OrigVT)) {
Evan Cheng354be062008-03-04 08:05:30 +0000511 const Type *SType = MVT::getTypeForValueType(SVT);
512 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
513 VT = SVT;
514 Extend = true;
515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 }
517
518 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng354be062008-03-04 08:05:30 +0000519 if (Extend)
520 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000521 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Cheng354be062008-03-04 08:05:30 +0000522 0, VT);
523 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
524 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525}
526
527
528/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
529/// operations.
530static
531SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
532 SelectionDAG &DAG, TargetLowering &TLI) {
533 MVT::ValueType VT = Node->getValueType(0);
534 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
535 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
536 "fcopysign expansion only supported for f32 and f64");
537 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
538
539 // First get the sign bit of second operand.
540 SDOperand Mask1 = (SrcVT == MVT::f64)
541 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
542 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
543 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
544 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
545 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
546 // Shift right or sign-extend it if the two operands have different types.
547 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
548 if (SizeDiff > 0) {
549 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
550 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
551 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
552 } else if (SizeDiff < 0)
553 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
554
555 // Clear the sign bit of first operand.
556 SDOperand Mask2 = (VT == MVT::f64)
557 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
558 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
559 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
560 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
561 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
562
563 // Or the value with the sign bit.
564 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
565 return Result;
566}
567
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000568/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
569static
570SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
571 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000572 SDOperand Chain = ST->getChain();
573 SDOperand Ptr = ST->getBasePtr();
574 SDOperand Val = ST->getValue();
575 MVT::ValueType VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000576 int Alignment = ST->getAlignment();
577 int SVOffset = ST->getSrcValueOffset();
Dale Johannesendc0ee192008-02-27 22:36:00 +0000578 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
579 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen08275382007-09-08 19:29:23 +0000580 // Expand to a bitconvert of the value to the integer type of the
581 // same size, then a (misaligned) int store.
582 MVT::ValueType intVT;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000583 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000584 intVT = MVT::i128;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000585 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
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 Johannesenf8c1e852008-03-01 03:40:57 +0000637 if (MVT::is128BitVector(LoadedVT) ||
638 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000639 intVT = MVT::i128;
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000640 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000641 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000642 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000643 intVT = MVT::i32;
644 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000645 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000646
647 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
648 SVOffset, LD->isVolatile(),
649 LD->getAlignment());
650 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesendc0ee192008-02-27 22:36:00 +0000651 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000652 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
653
654 SDOperand Ops[] = { Result, Chain };
655 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
656 Ops, 2);
657 }
Dale Johannesendc0ee192008-02-27 22:36:00 +0000658 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000659 "Unaligned load of unsupported type.");
660
Dale Johannesendc0ee192008-02-27 22:36:00 +0000661 // Compute the new VT that is half the size of the old one. This is an
662 // integer MVT.
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000663 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
664 MVT::ValueType NewLoadedVT;
Dale Johannesendc0ee192008-02-27 22:36:00 +0000665 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000666 NumBits >>= 1;
667
668 unsigned Alignment = LD->getAlignment();
669 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000670 ISD::LoadExtType HiExtType = LD->getExtensionType();
671
672 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
673 if (HiExtType == ISD::NON_EXTLOAD)
674 HiExtType = ISD::ZEXTLOAD;
675
676 // Load the value in two parts
677 SDOperand Lo, Hi;
678 if (TLI.isLittleEndian()) {
679 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
680 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
684 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000685 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000686 } else {
687 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
688 NewLoadedVT,LD->isVolatile(), Alignment);
689 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
690 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
691 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
692 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000693 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000694 }
695
696 // aggregate the two parts
697 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
698 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
699 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
700
701 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
702 Hi.getValue(1));
703
704 SDOperand Ops[] = { Result, TF };
705 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
706}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707
Dan Gohman6d05cac2007-10-11 23:57:53 +0000708/// UnrollVectorOp - We know that the given vector has a legal type, however
709/// the operation it performs is not legal and is an operation that we have
710/// no way of lowering. "Unroll" the vector, splitting out the scalars and
711/// operating on each element individually.
712SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
713 MVT::ValueType VT = Op.getValueType();
714 assert(isTypeLegal(VT) &&
715 "Caller should expand or promote operands that are not legal!");
716 assert(Op.Val->getNumValues() == 1 &&
717 "Can't unroll a vector with multiple results!");
718 unsigned NE = MVT::getVectorNumElements(VT);
719 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
720
721 SmallVector<SDOperand, 8> Scalars;
722 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
723 for (unsigned i = 0; i != NE; ++i) {
724 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
725 SDOperand Operand = Op.getOperand(j);
726 MVT::ValueType OperandVT = Operand.getValueType();
727 if (MVT::isVector(OperandVT)) {
728 // A vector operand; extract a single element.
729 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
730 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
731 OperandEltVT,
732 Operand,
733 DAG.getConstant(i, MVT::i32));
734 } else {
735 // A scalar operand; just use it as is.
736 Operands[j] = Operand;
737 }
738 }
739 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
740 &Operands[0], Operands.size()));
741 }
742
743 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
744}
745
Duncan Sands37a3f472008-01-10 10:28:30 +0000746/// GetFPLibCall - Return the right libcall for the given floating point type.
747static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
748 RTLIB::Libcall Call_F32,
749 RTLIB::Libcall Call_F64,
750 RTLIB::Libcall Call_F80,
751 RTLIB::Libcall Call_PPCF128) {
752 return
753 VT == MVT::f32 ? Call_F32 :
754 VT == MVT::f64 ? Call_F64 :
755 VT == MVT::f80 ? Call_F80 :
756 VT == MVT::ppcf128 ? Call_PPCF128 :
757 RTLIB::UNKNOWN_LIBCALL;
758}
759
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760/// LegalizeOp - We know that the specified value has a legal type, and
761/// that its operands are legal. Now ensure that the operation itself
762/// is legal, recursively ensuring that the operands' operations remain
763/// legal.
764SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
766 return Op;
767
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 assert(isTypeLegal(Op.getValueType()) &&
769 "Caller should expand or promote operands that are not legal!");
770 SDNode *Node = Op.Val;
771
772 // If this operation defines any values that cannot be represented in a
773 // register on this target, make sure to expand or promote them.
774 if (Node->getNumValues() > 1) {
775 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
776 if (getTypeAction(Node->getValueType(i)) != Legal) {
777 HandleOp(Op.getValue(i));
778 assert(LegalizedNodes.count(Op) &&
779 "Handling didn't add legal operands!");
780 return LegalizedNodes[Op];
781 }
782 }
783
784 // Note that LegalizeOp may be reentered even from single-use nodes, which
785 // means that we always must cache transformed nodes.
786 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
787 if (I != LegalizedNodes.end()) return I->second;
788
789 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
790 SDOperand Result = Op;
791 bool isCustom = false;
792
793 switch (Node->getOpcode()) {
794 case ISD::FrameIndex:
795 case ISD::EntryToken:
796 case ISD::Register:
797 case ISD::BasicBlock:
798 case ISD::TargetFrameIndex:
799 case ISD::TargetJumpTable:
800 case ISD::TargetConstant:
801 case ISD::TargetConstantFP:
802 case ISD::TargetConstantPool:
803 case ISD::TargetGlobalAddress:
804 case ISD::TargetGlobalTLSAddress:
805 case ISD::TargetExternalSymbol:
806 case ISD::VALUETYPE:
807 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000808 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 case ISD::STRING:
810 case ISD::CONDCODE:
811 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000812 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 "This must be legal!");
814 break;
815 default:
816 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
817 // If this is a target node, legalize it by legalizing the operands then
818 // passing it through.
819 SmallVector<SDOperand, 8> Ops;
820 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
821 Ops.push_back(LegalizeOp(Node->getOperand(i)));
822
823 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
824
825 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
826 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
827 return Result.getValue(Op.ResNo);
828 }
829 // Otherwise this is an unhandled builtin node. splat.
830#ifndef NDEBUG
831 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
832#endif
833 assert(0 && "Do not know how to legalize this operator!");
834 abort();
835 case ISD::GLOBAL_OFFSET_TABLE:
836 case ISD::GlobalAddress:
837 case ISD::GlobalTLSAddress:
838 case ISD::ExternalSymbol:
839 case ISD::ConstantPool:
840 case ISD::JumpTable: // Nothing to do.
841 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
842 default: assert(0 && "This action is not supported yet!");
843 case TargetLowering::Custom:
844 Tmp1 = TLI.LowerOperation(Op, DAG);
845 if (Tmp1.Val) Result = Tmp1;
846 // FALLTHROUGH if the target doesn't want to lower this op after all.
847 case TargetLowering::Legal:
848 break;
849 }
850 break;
851 case ISD::FRAMEADDR:
852 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 // The only option for these nodes is to custom lower them. If the target
854 // does not custom lower them, then return zero.
855 Tmp1 = TLI.LowerOperation(Op, DAG);
856 if (Tmp1.Val)
857 Result = Tmp1;
858 else
859 Result = DAG.getConstant(0, TLI.getPointerTy());
860 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000861 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000862 MVT::ValueType VT = Node->getValueType(0);
863 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
864 default: assert(0 && "This action is not supported yet!");
865 case TargetLowering::Custom:
866 Result = TLI.LowerOperation(Op, DAG);
867 if (Result.Val) break;
868 // Fall Thru
869 case TargetLowering::Legal:
870 Result = DAG.getConstant(0, VT);
871 break;
872 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000873 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000874 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 case ISD::EXCEPTIONADDR: {
876 Tmp1 = LegalizeOp(Node->getOperand(0));
877 MVT::ValueType VT = Node->getValueType(0);
878 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
879 default: assert(0 && "This action is not supported yet!");
880 case TargetLowering::Expand: {
881 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000882 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 }
884 break;
885 case TargetLowering::Custom:
886 Result = TLI.LowerOperation(Op, DAG);
887 if (Result.Val) break;
888 // Fall Thru
889 case TargetLowering::Legal: {
890 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
891 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000892 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 break;
894 }
895 }
896 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000897 if (Result.Val->getNumValues() == 1) break;
898
899 assert(Result.Val->getNumValues() == 2 &&
900 "Cannot return more than two values!");
901
902 // Since we produced two values, make sure to remember that we
903 // legalized both of them.
904 Tmp1 = LegalizeOp(Result);
905 Tmp2 = LegalizeOp(Result.getValue(1));
906 AddLegalizedOperand(Op.getValue(0), Tmp1);
907 AddLegalizedOperand(Op.getValue(1), Tmp2);
908 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 case ISD::EHSELECTION: {
910 Tmp1 = LegalizeOp(Node->getOperand(0));
911 Tmp2 = LegalizeOp(Node->getOperand(1));
912 MVT::ValueType VT = Node->getValueType(0);
913 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Expand: {
916 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000917 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 }
919 break;
920 case TargetLowering::Custom:
921 Result = TLI.LowerOperation(Op, DAG);
922 if (Result.Val) break;
923 // Fall Thru
924 case TargetLowering::Legal: {
925 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
926 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000927 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 break;
929 }
930 }
931 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000932 if (Result.Val->getNumValues() == 1) break;
933
934 assert(Result.Val->getNumValues() == 2 &&
935 "Cannot return more than two values!");
936
937 // Since we produced two values, make sure to remember that we
938 // legalized both of them.
939 Tmp1 = LegalizeOp(Result);
940 Tmp2 = LegalizeOp(Result.getValue(1));
941 AddLegalizedOperand(Op.getValue(0), Tmp1);
942 AddLegalizedOperand(Op.getValue(1), Tmp2);
943 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 case ISD::EH_RETURN: {
945 MVT::ValueType VT = Node->getValueType(0);
946 // The only "good" option for this node is to custom lower it.
947 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
948 default: assert(0 && "This action is not supported at all!");
949 case TargetLowering::Custom:
950 Result = TLI.LowerOperation(Op, DAG);
951 if (Result.Val) break;
952 // Fall Thru
953 case TargetLowering::Legal:
954 // Target does not know, how to lower this, lower to noop
955 Result = LegalizeOp(Node->getOperand(0));
956 break;
957 }
958 }
959 break;
960 case ISD::AssertSext:
961 case ISD::AssertZext:
962 Tmp1 = LegalizeOp(Node->getOperand(0));
963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
964 break;
965 case ISD::MERGE_VALUES:
966 // Legalize eliminates MERGE_VALUES nodes.
967 Result = Node->getOperand(Op.ResNo);
968 break;
969 case ISD::CopyFromReg:
970 Tmp1 = LegalizeOp(Node->getOperand(0));
971 Result = Op.getValue(0);
972 if (Node->getNumValues() == 2) {
973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
974 } else {
975 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
976 if (Node->getNumOperands() == 3) {
977 Tmp2 = LegalizeOp(Node->getOperand(2));
978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
979 } else {
980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
981 }
982 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
983 }
984 // Since CopyFromReg produces two values, make sure to remember that we
985 // legalized both of them.
986 AddLegalizedOperand(Op.getValue(0), Result);
987 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
988 return Result.getValue(Op.ResNo);
989 case ISD::UNDEF: {
990 MVT::ValueType VT = Op.getValueType();
991 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
992 default: assert(0 && "This action is not supported yet!");
993 case TargetLowering::Expand:
994 if (MVT::isInteger(VT))
995 Result = DAG.getConstant(0, VT);
996 else if (MVT::isFloatingPoint(VT))
Dale Johannesen20b76352007-09-26 17:26:49 +0000997 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
998 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 else
1000 assert(0 && "Unknown value type!");
1001 break;
1002 case TargetLowering::Legal:
1003 break;
1004 }
1005 break;
1006 }
1007
1008 case ISD::INTRINSIC_W_CHAIN:
1009 case ISD::INTRINSIC_WO_CHAIN:
1010 case ISD::INTRINSIC_VOID: {
1011 SmallVector<SDOperand, 8> Ops;
1012 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1013 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1014 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1015
1016 // Allow the target to custom lower its intrinsics if it wants to.
1017 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1018 TargetLowering::Custom) {
1019 Tmp3 = TLI.LowerOperation(Result, DAG);
1020 if (Tmp3.Val) Result = Tmp3;
1021 }
1022
1023 if (Result.Val->getNumValues() == 1) break;
1024
1025 // Must have return value and chain result.
1026 assert(Result.Val->getNumValues() == 2 &&
1027 "Cannot return more than two values!");
1028
1029 // Since loads produce two values, make sure to remember that we
1030 // legalized both of them.
1031 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1032 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1033 return Result.getValue(Op.ResNo);
1034 }
1035
1036 case ISD::LOCATION:
1037 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1039
1040 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1041 case TargetLowering::Promote:
1042 default: assert(0 && "This action is not supported yet!");
1043 case TargetLowering::Expand: {
1044 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1045 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
1046 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
1047
1048 if (MMI && (useDEBUG_LOC || useLABEL)) {
1049 const std::string &FName =
1050 cast<StringSDNode>(Node->getOperand(3))->getValue();
1051 const std::string &DirName =
1052 cast<StringSDNode>(Node->getOperand(4))->getValue();
1053 unsigned SrcFile = MMI->RecordSource(DirName, FName);
1054
1055 SmallVector<SDOperand, 8> Ops;
1056 Ops.push_back(Tmp1); // chain
1057 SDOperand LineOp = Node->getOperand(1);
1058 SDOperand ColOp = Node->getOperand(2);
1059
1060 if (useDEBUG_LOC) {
1061 Ops.push_back(LineOp); // line #
1062 Ops.push_back(ColOp); // col #
1063 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
1064 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
1065 } else {
1066 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1067 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng69eda822008-02-01 02:05:57 +00001068 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng13d1c292008-01-31 09:59:15 +00001070 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1071 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 }
1073 } else {
1074 Result = Tmp1; // chain
1075 }
1076 break;
1077 }
1078 case TargetLowering::Legal:
1079 if (Tmp1 != Node->getOperand(0) ||
1080 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
1081 SmallVector<SDOperand, 8> Ops;
1082 Ops.push_back(Tmp1);
1083 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1084 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1085 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1086 } else {
1087 // Otherwise promote them.
1088 Ops.push_back(PromoteOp(Node->getOperand(1)));
1089 Ops.push_back(PromoteOp(Node->getOperand(2)));
1090 }
1091 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1092 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1093 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1094 }
1095 break;
1096 }
1097 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001098
1099 case ISD::DECLARE:
1100 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1101 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1102 default: assert(0 && "This action is not supported yet!");
1103 case TargetLowering::Legal:
1104 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1105 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1106 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1108 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001109 case TargetLowering::Expand:
1110 Result = LegalizeOp(Node->getOperand(0));
1111 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001112 }
1113 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001114
1115 case ISD::DEBUG_LOC:
1116 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1117 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1118 default: assert(0 && "This action is not supported yet!");
1119 case TargetLowering::Legal:
1120 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1121 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1122 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1123 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1125 break;
1126 }
1127 break;
1128
1129 case ISD::LABEL:
Evan Cheng13d1c292008-01-31 09:59:15 +00001130 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1132 default: assert(0 && "This action is not supported yet!");
1133 case TargetLowering::Legal:
1134 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1135 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng13d1c292008-01-31 09:59:15 +00001136 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 break;
1139 case TargetLowering::Expand:
1140 Result = LegalizeOp(Node->getOperand(0));
1141 break;
1142 }
1143 break;
1144
Andrew Lenharth785610d2008-02-16 01:24:58 +00001145 case ISD::MEMBARRIER: {
1146 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001147 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1148 default: assert(0 && "This action is not supported yet!");
1149 case TargetLowering::Legal: {
1150 SDOperand Ops[6];
1151 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001152 for (int x = 1; x < 6; ++x) {
1153 Ops[x] = Node->getOperand(x);
1154 if (!isTypeLegal(Ops[x].getValueType()))
1155 Ops[x] = PromoteOp(Ops[x]);
1156 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001157 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1158 break;
1159 }
1160 case TargetLowering::Expand:
1161 //There is no libgcc call for this op
1162 Result = Node->getOperand(0); // Noop
1163 break;
1164 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001165 break;
1166 }
1167
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001168 case ISD::ATOMIC_LCS:
1169 case ISD::ATOMIC_LAS:
1170 case ISD::ATOMIC_SWAP: {
1171 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1172 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1173 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001174 "Invalid Atomic node!");
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001175 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001176 SDOperand Ops[4];
1177 for (int x = 0; x < num; ++x)
1178 Ops[x] = LegalizeOp(Node->getOperand(x));
1179 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1180
1181 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001182 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001183 case TargetLowering::Custom:
1184 Result = TLI.LowerOperation(Result, DAG);
1185 break;
1186 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001187 break;
1188 }
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1191 return Result.getValue(Op.ResNo);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001192 }
1193
Scott Michelf2e2b702007-08-08 23:23:31 +00001194 case ISD::Constant: {
1195 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1196 unsigned opAction =
1197 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 // We know we don't need to expand constants here, constants only have one
1200 // value and we check that it is fine above.
1201
Scott Michelf2e2b702007-08-08 23:23:31 +00001202 if (opAction == TargetLowering::Custom) {
1203 Tmp1 = TLI.LowerOperation(Result, DAG);
1204 if (Tmp1.Val)
1205 Result = Tmp1;
1206 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001207 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001208 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 case ISD::ConstantFP: {
1210 // Spill FP immediates to the constant pool if the target cannot directly
1211 // codegen them. Targets often have some immediate values that can be
1212 // efficiently generated into an FP register without a load. We explicitly
1213 // leave these constants as ConstantFP nodes for the target to deal with.
1214 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1217 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001218 case TargetLowering::Legal:
1219 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 case TargetLowering::Custom:
1221 Tmp3 = TLI.LowerOperation(Result, DAG);
1222 if (Tmp3.Val) {
1223 Result = Tmp3;
1224 break;
1225 }
1226 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001227 case TargetLowering::Expand: {
1228 // Check to see if this FP immediate is already legal.
1229 bool isLegal = false;
1230 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1231 E = TLI.legal_fpimm_end(); I != E; ++I) {
1232 if (CFP->isExactlyValue(*I)) {
1233 isLegal = true;
1234 break;
1235 }
1236 }
1237 // If this is a legal constant, turn it into a TargetConstantFP node.
1238 if (isLegal)
1239 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1241 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001242 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 break;
1244 }
1245 case ISD::TokenFactor:
1246 if (Node->getNumOperands() == 2) {
1247 Tmp1 = LegalizeOp(Node->getOperand(0));
1248 Tmp2 = LegalizeOp(Node->getOperand(1));
1249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1250 } else if (Node->getNumOperands() == 3) {
1251 Tmp1 = LegalizeOp(Node->getOperand(0));
1252 Tmp2 = LegalizeOp(Node->getOperand(1));
1253 Tmp3 = LegalizeOp(Node->getOperand(2));
1254 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1255 } else {
1256 SmallVector<SDOperand, 8> Ops;
1257 // Legalize the operands.
1258 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1259 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1260 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1261 }
1262 break;
1263
1264 case ISD::FORMAL_ARGUMENTS:
1265 case ISD::CALL:
1266 // The only option for this is to custom lower it.
1267 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1268 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001269
1270 // The number of incoming and outgoing values should match; unless the final
1271 // outgoing value is a flag.
1272 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1273 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1274 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1275 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 "Lowering call/formal_arguments produced unexpected # results!");
1277
1278 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1279 // remember that we legalized all of them, so it doesn't get relegalized.
1280 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001281 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1282 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1284 if (Op.ResNo == i)
1285 Tmp2 = Tmp1;
1286 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1287 }
1288 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001289 case ISD::EXTRACT_SUBREG: {
1290 Tmp1 = LegalizeOp(Node->getOperand(0));
1291 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1292 assert(idx && "Operand must be a constant");
1293 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1294 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1295 }
1296 break;
1297 case ISD::INSERT_SUBREG: {
1298 Tmp1 = LegalizeOp(Node->getOperand(0));
1299 Tmp2 = LegalizeOp(Node->getOperand(1));
1300 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1301 assert(idx && "Operand must be a constant");
1302 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1303 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1304 }
1305 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 case ISD::BUILD_VECTOR:
1307 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1308 default: assert(0 && "This action is not supported yet!");
1309 case TargetLowering::Custom:
1310 Tmp3 = TLI.LowerOperation(Result, DAG);
1311 if (Tmp3.Val) {
1312 Result = Tmp3;
1313 break;
1314 }
1315 // FALLTHROUGH
1316 case TargetLowering::Expand:
1317 Result = ExpandBUILD_VECTOR(Result.Val);
1318 break;
1319 }
1320 break;
1321 case ISD::INSERT_VECTOR_ELT:
1322 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001324
1325 // The type of the value to insert may not be legal, even though the vector
1326 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1327 // here.
1328 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1329 default: assert(0 && "Cannot expand insert element operand");
1330 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1331 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1332 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1334
1335 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1336 Node->getValueType(0))) {
1337 default: assert(0 && "This action is not supported yet!");
1338 case TargetLowering::Legal:
1339 break;
1340 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001341 Tmp4 = TLI.LowerOperation(Result, DAG);
1342 if (Tmp4.Val) {
1343 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 break;
1345 }
1346 // FALLTHROUGH
1347 case TargetLowering::Expand: {
1348 // If the insert index is a constant, codegen this as a scalar_to_vector,
1349 // then a shuffle that inserts it into the right position in the vector.
1350 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001351 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1352 // match the element type of the vector being created.
1353 if (Tmp2.getValueType() ==
1354 MVT::getVectorElementType(Op.getValueType())) {
1355 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1356 Tmp1.getValueType(), Tmp2);
1357
1358 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1359 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1360 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1361
1362 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1363 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1364 // elt 0 of the RHS.
1365 SmallVector<SDOperand, 8> ShufOps;
1366 for (unsigned i = 0; i != NumElts; ++i) {
1367 if (i != InsertPos->getValue())
1368 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1369 else
1370 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1371 }
1372 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1373 &ShufOps[0], ShufOps.size());
1374
1375 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1376 Tmp1, ScVec, ShufMask);
1377 Result = LegalizeOp(Result);
1378 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 }
1381
1382 // If the target doesn't support this, we have to spill the input vector
1383 // to a temporary stack slot, update the element, then reload it. This is
1384 // badness. We could also load the value into a vector register (either
1385 // with a "move to register" or "extload into register" instruction, then
1386 // permute it into place, if the idx is a constant and if the idx is
1387 // supported by the target.
1388 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001389 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 MVT::ValueType IdxVT = Tmp3.getValueType();
1391 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001392 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman12a9c082008-02-06 22:27:42 +00001393
Dan Gohman20e37962008-02-11 18:58:42 +00001394 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman12a9c082008-02-06 22:27:42 +00001395 int SPFI = StackPtrFI->getIndex();
1396
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 // Store the vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001398 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001399 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001400 SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401
1402 // Truncate or zero extend offset to target pointer type.
1403 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1404 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1405 // Add the offset to the index.
1406 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1407 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1408 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1409 // Store the scalar value.
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001410 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1411 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 // Load the updated vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001413 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001414 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 break;
1416 }
1417 }
1418 break;
1419 case ISD::SCALAR_TO_VECTOR:
1420 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1421 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1422 break;
1423 }
1424
1425 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1426 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1427 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1428 Node->getValueType(0))) {
1429 default: assert(0 && "This action is not supported yet!");
1430 case TargetLowering::Legal:
1431 break;
1432 case TargetLowering::Custom:
1433 Tmp3 = TLI.LowerOperation(Result, DAG);
1434 if (Tmp3.Val) {
1435 Result = Tmp3;
1436 break;
1437 }
1438 // FALLTHROUGH
1439 case TargetLowering::Expand:
1440 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1441 break;
1442 }
1443 break;
1444 case ISD::VECTOR_SHUFFLE:
1445 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1446 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1447 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1448
1449 // Allow targets to custom lower the SHUFFLEs they support.
1450 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1451 default: assert(0 && "Unknown operation action!");
1452 case TargetLowering::Legal:
1453 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1454 "vector shuffle should not be created if not legal!");
1455 break;
1456 case TargetLowering::Custom:
1457 Tmp3 = TLI.LowerOperation(Result, DAG);
1458 if (Tmp3.Val) {
1459 Result = Tmp3;
1460 break;
1461 }
1462 // FALLTHROUGH
1463 case TargetLowering::Expand: {
1464 MVT::ValueType VT = Node->getValueType(0);
1465 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1466 MVT::ValueType PtrVT = TLI.getPointerTy();
1467 SDOperand Mask = Node->getOperand(2);
1468 unsigned NumElems = Mask.getNumOperands();
1469 SmallVector<SDOperand,8> Ops;
1470 for (unsigned i = 0; i != NumElems; ++i) {
1471 SDOperand Arg = Mask.getOperand(i);
1472 if (Arg.getOpcode() == ISD::UNDEF) {
1473 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1474 } else {
1475 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1476 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1477 if (Idx < NumElems)
1478 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1479 DAG.getConstant(Idx, PtrVT)));
1480 else
1481 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1482 DAG.getConstant(Idx - NumElems, PtrVT)));
1483 }
1484 }
1485 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1486 break;
1487 }
1488 case TargetLowering::Promote: {
1489 // Change base type to a different vector type.
1490 MVT::ValueType OVT = Node->getValueType(0);
1491 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1492
1493 // Cast the two input vectors.
1494 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1495 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1496
1497 // Convert the shuffle mask to the right # elements.
1498 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1499 assert(Tmp3.Val && "Shuffle not legal?");
1500 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1501 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1502 break;
1503 }
1504 }
1505 break;
1506
1507 case ISD::EXTRACT_VECTOR_ELT:
1508 Tmp1 = Node->getOperand(0);
1509 Tmp2 = LegalizeOp(Node->getOperand(1));
1510 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1511 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1512 break;
1513
1514 case ISD::EXTRACT_SUBVECTOR:
1515 Tmp1 = Node->getOperand(0);
1516 Tmp2 = LegalizeOp(Node->getOperand(1));
1517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1518 Result = ExpandEXTRACT_SUBVECTOR(Result);
1519 break;
1520
1521 case ISD::CALLSEQ_START: {
1522 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1523
1524 // Recursively Legalize all of the inputs of the call end that do not lead
1525 // to this call start. This ensures that any libcalls that need be inserted
1526 // are inserted *before* the CALLSEQ_START.
1527 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1528 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1529 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1530 NodesLeadingTo);
1531 }
1532
1533 // Now that we legalized all of the inputs (which may have inserted
1534 // libcalls) create the new CALLSEQ_START node.
1535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1536
1537 // Merge in the last call, to ensure that this call start after the last
1538 // call ended.
1539 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1540 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1541 Tmp1 = LegalizeOp(Tmp1);
1542 }
1543
1544 // Do not try to legalize the target-specific arguments (#1+).
1545 if (Tmp1 != Node->getOperand(0)) {
1546 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1547 Ops[0] = Tmp1;
1548 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1549 }
1550
1551 // Remember that the CALLSEQ_START is legalized.
1552 AddLegalizedOperand(Op.getValue(0), Result);
1553 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1554 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1555
1556 // Now that the callseq_start and all of the non-call nodes above this call
1557 // sequence have been legalized, legalize the call itself. During this
1558 // process, no libcalls can/will be inserted, guaranteeing that no calls
1559 // can overlap.
1560 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1561 SDOperand InCallSEQ = LastCALLSEQ_END;
1562 // Note that we are selecting this call!
1563 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1564 IsLegalizingCall = true;
1565
1566 // Legalize the call, starting from the CALLSEQ_END.
1567 LegalizeOp(LastCALLSEQ_END);
1568 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1569 return Result;
1570 }
1571 case ISD::CALLSEQ_END:
1572 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1573 // will cause this node to be legalized as well as handling libcalls right.
1574 if (LastCALLSEQ_END.Val != Node) {
1575 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1576 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1577 assert(I != LegalizedNodes.end() &&
1578 "Legalizing the call start should have legalized this node!");
1579 return I->second;
1580 }
1581
1582 // Otherwise, the call start has been legalized and everything is going
1583 // according to plan. Just legalize ourselves normally here.
1584 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1585 // Do not try to legalize the target-specific arguments (#1+), except for
1586 // an optional flag input.
1587 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1588 if (Tmp1 != Node->getOperand(0)) {
1589 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1590 Ops[0] = Tmp1;
1591 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1592 }
1593 } else {
1594 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1595 if (Tmp1 != Node->getOperand(0) ||
1596 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1597 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1598 Ops[0] = Tmp1;
1599 Ops.back() = Tmp2;
1600 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1601 }
1602 }
1603 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1604 // This finishes up call legalization.
1605 IsLegalizingCall = false;
1606
1607 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1608 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1609 if (Node->getNumValues() == 2)
1610 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1611 return Result.getValue(Op.ResNo);
1612 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001613 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1615 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1616 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1617 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1618
1619 Tmp1 = Result.getValue(0);
1620 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001621 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001622 default: assert(0 && "This action is not supported yet!");
1623 case TargetLowering::Expand: {
1624 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1625 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1626 " not tell us which reg is the stack pointer!");
1627 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001628
1629 // Chain the dynamic stack allocation so that it doesn't modify the stack
1630 // pointer when other instructions are using the stack.
1631 Chain = DAG.getCALLSEQ_START(Chain,
1632 DAG.getConstant(0, TLI.getPointerTy()));
1633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001635 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1636 Chain = SP.getValue(1);
1637 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1638 unsigned StackAlign =
1639 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1640 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001641 SP = DAG.getNode(ISD::AND, VT, SP,
1642 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001643 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001644 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1645
1646 Tmp2 =
1647 DAG.getCALLSEQ_END(Chain,
1648 DAG.getConstant(0, TLI.getPointerTy()),
1649 DAG.getConstant(0, TLI.getPointerTy()),
1650 SDOperand());
1651
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 Tmp1 = LegalizeOp(Tmp1);
1653 Tmp2 = LegalizeOp(Tmp2);
1654 break;
1655 }
1656 case TargetLowering::Custom:
1657 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1658 if (Tmp3.Val) {
1659 Tmp1 = LegalizeOp(Tmp3);
1660 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1661 }
1662 break;
1663 case TargetLowering::Legal:
1664 break;
1665 }
1666 // Since this op produce two values, make sure to remember that we
1667 // legalized both of them.
1668 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1669 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1670 return Op.ResNo ? Tmp2 : Tmp1;
1671 }
1672 case ISD::INLINEASM: {
1673 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1674 bool Changed = false;
1675 // Legalize all of the operands of the inline asm, in case they are nodes
1676 // that need to be expanded or something. Note we skip the asm string and
1677 // all of the TargetConstant flags.
1678 SDOperand Op = LegalizeOp(Ops[0]);
1679 Changed = Op != Ops[0];
1680 Ops[0] = Op;
1681
1682 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1683 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1684 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1685 for (++i; NumVals; ++i, --NumVals) {
1686 SDOperand Op = LegalizeOp(Ops[i]);
1687 if (Op != Ops[i]) {
1688 Changed = true;
1689 Ops[i] = Op;
1690 }
1691 }
1692 }
1693
1694 if (HasInFlag) {
1695 Op = LegalizeOp(Ops.back());
1696 Changed |= Op != Ops.back();
1697 Ops.back() = Op;
1698 }
1699
1700 if (Changed)
1701 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1702
1703 // INLINE asm returns a chain and flag, make sure to add both to the map.
1704 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1705 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1706 return Result.getValue(Op.ResNo);
1707 }
1708 case ISD::BR:
1709 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1710 // Ensure that libcalls are emitted before a branch.
1711 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1712 Tmp1 = LegalizeOp(Tmp1);
1713 LastCALLSEQ_END = DAG.getEntryNode();
1714
1715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1716 break;
1717 case ISD::BRIND:
1718 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1719 // Ensure that libcalls are emitted before a branch.
1720 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1721 Tmp1 = LegalizeOp(Tmp1);
1722 LastCALLSEQ_END = DAG.getEntryNode();
1723
1724 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1725 default: assert(0 && "Indirect target must be legal type (pointer)!");
1726 case Legal:
1727 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1728 break;
1729 }
1730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1731 break;
1732 case ISD::BR_JT:
1733 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1734 // Ensure that libcalls are emitted before a branch.
1735 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1736 Tmp1 = LegalizeOp(Tmp1);
1737 LastCALLSEQ_END = DAG.getEntryNode();
1738
1739 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1740 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1741
1742 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1743 default: assert(0 && "This action is not supported yet!");
1744 case TargetLowering::Legal: break;
1745 case TargetLowering::Custom:
1746 Tmp1 = TLI.LowerOperation(Result, DAG);
1747 if (Tmp1.Val) Result = Tmp1;
1748 break;
1749 case TargetLowering::Expand: {
1750 SDOperand Chain = Result.getOperand(0);
1751 SDOperand Table = Result.getOperand(1);
1752 SDOperand Index = Result.getOperand(2);
1753
1754 MVT::ValueType PTy = TLI.getPointerTy();
1755 MachineFunction &MF = DAG.getMachineFunction();
1756 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1757 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1758 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1759
1760 SDOperand LD;
1761 switch (EntrySize) {
1762 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001763 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001764 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001765 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001766 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 }
1768
Evan Cheng6fb06762007-11-09 01:32:10 +00001769 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001770 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1771 // For PIC, the sequence is:
1772 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001773 // RelocBase can be JumpTable, GOT or some sort of global base.
1774 if (PTy != MVT::i32)
1775 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1776 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1777 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001779 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 }
1781 }
1782 break;
1783 case ISD::BRCOND:
1784 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1785 // Ensure that libcalls are emitted before a return.
1786 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1787 Tmp1 = LegalizeOp(Tmp1);
1788 LastCALLSEQ_END = DAG.getEntryNode();
1789
1790 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1791 case Expand: assert(0 && "It's impossible to expand bools");
1792 case Legal:
1793 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1794 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001795 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1797
1798 // The top bits of the promoted condition are not necessarily zero, ensure
1799 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001800 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001802 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001803 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1804 break;
1805 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001806 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807
1808 // Basic block destination (Op#2) is always legal.
1809 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1810
1811 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1812 default: assert(0 && "This action is not supported yet!");
1813 case TargetLowering::Legal: break;
1814 case TargetLowering::Custom:
1815 Tmp1 = TLI.LowerOperation(Result, DAG);
1816 if (Tmp1.Val) Result = Tmp1;
1817 break;
1818 case TargetLowering::Expand:
1819 // Expand brcond's setcc into its constituent parts and create a BR_CC
1820 // Node.
1821 if (Tmp2.getOpcode() == ISD::SETCC) {
1822 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1823 Tmp2.getOperand(0), Tmp2.getOperand(1),
1824 Node->getOperand(2));
1825 } else {
1826 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1827 DAG.getCondCode(ISD::SETNE), Tmp2,
1828 DAG.getConstant(0, Tmp2.getValueType()),
1829 Node->getOperand(2));
1830 }
1831 break;
1832 }
1833 break;
1834 case ISD::BR_CC:
1835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1836 // Ensure that libcalls are emitted before a branch.
1837 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1838 Tmp1 = LegalizeOp(Tmp1);
1839 Tmp2 = Node->getOperand(2); // LHS
1840 Tmp3 = Node->getOperand(3); // RHS
1841 Tmp4 = Node->getOperand(1); // CC
1842
1843 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1844 LastCALLSEQ_END = DAG.getEntryNode();
1845
1846 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1847 // the LHS is a legal SETCC itself. In this case, we need to compare
1848 // the result against zero to select between true and false values.
1849 if (Tmp3.Val == 0) {
1850 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1851 Tmp4 = DAG.getCondCode(ISD::SETNE);
1852 }
1853
1854 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1855 Node->getOperand(4));
1856
1857 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1858 default: assert(0 && "Unexpected action for BR_CC!");
1859 case TargetLowering::Legal: break;
1860 case TargetLowering::Custom:
1861 Tmp4 = TLI.LowerOperation(Result, DAG);
1862 if (Tmp4.Val) Result = Tmp4;
1863 break;
1864 }
1865 break;
1866 case ISD::LOAD: {
1867 LoadSDNode *LD = cast<LoadSDNode>(Node);
1868 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1869 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1870
1871 ISD::LoadExtType ExtType = LD->getExtensionType();
1872 if (ExtType == ISD::NON_EXTLOAD) {
1873 MVT::ValueType VT = Node->getValueType(0);
1874 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1875 Tmp3 = Result.getValue(0);
1876 Tmp4 = Result.getValue(1);
1877
1878 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1879 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001880 case TargetLowering::Legal:
1881 // If this is an unaligned load and the target doesn't support it,
1882 // expand it.
1883 if (!TLI.allowsUnalignedMemoryAccesses()) {
1884 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001885 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001886 if (LD->getAlignment() < ABIAlignment){
1887 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1888 TLI);
1889 Tmp3 = Result.getOperand(0);
1890 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001891 Tmp3 = LegalizeOp(Tmp3);
1892 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001893 }
1894 }
1895 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001896 case TargetLowering::Custom:
1897 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1898 if (Tmp1.Val) {
1899 Tmp3 = LegalizeOp(Tmp1);
1900 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1901 }
1902 break;
1903 case TargetLowering::Promote: {
1904 // Only promote a load of vector type to another.
1905 assert(MVT::isVector(VT) && "Cannot promote this load!");
1906 // Change base type to a different vector type.
1907 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1908
1909 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1910 LD->getSrcValueOffset(),
1911 LD->isVolatile(), LD->getAlignment());
1912 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1913 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1914 break;
1915 }
1916 }
1917 // Since loads produce two values, make sure to remember that we
1918 // legalized both of them.
1919 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1920 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1921 return Op.ResNo ? Tmp4 : Tmp3;
1922 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001923 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands082524c2008-01-23 20:39:46 +00001924 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1925 int SVOffset = LD->getSrcValueOffset();
1926 unsigned Alignment = LD->getAlignment();
1927 bool isVolatile = LD->isVolatile();
1928
1929 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1930 // Some targets pretend to have an i1 loading operation, and actually
1931 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1932 // bits are guaranteed to be zero; it helps the optimizers understand
1933 // that these bits are zero. It is also useful for EXTLOAD, since it
1934 // tells the optimizers that those bits are undefined. It would be
1935 // nice to have an effective generic way of getting these benefits...
1936 // Until such a way is found, don't insist on promoting i1 here.
1937 (SrcVT != MVT::i1 ||
1938 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1939 // Promote to a byte-sized load if not loading an integral number of
1940 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1941 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1942 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1943 SDOperand Ch;
1944
1945 // The extra bits are guaranteed to be zero, since we stored them that
1946 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1947
1948 ISD::LoadExtType NewExtType =
1949 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1950
1951 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1952 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1953 NVT, isVolatile, Alignment);
1954
1955 Ch = Result.getValue(1); // The chain.
1956
1957 if (ExtType == ISD::SEXTLOAD)
1958 // Having the top bits zero doesn't help when sign extending.
1959 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1960 Result, DAG.getValueType(SrcVT));
1961 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1962 // All the top bits are guaranteed to be zero - inform the optimizers.
1963 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1964 DAG.getValueType(SrcVT));
1965
1966 Tmp1 = LegalizeOp(Result);
1967 Tmp2 = LegalizeOp(Ch);
1968 } else if (SrcWidth & (SrcWidth - 1)) {
1969 // If not loading a power-of-2 number of bits, expand as two loads.
1970 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1971 "Unsupported extload!");
1972 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1973 assert(RoundWidth < SrcWidth);
1974 unsigned ExtraWidth = SrcWidth - RoundWidth;
1975 assert(ExtraWidth < RoundWidth);
1976 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1977 "Load size not an integral number of bytes!");
1978 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1979 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1980 SDOperand Lo, Hi, Ch;
1981 unsigned IncrementSize;
1982
1983 if (TLI.isLittleEndian()) {
1984 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1985 // Load the bottom RoundWidth bits.
1986 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1987 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1988 Alignment);
1989
1990 // Load the remaining ExtraWidth bits.
1991 IncrementSize = RoundWidth / 8;
1992 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1993 DAG.getIntPtrConstant(IncrementSize));
1994 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1995 LD->getSrcValue(), SVOffset + IncrementSize,
1996 ExtraVT, isVolatile,
1997 MinAlign(Alignment, IncrementSize));
1998
1999 // Build a factor node to remember that this load is independent of the
2000 // other one.
2001 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2002 Hi.getValue(1));
2003
2004 // Move the top bits to the right place.
2005 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2006 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2007
2008 // Join the hi and lo parts.
2009 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002010 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002011 // Big endian - avoid unaligned loads.
2012 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2013 // Load the top RoundWidth bits.
2014 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2015 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2016 Alignment);
2017
2018 // Load the remaining ExtraWidth bits.
2019 IncrementSize = RoundWidth / 8;
2020 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2021 DAG.getIntPtrConstant(IncrementSize));
2022 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2023 LD->getSrcValue(), SVOffset + IncrementSize,
2024 ExtraVT, isVolatile,
2025 MinAlign(Alignment, IncrementSize));
2026
2027 // Build a factor node to remember that this load is independent of the
2028 // other one.
2029 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2030 Hi.getValue(1));
2031
2032 // Move the top bits to the right place.
2033 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2034 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2035
2036 // Join the hi and lo parts.
2037 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2038 }
2039
2040 Tmp1 = LegalizeOp(Result);
2041 Tmp2 = LegalizeOp(Ch);
2042 } else {
2043 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2044 default: assert(0 && "This action is not supported yet!");
2045 case TargetLowering::Custom:
2046 isCustom = true;
2047 // FALLTHROUGH
2048 case TargetLowering::Legal:
2049 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2050 Tmp1 = Result.getValue(0);
2051 Tmp2 = Result.getValue(1);
2052
2053 if (isCustom) {
2054 Tmp3 = TLI.LowerOperation(Result, DAG);
2055 if (Tmp3.Val) {
2056 Tmp1 = LegalizeOp(Tmp3);
2057 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2058 }
2059 } else {
2060 // If this is an unaligned load and the target doesn't support it,
2061 // expand it.
2062 if (!TLI.allowsUnalignedMemoryAccesses()) {
2063 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002064 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands082524c2008-01-23 20:39:46 +00002065 if (LD->getAlignment() < ABIAlignment){
2066 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2067 TLI);
2068 Tmp1 = Result.getOperand(0);
2069 Tmp2 = Result.getOperand(1);
2070 Tmp1 = LegalizeOp(Tmp1);
2071 Tmp2 = LegalizeOp(Tmp2);
2072 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002073 }
2074 }
Duncan Sands082524c2008-01-23 20:39:46 +00002075 break;
2076 case TargetLowering::Expand:
2077 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2078 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2079 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2080 LD->getSrcValueOffset(),
2081 LD->isVolatile(), LD->getAlignment());
2082 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2083 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2084 Tmp2 = LegalizeOp(Load.getValue(1));
2085 break;
2086 }
2087 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2088 // Turn the unsupported load into an EXTLOAD followed by an explicit
2089 // zero/sign extend inreg.
2090 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2091 Tmp1, Tmp2, LD->getSrcValue(),
2092 LD->getSrcValueOffset(), SrcVT,
2093 LD->isVolatile(), LD->getAlignment());
2094 SDOperand ValRes;
2095 if (ExtType == ISD::SEXTLOAD)
2096 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2097 Result, DAG.getValueType(SrcVT));
2098 else
2099 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2100 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2101 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 break;
2103 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 }
Duncan Sands082524c2008-01-23 20:39:46 +00002105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 // Since loads produce two values, make sure to remember that we legalized
2107 // both of them.
2108 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2109 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2110 return Op.ResNo ? Tmp2 : Tmp1;
2111 }
2112 }
2113 case ISD::EXTRACT_ELEMENT: {
2114 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2115 switch (getTypeAction(OpTy)) {
2116 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2117 case Legal:
2118 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2119 // 1 -> Hi
2120 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2121 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2122 TLI.getShiftAmountTy()));
2123 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2124 } else {
2125 // 0 -> Lo
2126 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2127 Node->getOperand(0));
2128 }
2129 break;
2130 case Expand:
2131 // Get both the low and high parts.
2132 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2133 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2134 Result = Tmp2; // 1 -> Hi
2135 else
2136 Result = Tmp1; // 0 -> Lo
2137 break;
2138 }
2139 break;
2140 }
2141
2142 case ISD::CopyToReg:
2143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2144
2145 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2146 "Register type must be legal!");
2147 // Legalize the incoming value (must be a legal type).
2148 Tmp2 = LegalizeOp(Node->getOperand(2));
2149 if (Node->getNumValues() == 1) {
2150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2151 } else {
2152 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2153 if (Node->getNumOperands() == 4) {
2154 Tmp3 = LegalizeOp(Node->getOperand(3));
2155 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2156 Tmp3);
2157 } else {
2158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2159 }
2160
2161 // Since this produces two values, make sure to remember that we legalized
2162 // both of them.
2163 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2164 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2165 return Result;
2166 }
2167 break;
2168
2169 case ISD::RET:
2170 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2171
2172 // Ensure that libcalls are emitted before a return.
2173 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2174 Tmp1 = LegalizeOp(Tmp1);
2175 LastCALLSEQ_END = DAG.getEntryNode();
2176
2177 switch (Node->getNumOperands()) {
2178 case 3: // ret val
2179 Tmp2 = Node->getOperand(1);
2180 Tmp3 = Node->getOperand(2); // Signness
2181 switch (getTypeAction(Tmp2.getValueType())) {
2182 case Legal:
2183 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2184 break;
2185 case Expand:
2186 if (!MVT::isVector(Tmp2.getValueType())) {
2187 SDOperand Lo, Hi;
2188 ExpandOp(Tmp2, Lo, Hi);
2189
2190 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002191 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002192 std::swap(Lo, Hi);
2193
2194 if (Hi.Val)
2195 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2196 else
2197 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2198 Result = LegalizeOp(Result);
2199 } else {
2200 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002201 int InIx = Tmp2.ResNo;
2202 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2203 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002204
2205 // Figure out if there is a simple type corresponding to this Vector
2206 // type. If so, convert to the vector type.
2207 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2208 if (TLI.isTypeLegal(TVT)) {
2209 // Turn this into a return of the vector type.
2210 Tmp2 = LegalizeOp(Tmp2);
2211 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2212 } else if (NumElems == 1) {
2213 // Turn this into a return of the scalar type.
2214 Tmp2 = ScalarizeVectorOp(Tmp2);
2215 Tmp2 = LegalizeOp(Tmp2);
2216 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2217
2218 // FIXME: Returns of gcc generic vectors smaller than a legal type
2219 // should be returned in integer registers!
2220
2221 // The scalarized value type may not be legal, e.g. it might require
2222 // promotion or expansion. Relegalize the return.
2223 Result = LegalizeOp(Result);
2224 } else {
2225 // FIXME: Returns of gcc generic vectors larger than a legal vector
2226 // type should be returned by reference!
2227 SDOperand Lo, Hi;
2228 SplitVectorOp(Tmp2, Lo, Hi);
2229 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2230 Result = LegalizeOp(Result);
2231 }
2232 }
2233 break;
2234 case Promote:
2235 Tmp2 = PromoteOp(Node->getOperand(1));
2236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2237 Result = LegalizeOp(Result);
2238 break;
2239 }
2240 break;
2241 case 1: // ret void
2242 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2243 break;
2244 default: { // ret <values>
2245 SmallVector<SDOperand, 8> NewValues;
2246 NewValues.push_back(Tmp1);
2247 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2248 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2249 case Legal:
2250 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2251 NewValues.push_back(Node->getOperand(i+1));
2252 break;
2253 case Expand: {
2254 SDOperand Lo, Hi;
2255 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
2256 "FIXME: TODO: implement returning non-legal vector types!");
2257 ExpandOp(Node->getOperand(i), Lo, Hi);
2258 NewValues.push_back(Lo);
2259 NewValues.push_back(Node->getOperand(i+1));
2260 if (Hi.Val) {
2261 NewValues.push_back(Hi);
2262 NewValues.push_back(Node->getOperand(i+1));
2263 }
2264 break;
2265 }
2266 case Promote:
2267 assert(0 && "Can't promote multiple return value yet!");
2268 }
2269
2270 if (NewValues.size() == Node->getNumOperands())
2271 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2272 else
2273 Result = DAG.getNode(ISD::RET, MVT::Other,
2274 &NewValues[0], NewValues.size());
2275 break;
2276 }
2277 }
2278
2279 if (Result.getOpcode() == ISD::RET) {
2280 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2281 default: assert(0 && "This action is not supported yet!");
2282 case TargetLowering::Legal: break;
2283 case TargetLowering::Custom:
2284 Tmp1 = TLI.LowerOperation(Result, DAG);
2285 if (Tmp1.Val) Result = Tmp1;
2286 break;
2287 }
2288 }
2289 break;
2290 case ISD::STORE: {
2291 StoreSDNode *ST = cast<StoreSDNode>(Node);
2292 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2293 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2294 int SVOffset = ST->getSrcValueOffset();
2295 unsigned Alignment = ST->getAlignment();
2296 bool isVolatile = ST->isVolatile();
2297
2298 if (!ST->isTruncatingStore()) {
2299 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2300 // FIXME: We shouldn't do this for TargetConstantFP's.
2301 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2302 // to phase ordering between legalized code and the dag combiner. This
2303 // probably means that we need to integrate dag combiner and legalizer
2304 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002305 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002306 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002307 if (CFP->getValueType(0) == MVT::f32 &&
2308 getTypeAction(MVT::i32) == Legal) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00002309 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2310 convertToAPInt().getZExtValue(),
Dale Johannesen1616e902007-09-11 18:32:33 +00002311 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002312 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2313 SVOffset, isVolatile, Alignment);
2314 break;
2315 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002316 // If this target supports 64-bit registers, do a single 64-bit store.
2317 if (getTypeAction(MVT::i64) == Legal) {
2318 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2319 getZExtValue(), MVT::i64);
2320 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2321 SVOffset, isVolatile, Alignment);
2322 break;
2323 } else if (getTypeAction(MVT::i32) == Legal) {
2324 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2325 // stores. If the target supports neither 32- nor 64-bits, this
2326 // xform is certainly not worth it.
2327 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2328 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2329 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002330 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002331
2332 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2333 SVOffset, isVolatile, Alignment);
2334 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002335 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002336 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002337 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002338
2339 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2340 break;
2341 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002343 }
2344
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002345 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 case Legal: {
2347 Tmp3 = LegalizeOp(ST->getValue());
2348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2349 ST->getOffset());
2350
2351 MVT::ValueType VT = Tmp3.getValueType();
2352 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2353 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002354 case TargetLowering::Legal:
2355 // If this is an unaligned store and the target doesn't support it,
2356 // expand it.
2357 if (!TLI.allowsUnalignedMemoryAccesses()) {
2358 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002359 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002360 if (ST->getAlignment() < ABIAlignment)
2361 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2362 TLI);
2363 }
2364 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002365 case TargetLowering::Custom:
2366 Tmp1 = TLI.LowerOperation(Result, DAG);
2367 if (Tmp1.Val) Result = Tmp1;
2368 break;
2369 case TargetLowering::Promote:
2370 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2371 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2372 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2373 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2374 ST->getSrcValue(), SVOffset, isVolatile,
2375 Alignment);
2376 break;
2377 }
2378 break;
2379 }
2380 case Promote:
2381 // Truncate the value and store the result.
2382 Tmp3 = PromoteOp(ST->getValue());
2383 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002384 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 isVolatile, Alignment);
2386 break;
2387
2388 case Expand:
2389 unsigned IncrementSize = 0;
2390 SDOperand Lo, Hi;
2391
2392 // If this is a vector type, then we have to calculate the increment as
2393 // the product of the element size in bytes, and the number of elements
2394 // in the high half of the vector.
2395 if (MVT::isVector(ST->getValue().getValueType())) {
2396 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002397 int InIx = ST->getValue().ResNo;
Chris Lattner5872a362008-01-17 07:00:52 +00002398 MVT::ValueType InVT = InVal->getValueType(InIx);
2399 unsigned NumElems = MVT::getVectorNumElements(InVT);
2400 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002401
2402 // Figure out if there is a simple type corresponding to this Vector
2403 // type. If so, convert to the vector type.
2404 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2405 if (TLI.isTypeLegal(TVT)) {
2406 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002407 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002408 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2409 SVOffset, isVolatile, Alignment);
2410 Result = LegalizeOp(Result);
2411 break;
2412 } else if (NumElems == 1) {
2413 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002414 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2416 SVOffset, isVolatile, Alignment);
2417 // The scalarized value type may not be legal, e.g. it might require
2418 // promotion or expansion. Relegalize the scalar store.
2419 Result = LegalizeOp(Result);
2420 break;
2421 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002422 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00002423 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2424 MVT::getSizeInBits(EVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 }
2426 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002427 ExpandOp(ST->getValue(), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2429
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002430 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 std::swap(Lo, Hi);
2432 }
2433
2434 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2435 SVOffset, isVolatile, Alignment);
2436
2437 if (Hi.Val == NULL) {
2438 // Must be int <-> float one-to-one expansion.
2439 Result = Lo;
2440 break;
2441 }
2442
2443 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002444 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 assert(isTypeLegal(Tmp2.getValueType()) &&
2446 "Pointers must be legal!");
2447 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002448 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2450 SVOffset, isVolatile, Alignment);
2451 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2452 break;
2453 }
2454 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002455 switch (getTypeAction(ST->getValue().getValueType())) {
2456 case Legal:
2457 Tmp3 = LegalizeOp(ST->getValue());
2458 break;
2459 case Promote:
2460 // We can promote the value, the truncstore will still take care of it.
2461 Tmp3 = PromoteOp(ST->getValue());
2462 break;
2463 case Expand:
2464 // Just store the low part. This may become a non-trunc store, so make
2465 // sure to use getTruncStore, not UpdateNodeOperands below.
2466 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2467 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2468 SVOffset, MVT::i8, isVolatile, Alignment);
2469 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002471 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands40676662008-01-22 07:17:34 +00002472 unsigned StWidth = MVT::getSizeInBits(StVT);
2473
2474 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2475 // Promote to a byte-sized store with upper bits zero if not
2476 // storing an integral number of bytes. For example, promote
2477 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2478 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2479 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2480 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2481 SVOffset, NVT, isVolatile, Alignment);
2482 } else if (StWidth & (StWidth - 1)) {
2483 // If not storing a power-of-2 number of bits, expand as two stores.
2484 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2485 "Unsupported truncstore!");
2486 unsigned RoundWidth = 1 << Log2_32(StWidth);
2487 assert(RoundWidth < StWidth);
2488 unsigned ExtraWidth = StWidth - RoundWidth;
2489 assert(ExtraWidth < RoundWidth);
2490 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2491 "Store size not an integral number of bytes!");
2492 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2493 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2494 SDOperand Lo, Hi;
2495 unsigned IncrementSize;
2496
2497 if (TLI.isLittleEndian()) {
2498 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2499 // Store the bottom RoundWidth bits.
2500 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2501 SVOffset, RoundVT,
2502 isVolatile, Alignment);
2503
2504 // Store the remaining ExtraWidth bits.
2505 IncrementSize = RoundWidth / 8;
2506 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2507 DAG.getIntPtrConstant(IncrementSize));
2508 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2509 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2510 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2511 SVOffset + IncrementSize, ExtraVT, isVolatile,
2512 MinAlign(Alignment, IncrementSize));
2513 } else {
2514 // Big endian - avoid unaligned stores.
2515 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2516 // Store the top RoundWidth bits.
2517 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2518 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2519 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2520 RoundVT, isVolatile, Alignment);
2521
2522 // Store the remaining ExtraWidth bits.
2523 IncrementSize = RoundWidth / 8;
2524 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2525 DAG.getIntPtrConstant(IncrementSize));
2526 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2527 SVOffset + IncrementSize, ExtraVT, isVolatile,
2528 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002529 }
Duncan Sands40676662008-01-22 07:17:34 +00002530
2531 // The order of the stores doesn't matter.
2532 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2533 } else {
2534 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2535 Tmp2 != ST->getBasePtr())
2536 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2537 ST->getOffset());
2538
2539 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2540 default: assert(0 && "This action is not supported yet!");
2541 case TargetLowering::Legal:
2542 // If this is an unaligned store and the target doesn't support it,
2543 // expand it.
2544 if (!TLI.allowsUnalignedMemoryAccesses()) {
2545 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002546 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands40676662008-01-22 07:17:34 +00002547 if (ST->getAlignment() < ABIAlignment)
2548 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2549 TLI);
2550 }
2551 break;
2552 case TargetLowering::Custom:
2553 Result = TLI.LowerOperation(Result, DAG);
2554 break;
2555 case Expand:
2556 // TRUNCSTORE:i16 i32 -> STORE i16
2557 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2558 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2559 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2560 isVolatile, Alignment);
2561 break;
2562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563 }
2564 }
2565 break;
2566 }
2567 case ISD::PCMARKER:
2568 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2569 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2570 break;
2571 case ISD::STACKSAVE:
2572 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2573 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2574 Tmp1 = Result.getValue(0);
2575 Tmp2 = Result.getValue(1);
2576
2577 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2578 default: assert(0 && "This action is not supported yet!");
2579 case TargetLowering::Legal: break;
2580 case TargetLowering::Custom:
2581 Tmp3 = TLI.LowerOperation(Result, DAG);
2582 if (Tmp3.Val) {
2583 Tmp1 = LegalizeOp(Tmp3);
2584 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2585 }
2586 break;
2587 case TargetLowering::Expand:
2588 // Expand to CopyFromReg if the target set
2589 // StackPointerRegisterToSaveRestore.
2590 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2591 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2592 Node->getValueType(0));
2593 Tmp2 = Tmp1.getValue(1);
2594 } else {
2595 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2596 Tmp2 = Node->getOperand(0);
2597 }
2598 break;
2599 }
2600
2601 // Since stacksave produce two values, make sure to remember that we
2602 // legalized both of them.
2603 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2604 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2605 return Op.ResNo ? Tmp2 : Tmp1;
2606
2607 case ISD::STACKRESTORE:
2608 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2609 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2610 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2611
2612 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2613 default: assert(0 && "This action is not supported yet!");
2614 case TargetLowering::Legal: break;
2615 case TargetLowering::Custom:
2616 Tmp1 = TLI.LowerOperation(Result, DAG);
2617 if (Tmp1.Val) Result = Tmp1;
2618 break;
2619 case TargetLowering::Expand:
2620 // Expand to CopyToReg if the target set
2621 // StackPointerRegisterToSaveRestore.
2622 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2623 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2624 } else {
2625 Result = Tmp1;
2626 }
2627 break;
2628 }
2629 break;
2630
2631 case ISD::READCYCLECOUNTER:
2632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2633 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2634 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2635 Node->getValueType(0))) {
2636 default: assert(0 && "This action is not supported yet!");
2637 case TargetLowering::Legal:
2638 Tmp1 = Result.getValue(0);
2639 Tmp2 = Result.getValue(1);
2640 break;
2641 case TargetLowering::Custom:
2642 Result = TLI.LowerOperation(Result, DAG);
2643 Tmp1 = LegalizeOp(Result.getValue(0));
2644 Tmp2 = LegalizeOp(Result.getValue(1));
2645 break;
2646 }
2647
2648 // Since rdcc produce two values, make sure to remember that we legalized
2649 // both of them.
2650 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2651 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2652 return Result;
2653
2654 case ISD::SELECT:
2655 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2656 case Expand: assert(0 && "It's impossible to expand bools");
2657 case Legal:
2658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2659 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002660 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2662 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002663 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002665 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002666 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2667 break;
2668 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002669 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2671 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2672
2673 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2674
2675 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2676 default: assert(0 && "This action is not supported yet!");
2677 case TargetLowering::Legal: break;
2678 case TargetLowering::Custom: {
2679 Tmp1 = TLI.LowerOperation(Result, DAG);
2680 if (Tmp1.Val) Result = Tmp1;
2681 break;
2682 }
2683 case TargetLowering::Expand:
2684 if (Tmp1.getOpcode() == ISD::SETCC) {
2685 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2686 Tmp2, Tmp3,
2687 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2688 } else {
2689 Result = DAG.getSelectCC(Tmp1,
2690 DAG.getConstant(0, Tmp1.getValueType()),
2691 Tmp2, Tmp3, ISD::SETNE);
2692 }
2693 break;
2694 case TargetLowering::Promote: {
2695 MVT::ValueType NVT =
2696 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2697 unsigned ExtOp, TruncOp;
2698 if (MVT::isVector(Tmp2.getValueType())) {
2699 ExtOp = ISD::BIT_CONVERT;
2700 TruncOp = ISD::BIT_CONVERT;
2701 } else if (MVT::isInteger(Tmp2.getValueType())) {
2702 ExtOp = ISD::ANY_EXTEND;
2703 TruncOp = ISD::TRUNCATE;
2704 } else {
2705 ExtOp = ISD::FP_EXTEND;
2706 TruncOp = ISD::FP_ROUND;
2707 }
2708 // Promote each of the values to the new type.
2709 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2710 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2711 // Perform the larger operation, then round down.
2712 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002713 if (TruncOp != ISD::FP_ROUND)
2714 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2715 else
2716 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2717 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 break;
2719 }
2720 }
2721 break;
2722 case ISD::SELECT_CC: {
2723 Tmp1 = Node->getOperand(0); // LHS
2724 Tmp2 = Node->getOperand(1); // RHS
2725 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2726 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2727 SDOperand CC = Node->getOperand(4);
2728
2729 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2730
2731 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2732 // the LHS is a legal SETCC itself. In this case, we need to compare
2733 // the result against zero to select between true and false values.
2734 if (Tmp2.Val == 0) {
2735 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2736 CC = DAG.getCondCode(ISD::SETNE);
2737 }
2738 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2739
2740 // Everything is legal, see if we should expand this op or something.
2741 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2742 default: assert(0 && "This action is not supported yet!");
2743 case TargetLowering::Legal: break;
2744 case TargetLowering::Custom:
2745 Tmp1 = TLI.LowerOperation(Result, DAG);
2746 if (Tmp1.Val) Result = Tmp1;
2747 break;
2748 }
2749 break;
2750 }
2751 case ISD::SETCC:
2752 Tmp1 = Node->getOperand(0);
2753 Tmp2 = Node->getOperand(1);
2754 Tmp3 = Node->getOperand(2);
2755 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2756
2757 // If we had to Expand the SetCC operands into a SELECT node, then it may
2758 // not always be possible to return a true LHS & RHS. In this case, just
2759 // return the value we legalized, returned in the LHS
2760 if (Tmp2.Val == 0) {
2761 Result = Tmp1;
2762 break;
2763 }
2764
2765 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2766 default: assert(0 && "Cannot handle this action for SETCC yet!");
2767 case TargetLowering::Custom:
2768 isCustom = true;
2769 // FALLTHROUGH.
2770 case TargetLowering::Legal:
2771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2772 if (isCustom) {
2773 Tmp4 = TLI.LowerOperation(Result, DAG);
2774 if (Tmp4.Val) Result = Tmp4;
2775 }
2776 break;
2777 case TargetLowering::Promote: {
2778 // First step, figure out the appropriate operation to use.
2779 // Allow SETCC to not be supported for all legal data types
2780 // Mostly this targets FP
2781 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2782 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2783
2784 // Scan for the appropriate larger type to use.
2785 while (1) {
2786 NewInTy = (MVT::ValueType)(NewInTy+1);
2787
2788 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2789 "Fell off of the edge of the integer world");
2790 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2791 "Fell off of the edge of the floating point world");
2792
2793 // If the target supports SETCC of this type, use it.
2794 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2795 break;
2796 }
2797 if (MVT::isInteger(NewInTy))
2798 assert(0 && "Cannot promote Legal Integer SETCC yet");
2799 else {
2800 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2801 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2802 }
2803 Tmp1 = LegalizeOp(Tmp1);
2804 Tmp2 = LegalizeOp(Tmp2);
2805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2806 Result = LegalizeOp(Result);
2807 break;
2808 }
2809 case TargetLowering::Expand:
2810 // Expand a setcc node into a select_cc of the same condition, lhs, and
2811 // rhs that selects between const 1 (true) and const 0 (false).
2812 MVT::ValueType VT = Node->getValueType(0);
2813 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2814 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2815 Tmp3);
2816 break;
2817 }
2818 break;
2819 case ISD::MEMSET:
2820 case ISD::MEMCPY:
2821 case ISD::MEMMOVE: {
2822 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2823 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2824
2825 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2826 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2827 case Expand: assert(0 && "Cannot expand a byte!");
2828 case Legal:
2829 Tmp3 = LegalizeOp(Node->getOperand(2));
2830 break;
2831 case Promote:
2832 Tmp3 = PromoteOp(Node->getOperand(2));
2833 break;
2834 }
2835 } else {
2836 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2837 }
2838
2839 SDOperand Tmp4;
2840 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2841 case Expand: {
2842 // Length is too big, just take the lo-part of the length.
2843 SDOperand HiPart;
2844 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2845 break;
2846 }
2847 case Legal:
2848 Tmp4 = LegalizeOp(Node->getOperand(3));
2849 break;
2850 case Promote:
2851 Tmp4 = PromoteOp(Node->getOperand(3));
2852 break;
2853 }
2854
2855 SDOperand Tmp5;
2856 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2857 case Expand: assert(0 && "Cannot expand this yet!");
2858 case Legal:
2859 Tmp5 = LegalizeOp(Node->getOperand(4));
2860 break;
2861 case Promote:
2862 Tmp5 = PromoteOp(Node->getOperand(4));
2863 break;
2864 }
2865
Rafael Espindola80825902007-10-19 10:41:11 +00002866 SDOperand Tmp6;
2867 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2868 case Expand: assert(0 && "Cannot expand this yet!");
2869 case Legal:
2870 Tmp6 = LegalizeOp(Node->getOperand(5));
2871 break;
2872 case Promote:
2873 Tmp6 = PromoteOp(Node->getOperand(5));
2874 break;
2875 }
2876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002877 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2878 default: assert(0 && "This action not implemented for this operation!");
2879 case TargetLowering::Custom:
2880 isCustom = true;
2881 // FALLTHROUGH
Rafael Espindola80825902007-10-19 10:41:11 +00002882 case TargetLowering::Legal: {
2883 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2884 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 if (isCustom) {
2886 Tmp1 = TLI.LowerOperation(Result, DAG);
2887 if (Tmp1.Val) Result = Tmp1;
2888 }
2889 break;
Rafael Espindola80825902007-10-19 10:41:11 +00002890 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 case TargetLowering::Expand: {
2892 // Otherwise, the target does not support this operation. Lower the
2893 // operation to an explicit libcall as appropriate.
2894 MVT::ValueType IntPtr = TLI.getPointerTy();
2895 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2896 TargetLowering::ArgListTy Args;
2897 TargetLowering::ArgListEntry Entry;
2898
2899 const char *FnName = 0;
2900 if (Node->getOpcode() == ISD::MEMSET) {
2901 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2902 Args.push_back(Entry);
2903 // Extend the (previously legalized) ubyte argument to be an int value
2904 // for the call.
2905 if (Tmp3.getValueType() > MVT::i32)
2906 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2907 else
2908 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2909 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2910 Args.push_back(Entry);
2911 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2912 Args.push_back(Entry);
2913
2914 FnName = "memset";
2915 } else if (Node->getOpcode() == ISD::MEMCPY ||
2916 Node->getOpcode() == ISD::MEMMOVE) {
2917 Entry.Ty = IntPtrTy;
2918 Entry.Node = Tmp2; Args.push_back(Entry);
2919 Entry.Node = Tmp3; Args.push_back(Entry);
2920 Entry.Node = Tmp4; Args.push_back(Entry);
2921 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2922 } else {
2923 assert(0 && "Unknown op!");
2924 }
2925
2926 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00002927 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2928 false, false, false, CallingConv::C, false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2930 Result = CallResult.second;
2931 break;
2932 }
2933 }
2934 break;
2935 }
2936
2937 case ISD::SHL_PARTS:
2938 case ISD::SRA_PARTS:
2939 case ISD::SRL_PARTS: {
2940 SmallVector<SDOperand, 8> Ops;
2941 bool Changed = false;
2942 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2943 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2944 Changed |= Ops.back() != Node->getOperand(i);
2945 }
2946 if (Changed)
2947 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2948
2949 switch (TLI.getOperationAction(Node->getOpcode(),
2950 Node->getValueType(0))) {
2951 default: assert(0 && "This action is not supported yet!");
2952 case TargetLowering::Legal: break;
2953 case TargetLowering::Custom:
2954 Tmp1 = TLI.LowerOperation(Result, DAG);
2955 if (Tmp1.Val) {
2956 SDOperand Tmp2, RetVal(0, 0);
2957 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2958 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2959 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2960 if (i == Op.ResNo)
2961 RetVal = Tmp2;
2962 }
2963 assert(RetVal.Val && "Illegal result number");
2964 return RetVal;
2965 }
2966 break;
2967 }
2968
2969 // Since these produce multiple values, make sure to remember that we
2970 // legalized all of them.
2971 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2972 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2973 return Result.getValue(Op.ResNo);
2974 }
2975
2976 // Binary operators
2977 case ISD::ADD:
2978 case ISD::SUB:
2979 case ISD::MUL:
2980 case ISD::MULHS:
2981 case ISD::MULHU:
2982 case ISD::UDIV:
2983 case ISD::SDIV:
2984 case ISD::AND:
2985 case ISD::OR:
2986 case ISD::XOR:
2987 case ISD::SHL:
2988 case ISD::SRL:
2989 case ISD::SRA:
2990 case ISD::FADD:
2991 case ISD::FSUB:
2992 case ISD::FMUL:
2993 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002994 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2996 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2997 case Expand: assert(0 && "Not possible");
2998 case Legal:
2999 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3000 break;
3001 case Promote:
3002 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3003 break;
3004 }
3005
3006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3007
3008 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3009 default: assert(0 && "BinOp legalize operation not supported");
3010 case TargetLowering::Legal: break;
3011 case TargetLowering::Custom:
3012 Tmp1 = TLI.LowerOperation(Result, DAG);
3013 if (Tmp1.Val) Result = Tmp1;
3014 break;
3015 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00003016 MVT::ValueType VT = Op.getValueType();
3017
3018 // See if multiply or divide can be lowered using two-result operations.
3019 SDVTList VTs = DAG.getVTList(VT, VT);
3020 if (Node->getOpcode() == ISD::MUL) {
3021 // We just need the low half of the multiply; try both the signed
3022 // and unsigned forms. If the target supports both SMUL_LOHI and
3023 // UMUL_LOHI, form a preference by checking which forms of plain
3024 // MULH it supports.
3025 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3026 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3027 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3028 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3029 unsigned OpToUse = 0;
3030 if (HasSMUL_LOHI && !HasMULHS) {
3031 OpToUse = ISD::SMUL_LOHI;
3032 } else if (HasUMUL_LOHI && !HasMULHU) {
3033 OpToUse = ISD::UMUL_LOHI;
3034 } else if (HasSMUL_LOHI) {
3035 OpToUse = ISD::SMUL_LOHI;
3036 } else if (HasUMUL_LOHI) {
3037 OpToUse = ISD::UMUL_LOHI;
3038 }
3039 if (OpToUse) {
3040 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3041 break;
3042 }
3043 }
3044 if (Node->getOpcode() == ISD::MULHS &&
3045 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3046 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3047 break;
3048 }
3049 if (Node->getOpcode() == ISD::MULHU &&
3050 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3051 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3052 break;
3053 }
3054 if (Node->getOpcode() == ISD::SDIV &&
3055 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3056 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3057 break;
3058 }
3059 if (Node->getOpcode() == ISD::UDIV &&
3060 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3061 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3062 break;
3063 }
3064
Dan Gohman6d05cac2007-10-11 23:57:53 +00003065 // Check to see if we have a libcall for this operator.
3066 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3067 bool isSigned = false;
3068 switch (Node->getOpcode()) {
3069 case ISD::UDIV:
3070 case ISD::SDIV:
3071 if (VT == MVT::i32) {
3072 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003074 isSigned = Node->getOpcode() == ISD::SDIV;
3075 }
3076 break;
3077 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003078 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3079 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003080 break;
3081 default: break;
3082 }
3083 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3084 SDOperand Dummy;
3085 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 break;
3087 }
3088
3089 assert(MVT::isVector(Node->getValueType(0)) &&
3090 "Cannot expand this binary operator!");
3091 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003092 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093 break;
3094 }
3095 case TargetLowering::Promote: {
3096 switch (Node->getOpcode()) {
3097 default: assert(0 && "Do not know how to promote this BinOp!");
3098 case ISD::AND:
3099 case ISD::OR:
3100 case ISD::XOR: {
3101 MVT::ValueType OVT = Node->getValueType(0);
3102 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3103 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3104 // Bit convert each of the values to the new type.
3105 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3106 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3107 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3108 // Bit convert the result back the original type.
3109 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3110 break;
3111 }
3112 }
3113 }
3114 }
3115 break;
3116
Dan Gohman475cd732007-10-05 14:17:22 +00003117 case ISD::SMUL_LOHI:
3118 case ISD::UMUL_LOHI:
3119 case ISD::SDIVREM:
3120 case ISD::UDIVREM:
3121 // These nodes will only be produced by target-specific lowering, so
3122 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003123 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003124 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003125
3126 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3127 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003129 break;
3130
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3132 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3133 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3134 case Expand: assert(0 && "Not possible");
3135 case Legal:
3136 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3137 break;
3138 case Promote:
3139 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3140 break;
3141 }
3142
3143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3144
3145 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3146 default: assert(0 && "Operation not supported");
3147 case TargetLowering::Custom:
3148 Tmp1 = TLI.LowerOperation(Result, DAG);
3149 if (Tmp1.Val) Result = Tmp1;
3150 break;
3151 case TargetLowering::Legal: break;
3152 case TargetLowering::Expand: {
3153 // If this target supports fabs/fneg natively and select is cheap,
3154 // do this efficiently.
3155 if (!TLI.isSelectExpensive() &&
3156 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3157 TargetLowering::Legal &&
3158 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3159 TargetLowering::Legal) {
3160 // Get the sign bit of the RHS.
3161 MVT::ValueType IVT =
3162 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3163 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3164 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3165 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3166 // Get the absolute value of the result.
3167 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3168 // Select between the nabs and abs value based on the sign bit of
3169 // the input.
3170 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3171 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3172 AbsVal),
3173 AbsVal);
3174 Result = LegalizeOp(Result);
3175 break;
3176 }
3177
3178 // Otherwise, do bitwise ops!
3179 MVT::ValueType NVT =
3180 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3181 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3182 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3183 Result = LegalizeOp(Result);
3184 break;
3185 }
3186 }
3187 break;
3188
3189 case ISD::ADDC:
3190 case ISD::SUBC:
3191 Tmp1 = LegalizeOp(Node->getOperand(0));
3192 Tmp2 = LegalizeOp(Node->getOperand(1));
3193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3194 // Since this produces two values, make sure to remember that we legalized
3195 // both of them.
3196 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3197 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3198 return Result;
3199
3200 case ISD::ADDE:
3201 case ISD::SUBE:
3202 Tmp1 = LegalizeOp(Node->getOperand(0));
3203 Tmp2 = LegalizeOp(Node->getOperand(1));
3204 Tmp3 = LegalizeOp(Node->getOperand(2));
3205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3206 // Since this produces two values, make sure to remember that we legalized
3207 // both of them.
3208 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3209 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3210 return Result;
3211
3212 case ISD::BUILD_PAIR: {
3213 MVT::ValueType PairTy = Node->getValueType(0);
3214 // TODO: handle the case where the Lo and Hi operands are not of legal type
3215 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3216 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3217 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3218 case TargetLowering::Promote:
3219 case TargetLowering::Custom:
3220 assert(0 && "Cannot promote/custom this yet!");
3221 case TargetLowering::Legal:
3222 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3223 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3224 break;
3225 case TargetLowering::Expand:
3226 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3227 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3228 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3229 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3230 TLI.getShiftAmountTy()));
3231 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3232 break;
3233 }
3234 break;
3235 }
3236
3237 case ISD::UREM:
3238 case ISD::SREM:
3239 case ISD::FREM:
3240 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3241 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3242
3243 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3244 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3245 case TargetLowering::Custom:
3246 isCustom = true;
3247 // FALLTHROUGH
3248 case TargetLowering::Legal:
3249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3250 if (isCustom) {
3251 Tmp1 = TLI.LowerOperation(Result, DAG);
3252 if (Tmp1.Val) Result = Tmp1;
3253 }
3254 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003255 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3257 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00003258 MVT::ValueType VT = Node->getValueType(0);
3259
3260 // See if remainder can be lowered using two-result operations.
3261 SDVTList VTs = DAG.getVTList(VT, VT);
3262 if (Node->getOpcode() == ISD::SREM &&
3263 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3264 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3265 break;
3266 }
3267 if (Node->getOpcode() == ISD::UREM &&
3268 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3269 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3270 break;
3271 }
3272
3273 if (MVT::isInteger(VT)) {
3274 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003275 TargetLowering::Legal) {
3276 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3278 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3279 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003280 } else if (MVT::isVector(VT)) {
3281 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003283 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 "Cannot expand this binary operator!");
3285 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3286 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3287 SDOperand Dummy;
3288 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
3289 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003290 } else {
3291 assert(MVT::isFloatingPoint(VT) &&
3292 "remainder op must have integer or floating-point type");
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003293 if (MVT::isVector(VT)) {
3294 Result = LegalizeOp(UnrollVectorOp(Op));
3295 } else {
3296 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003297 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3298 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003299 SDOperand Dummy;
3300 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3301 false/*sign irrelevant*/, Dummy);
3302 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003303 }
3304 break;
3305 }
Dan Gohman5a199552007-10-08 18:33:35 +00003306 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307 break;
3308 case ISD::VAARG: {
3309 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3310 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3311
3312 MVT::ValueType VT = Node->getValueType(0);
3313 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3314 default: assert(0 && "This action is not supported yet!");
3315 case TargetLowering::Custom:
3316 isCustom = true;
3317 // FALLTHROUGH
3318 case TargetLowering::Legal:
3319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3320 Result = Result.getValue(0);
3321 Tmp1 = Result.getValue(1);
3322
3323 if (isCustom) {
3324 Tmp2 = TLI.LowerOperation(Result, DAG);
3325 if (Tmp2.Val) {
3326 Result = LegalizeOp(Tmp2);
3327 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3328 }
3329 }
3330 break;
3331 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003332 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3333 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 // Increment the pointer, VAList, to the next vaarg
3335 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3336 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3337 TLI.getPointerTy()));
3338 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003339 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 // Load the actual argument out of the pointer VAList
3341 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3342 Tmp1 = LegalizeOp(Result.getValue(1));
3343 Result = LegalizeOp(Result);
3344 break;
3345 }
3346 }
3347 // Since VAARG produces two values, make sure to remember that we
3348 // legalized both of them.
3349 AddLegalizedOperand(SDOperand(Node, 0), Result);
3350 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3351 return Op.ResNo ? Tmp1 : Result;
3352 }
3353
3354 case ISD::VACOPY:
3355 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3356 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3357 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3358
3359 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3360 default: assert(0 && "This action is not supported yet!");
3361 case TargetLowering::Custom:
3362 isCustom = true;
3363 // FALLTHROUGH
3364 case TargetLowering::Legal:
3365 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3366 Node->getOperand(3), Node->getOperand(4));
3367 if (isCustom) {
3368 Tmp1 = TLI.LowerOperation(Result, DAG);
3369 if (Tmp1.Val) Result = Tmp1;
3370 }
3371 break;
3372 case TargetLowering::Expand:
3373 // This defaults to loading a pointer from the input and storing it to the
3374 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003375 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3376 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3377 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3378 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 break;
3380 }
3381 break;
3382
3383 case ISD::VAEND:
3384 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3385 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3386
3387 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3388 default: assert(0 && "This action is not supported yet!");
3389 case TargetLowering::Custom:
3390 isCustom = true;
3391 // FALLTHROUGH
3392 case TargetLowering::Legal:
3393 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3394 if (isCustom) {
3395 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3396 if (Tmp1.Val) Result = Tmp1;
3397 }
3398 break;
3399 case TargetLowering::Expand:
3400 Result = Tmp1; // Default to a no-op, return the chain
3401 break;
3402 }
3403 break;
3404
3405 case ISD::VASTART:
3406 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3407 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3408
3409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3410
3411 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3412 default: assert(0 && "This action is not supported yet!");
3413 case TargetLowering::Legal: break;
3414 case TargetLowering::Custom:
3415 Tmp1 = TLI.LowerOperation(Result, DAG);
3416 if (Tmp1.Val) Result = Tmp1;
3417 break;
3418 }
3419 break;
3420
3421 case ISD::ROTL:
3422 case ISD::ROTR:
3423 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3424 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3426 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3427 default:
3428 assert(0 && "ROTL/ROTR legalize operation not supported");
3429 break;
3430 case TargetLowering::Legal:
3431 break;
3432 case TargetLowering::Custom:
3433 Tmp1 = TLI.LowerOperation(Result, DAG);
3434 if (Tmp1.Val) Result = Tmp1;
3435 break;
3436 case TargetLowering::Promote:
3437 assert(0 && "Do not know how to promote ROTL/ROTR");
3438 break;
3439 case TargetLowering::Expand:
3440 assert(0 && "Do not know how to expand ROTL/ROTR");
3441 break;
3442 }
3443 break;
3444
3445 case ISD::BSWAP:
3446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3447 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3448 case TargetLowering::Custom:
3449 assert(0 && "Cannot custom legalize this yet!");
3450 case TargetLowering::Legal:
3451 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3452 break;
3453 case TargetLowering::Promote: {
3454 MVT::ValueType OVT = Tmp1.getValueType();
3455 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3456 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3457
3458 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3459 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3460 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3461 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3462 break;
3463 }
3464 case TargetLowering::Expand:
3465 Result = ExpandBSWAP(Tmp1);
3466 break;
3467 }
3468 break;
3469
3470 case ISD::CTPOP:
3471 case ISD::CTTZ:
3472 case ISD::CTLZ:
3473 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3474 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003475 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 case TargetLowering::Legal:
3477 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003478 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003479 TargetLowering::Custom) {
3480 Tmp1 = TLI.LowerOperation(Result, DAG);
3481 if (Tmp1.Val) {
3482 Result = Tmp1;
3483 }
Scott Michel48b63e62007-07-30 21:00:31 +00003484 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485 break;
3486 case TargetLowering::Promote: {
3487 MVT::ValueType OVT = Tmp1.getValueType();
3488 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3489
3490 // Zero extend the argument.
3491 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3492 // Perform the larger operation, then subtract if needed.
3493 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3494 switch (Node->getOpcode()) {
3495 case ISD::CTPOP:
3496 Result = Tmp1;
3497 break;
3498 case ISD::CTTZ:
3499 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3500 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3501 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3502 ISD::SETEQ);
3503 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003504 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003505 break;
3506 case ISD::CTLZ:
3507 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3508 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3509 DAG.getConstant(MVT::getSizeInBits(NVT) -
3510 MVT::getSizeInBits(OVT), NVT));
3511 break;
3512 }
3513 break;
3514 }
3515 case TargetLowering::Expand:
3516 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3517 break;
3518 }
3519 break;
3520
3521 // Unary operators
3522 case ISD::FABS:
3523 case ISD::FNEG:
3524 case ISD::FSQRT:
3525 case ISD::FSIN:
3526 case ISD::FCOS:
3527 Tmp1 = LegalizeOp(Node->getOperand(0));
3528 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3529 case TargetLowering::Promote:
3530 case TargetLowering::Custom:
3531 isCustom = true;
3532 // FALLTHROUGH
3533 case TargetLowering::Legal:
3534 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3535 if (isCustom) {
3536 Tmp1 = TLI.LowerOperation(Result, DAG);
3537 if (Tmp1.Val) Result = Tmp1;
3538 }
3539 break;
3540 case TargetLowering::Expand:
3541 switch (Node->getOpcode()) {
3542 default: assert(0 && "Unreachable!");
3543 case ISD::FNEG:
3544 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3545 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3546 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3547 break;
3548 case ISD::FABS: {
3549 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3550 MVT::ValueType VT = Node->getValueType(0);
3551 Tmp2 = DAG.getConstantFP(0.0, VT);
3552 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
3553 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3554 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3555 break;
3556 }
3557 case ISD::FSQRT:
3558 case ISD::FSIN:
3559 case ISD::FCOS: {
3560 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003561
3562 // Expand unsupported unary vector operators by unrolling them.
3563 if (MVT::isVector(VT)) {
3564 Result = LegalizeOp(UnrollVectorOp(Op));
3565 break;
3566 }
3567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003568 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3569 switch(Node->getOpcode()) {
3570 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003571 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3572 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003573 break;
3574 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003575 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3576 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577 break;
3578 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003579 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3580 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 break;
3582 default: assert(0 && "Unreachable!");
3583 }
3584 SDOperand Dummy;
3585 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3586 false/*sign irrelevant*/, Dummy);
3587 break;
3588 }
3589 }
3590 break;
3591 }
3592 break;
3593 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003594 MVT::ValueType VT = Node->getValueType(0);
3595
3596 // Expand unsupported unary vector operators by unrolling them.
3597 if (MVT::isVector(VT)) {
3598 Result = LegalizeOp(UnrollVectorOp(Op));
3599 break;
3600 }
3601
3602 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003603 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3604 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003605 SDOperand Dummy;
3606 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3607 false/*sign irrelevant*/, Dummy);
3608 break;
3609 }
3610 case ISD::BIT_CONVERT:
3611 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003612 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3613 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3615 // The input has to be a vector type, we have to either scalarize it, pack
3616 // it, or convert it based on whether the input vector type is legal.
3617 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003618 int InIx = Node->getOperand(0).ResNo;
3619 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3620 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621
3622 // Figure out if there is a simple type corresponding to this Vector
3623 // type. If so, convert to the vector type.
3624 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3625 if (TLI.isTypeLegal(TVT)) {
3626 // Turn this into a bit convert of the vector input.
3627 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3628 LegalizeOp(Node->getOperand(0)));
3629 break;
3630 } else if (NumElems == 1) {
3631 // Turn this into a bit convert of the scalar input.
3632 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3633 ScalarizeVectorOp(Node->getOperand(0)));
3634 break;
3635 } else {
3636 // FIXME: UNIMP! Store then reload
3637 assert(0 && "Cast from unsupported vector type not implemented yet!");
3638 }
3639 } else {
3640 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3641 Node->getOperand(0).getValueType())) {
3642 default: assert(0 && "Unknown operation action!");
3643 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003644 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3645 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003646 break;
3647 case TargetLowering::Legal:
3648 Tmp1 = LegalizeOp(Node->getOperand(0));
3649 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3650 break;
3651 }
3652 }
3653 break;
3654
3655 // Conversion operators. The source and destination have different types.
3656 case ISD::SINT_TO_FP:
3657 case ISD::UINT_TO_FP: {
3658 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3659 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3660 case Legal:
3661 switch (TLI.getOperationAction(Node->getOpcode(),
3662 Node->getOperand(0).getValueType())) {
3663 default: assert(0 && "Unknown operation action!");
3664 case TargetLowering::Custom:
3665 isCustom = true;
3666 // FALLTHROUGH
3667 case TargetLowering::Legal:
3668 Tmp1 = LegalizeOp(Node->getOperand(0));
3669 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3670 if (isCustom) {
3671 Tmp1 = TLI.LowerOperation(Result, DAG);
3672 if (Tmp1.Val) Result = Tmp1;
3673 }
3674 break;
3675 case TargetLowering::Expand:
3676 Result = ExpandLegalINT_TO_FP(isSigned,
3677 LegalizeOp(Node->getOperand(0)),
3678 Node->getValueType(0));
3679 break;
3680 case TargetLowering::Promote:
3681 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3682 Node->getValueType(0),
3683 isSigned);
3684 break;
3685 }
3686 break;
3687 case Expand:
3688 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3689 Node->getValueType(0), Node->getOperand(0));
3690 break;
3691 case Promote:
3692 Tmp1 = PromoteOp(Node->getOperand(0));
3693 if (isSigned) {
3694 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3695 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3696 } else {
3697 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3698 Node->getOperand(0).getValueType());
3699 }
3700 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3701 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3702 break;
3703 }
3704 break;
3705 }
3706 case ISD::TRUNCATE:
3707 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3708 case Legal:
3709 Tmp1 = LegalizeOp(Node->getOperand(0));
3710 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3711 break;
3712 case Expand:
3713 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3714
3715 // Since the result is legal, we should just be able to truncate the low
3716 // part of the source.
3717 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3718 break;
3719 case Promote:
3720 Result = PromoteOp(Node->getOperand(0));
3721 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3722 break;
3723 }
3724 break;
3725
3726 case ISD::FP_TO_SINT:
3727 case ISD::FP_TO_UINT:
3728 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3729 case Legal:
3730 Tmp1 = LegalizeOp(Node->getOperand(0));
3731
3732 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3733 default: assert(0 && "Unknown operation action!");
3734 case TargetLowering::Custom:
3735 isCustom = true;
3736 // FALLTHROUGH
3737 case TargetLowering::Legal:
3738 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3739 if (isCustom) {
3740 Tmp1 = TLI.LowerOperation(Result, DAG);
3741 if (Tmp1.Val) Result = Tmp1;
3742 }
3743 break;
3744 case TargetLowering::Promote:
3745 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3746 Node->getOpcode() == ISD::FP_TO_SINT);
3747 break;
3748 case TargetLowering::Expand:
3749 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3750 SDOperand True, False;
3751 MVT::ValueType VT = Node->getOperand(0).getValueType();
3752 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003753 const uint64_t zero[] = {0, 0};
3754 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohman88ae8c52008-02-29 01:44:25 +00003755 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3756 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003757 Tmp2 = DAG.getConstantFP(apf, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3759 Node->getOperand(0), Tmp2, ISD::SETLT);
3760 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3761 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3762 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3763 Tmp2));
3764 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003765 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3767 break;
3768 } else {
3769 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3770 }
3771 break;
3772 }
3773 break;
3774 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003776 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003777 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003778 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003779 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3780 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3781 Node->getOperand(0), DAG.getValueType(MVT::f64));
3782 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3783 DAG.getIntPtrConstant(1));
3784 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3785 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003786 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3787 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3788 Tmp2 = DAG.getConstantFP(apf, OVT);
3789 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3790 // FIXME: generated code sucks.
3791 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3792 DAG.getNode(ISD::ADD, MVT::i32,
3793 DAG.getNode(ISD::FP_TO_SINT, VT,
3794 DAG.getNode(ISD::FSUB, OVT,
3795 Node->getOperand(0), Tmp2)),
3796 DAG.getConstant(0x80000000, MVT::i32)),
3797 DAG.getNode(ISD::FP_TO_SINT, VT,
3798 Node->getOperand(0)),
3799 DAG.getCondCode(ISD::SETGE));
3800 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003801 break;
3802 }
Dale Johannesend3b6af32007-10-11 23:32:15 +00003803 // Convert f32 / f64 to i32 / i64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3805 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003806 case ISD::FP_TO_SINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003807 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003808 LC = (VT == MVT::i32)
3809 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003810 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003811 LC = (VT == MVT::i32)
3812 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003813 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003814 assert(VT == MVT::i64);
Dale Johannesenac77b272007-10-05 20:04:43 +00003815 LC = RTLIB::FPTOSINT_F80_I64;
3816 }
3817 else if (OVT == MVT::ppcf128) {
3818 assert(VT == MVT::i64);
3819 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003820 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003822 }
3823 case ISD::FP_TO_UINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003824 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 LC = (VT == MVT::i32)
3826 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003827 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003828 LC = (VT == MVT::i32)
3829 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003830 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003831 LC = (VT == MVT::i32)
Dale Johannesenac77b272007-10-05 20:04:43 +00003832 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3833 }
3834 else if (OVT == MVT::ppcf128) {
3835 assert(VT == MVT::i64);
3836 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003837 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003838 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003839 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840 default: assert(0 && "Unreachable!");
3841 }
3842 SDOperand Dummy;
3843 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3844 false/*sign irrelevant*/, Dummy);
3845 break;
3846 }
3847 case Promote:
3848 Tmp1 = PromoteOp(Node->getOperand(0));
3849 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3850 Result = LegalizeOp(Result);
3851 break;
3852 }
3853 break;
3854
Chris Lattner56ecde32008-01-16 06:57:07 +00003855 case ISD::FP_EXTEND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003856 MVT::ValueType DstVT = Op.getValueType();
3857 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3858 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3859 // The only other way we can lower this is to turn it into a STORE,
3860 // LOAD pair, targetting a temporary location (a stack slot).
3861 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3862 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003863 }
3864 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3865 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3866 case Legal:
3867 Tmp1 = LegalizeOp(Node->getOperand(0));
3868 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3869 break;
3870 case Promote:
3871 Tmp1 = PromoteOp(Node->getOperand(0));
3872 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3873 break;
3874 }
3875 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003876 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003877 case ISD::FP_ROUND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003878 MVT::ValueType DstVT = Op.getValueType();
3879 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3880 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3881 if (SrcVT == MVT::ppcf128) {
Dale Johannesena0d36082008-01-20 01:18:38 +00003882 SDOperand Lo;
3883 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003884 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003885 if (DstVT!=MVT::f64)
3886 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003887 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003888 }
Chris Lattner5872a362008-01-17 07:00:52 +00003889 // The only other way we can lower this is to turn it into a STORE,
3890 // LOAD pair, targetting a temporary location (a stack slot).
3891 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3892 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003894 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3895 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3896 case Legal:
3897 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003899 break;
3900 case Promote:
3901 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003902 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3903 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003904 break;
3905 }
3906 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003907 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 case ISD::ANY_EXTEND:
3909 case ISD::ZERO_EXTEND:
3910 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3912 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3913 case Legal:
3914 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac7091c2008-02-15 23:05:48 +00003915 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3916 TargetLowering::Custom) {
3917 Tmp2 = TLI.LowerOperation(Result, DAG);
3918 if (Tmp2.Val) {
3919 Tmp1 = Tmp2;
3920 }
3921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3923 break;
3924 case Promote:
3925 switch (Node->getOpcode()) {
3926 case ISD::ANY_EXTEND:
3927 Tmp1 = PromoteOp(Node->getOperand(0));
3928 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3929 break;
3930 case ISD::ZERO_EXTEND:
3931 Result = PromoteOp(Node->getOperand(0));
3932 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3933 Result = DAG.getZeroExtendInReg(Result,
3934 Node->getOperand(0).getValueType());
3935 break;
3936 case ISD::SIGN_EXTEND:
3937 Result = PromoteOp(Node->getOperand(0));
3938 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3939 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3940 Result,
3941 DAG.getValueType(Node->getOperand(0).getValueType()));
3942 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003943 }
3944 }
3945 break;
3946 case ISD::FP_ROUND_INREG:
3947 case ISD::SIGN_EXTEND_INREG: {
3948 Tmp1 = LegalizeOp(Node->getOperand(0));
3949 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3950
3951 // If this operation is not supported, convert it to a shl/shr or load/store
3952 // pair.
3953 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3954 default: assert(0 && "This action not supported for this op yet!");
3955 case TargetLowering::Legal:
3956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3957 break;
3958 case TargetLowering::Expand:
3959 // If this is an integer extend and shifts are supported, do that.
3960 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3961 // NOTE: we could fall back on load/store here too for targets without
3962 // SAR. However, it is doubtful that any exist.
3963 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3964 MVT::getSizeInBits(ExtraVT);
3965 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3966 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3967 Node->getOperand(0), ShiftCst);
3968 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3969 Result, ShiftCst);
3970 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3971 // The only way we can lower this is to turn it into a TRUNCSTORE,
3972 // EXTLOAD pair, targetting a temporary location (a stack slot).
3973
3974 // NOTE: there is a choice here between constantly creating new stack
3975 // slots and always reusing the same one. We currently always create
3976 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003977 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3978 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979 } else {
3980 assert(0 && "Unknown op");
3981 }
3982 break;
3983 }
3984 break;
3985 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003986 case ISD::TRAMPOLINE: {
3987 SDOperand Ops[6];
3988 for (unsigned i = 0; i != 6; ++i)
3989 Ops[i] = LegalizeOp(Node->getOperand(i));
3990 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3991 // The only option for this node is to custom lower it.
3992 Result = TLI.LowerOperation(Result, DAG);
3993 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003994
3995 // Since trampoline produces two values, make sure to remember that we
3996 // legalized both of them.
3997 Tmp1 = LegalizeOp(Result.getValue(1));
3998 Result = LegalizeOp(Result);
3999 AddLegalizedOperand(SDOperand(Node, 0), Result);
4000 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
4001 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00004002 }
Dan Gohman819574c2008-01-31 00:41:03 +00004003 case ISD::FLT_ROUNDS_: {
Anton Korobeynikovc915e272007-11-15 23:25:33 +00004004 MVT::ValueType VT = Node->getValueType(0);
4005 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4006 default: assert(0 && "This action not supported for this op yet!");
4007 case TargetLowering::Custom:
4008 Result = TLI.LowerOperation(Op, DAG);
4009 if (Result.Val) break;
4010 // Fall Thru
4011 case TargetLowering::Legal:
4012 // If this operation is not supported, lower it to constant 1
4013 Result = DAG.getConstant(1, VT);
4014 break;
4015 }
4016 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004017 case ISD::TRAP: {
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004018 MVT::ValueType VT = Node->getValueType(0);
4019 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4020 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00004021 case TargetLowering::Legal:
4022 Tmp1 = LegalizeOp(Node->getOperand(0));
4023 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4024 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004025 case TargetLowering::Custom:
4026 Result = TLI.LowerOperation(Op, DAG);
4027 if (Result.Val) break;
4028 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00004029 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004030 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00004031 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004032 TargetLowering::ArgListTy Args;
4033 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004034 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4035 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00004036 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4037 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004038 Result = CallResult.second;
4039 break;
4040 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00004041 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004042 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 }
4044
4045 assert(Result.getValueType() == Op.getValueType() &&
4046 "Bad legalization!");
4047
4048 // Make sure that the generated code is itself legal.
4049 if (Result != Op)
4050 Result = LegalizeOp(Result);
4051
4052 // Note that LegalizeOp may be reentered even from single-use nodes, which
4053 // means that we always must cache transformed nodes.
4054 AddLegalizedOperand(Op, Result);
4055 return Result;
4056}
4057
4058/// PromoteOp - Given an operation that produces a value in an invalid type,
4059/// promote it to compute the value into a larger type. The produced value will
4060/// have the correct bits for the low portion of the register, but no guarantee
4061/// is made about the top bits: it may be zero, sign-extended, or garbage.
4062SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4063 MVT::ValueType VT = Op.getValueType();
4064 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4065 assert(getTypeAction(VT) == Promote &&
4066 "Caller should expand or legalize operands that are not promotable!");
4067 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4068 "Cannot promote to smaller type!");
4069
4070 SDOperand Tmp1, Tmp2, Tmp3;
4071 SDOperand Result;
4072 SDNode *Node = Op.Val;
4073
4074 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
4075 if (I != PromotedNodes.end()) return I->second;
4076
4077 switch (Node->getOpcode()) {
4078 case ISD::CopyFromReg:
4079 assert(0 && "CopyFromReg must be legal!");
4080 default:
4081#ifndef NDEBUG
4082 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4083#endif
4084 assert(0 && "Do not know how to promote this operator!");
4085 abort();
4086 case ISD::UNDEF:
4087 Result = DAG.getNode(ISD::UNDEF, NVT);
4088 break;
4089 case ISD::Constant:
4090 if (VT != MVT::i1)
4091 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4092 else
4093 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4094 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4095 break;
4096 case ISD::ConstantFP:
4097 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4098 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4099 break;
4100
4101 case ISD::SETCC:
4102 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
4103 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4104 Node->getOperand(1), Node->getOperand(2));
4105 break;
4106
4107 case ISD::TRUNCATE:
4108 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4109 case Legal:
4110 Result = LegalizeOp(Node->getOperand(0));
4111 assert(Result.getValueType() >= NVT &&
4112 "This truncation doesn't make sense!");
4113 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4114 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4115 break;
4116 case Promote:
4117 // The truncation is not required, because we don't guarantee anything
4118 // about high bits anyway.
4119 Result = PromoteOp(Node->getOperand(0));
4120 break;
4121 case Expand:
4122 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4123 // Truncate the low part of the expanded value to the result type
4124 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4125 }
4126 break;
4127 case ISD::SIGN_EXTEND:
4128 case ISD::ZERO_EXTEND:
4129 case ISD::ANY_EXTEND:
4130 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4131 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4132 case Legal:
4133 // Input is legal? Just do extend all the way to the larger type.
4134 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4135 break;
4136 case Promote:
4137 // Promote the reg if it's smaller.
4138 Result = PromoteOp(Node->getOperand(0));
4139 // The high bits are not guaranteed to be anything. Insert an extend.
4140 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4141 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4142 DAG.getValueType(Node->getOperand(0).getValueType()));
4143 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4144 Result = DAG.getZeroExtendInReg(Result,
4145 Node->getOperand(0).getValueType());
4146 break;
4147 }
4148 break;
4149 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004150 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4151 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 Result = PromoteOp(Result);
4153 break;
4154
4155 case ISD::FP_EXTEND:
4156 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4157 case ISD::FP_ROUND:
4158 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4159 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4160 case Promote: assert(0 && "Unreachable with 2 FP types!");
4161 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004162 if (Node->getConstantOperandVal(1) == 0) {
4163 // Input is legal? Do an FP_ROUND_INREG.
4164 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4165 DAG.getValueType(VT));
4166 } else {
4167 // Just remove the truncate, it isn't affecting the value.
4168 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4169 Node->getOperand(1));
4170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 break;
4172 }
4173 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174 case ISD::SINT_TO_FP:
4175 case ISD::UINT_TO_FP:
4176 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4177 case Legal:
4178 // No extra round required here.
4179 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4180 break;
4181
4182 case Promote:
4183 Result = PromoteOp(Node->getOperand(0));
4184 if (Node->getOpcode() == ISD::SINT_TO_FP)
4185 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4186 Result,
4187 DAG.getValueType(Node->getOperand(0).getValueType()));
4188 else
4189 Result = DAG.getZeroExtendInReg(Result,
4190 Node->getOperand(0).getValueType());
4191 // No extra round required here.
4192 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4193 break;
4194 case Expand:
4195 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4196 Node->getOperand(0));
4197 // Round if we cannot tolerate excess precision.
4198 if (NoExcessFPPrecision)
4199 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4200 DAG.getValueType(VT));
4201 break;
4202 }
4203 break;
4204
4205 case ISD::SIGN_EXTEND_INREG:
4206 Result = PromoteOp(Node->getOperand(0));
4207 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4208 Node->getOperand(1));
4209 break;
4210 case ISD::FP_TO_SINT:
4211 case ISD::FP_TO_UINT:
4212 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4213 case Legal:
4214 case Expand:
4215 Tmp1 = Node->getOperand(0);
4216 break;
4217 case Promote:
4218 // The input result is prerounded, so we don't have to do anything
4219 // special.
4220 Tmp1 = PromoteOp(Node->getOperand(0));
4221 break;
4222 }
4223 // If we're promoting a UINT to a larger size, check to see if the new node
4224 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4225 // we can use that instead. This allows us to generate better code for
4226 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4227 // legal, such as PowerPC.
4228 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4229 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4230 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4231 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4232 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4233 } else {
4234 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4235 }
4236 break;
4237
4238 case ISD::FABS:
4239 case ISD::FNEG:
4240 Tmp1 = PromoteOp(Node->getOperand(0));
4241 assert(Tmp1.getValueType() == NVT);
4242 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4243 // NOTE: we do not have to do any extra rounding here for
4244 // NoExcessFPPrecision, because we know the input will have the appropriate
4245 // precision, and these operations don't modify precision at all.
4246 break;
4247
4248 case ISD::FSQRT:
4249 case ISD::FSIN:
4250 case ISD::FCOS:
4251 Tmp1 = PromoteOp(Node->getOperand(0));
4252 assert(Tmp1.getValueType() == NVT);
4253 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4254 if (NoExcessFPPrecision)
4255 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4256 DAG.getValueType(VT));
4257 break;
4258
4259 case ISD::FPOWI: {
4260 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4261 // directly as well, which may be better.
4262 Tmp1 = PromoteOp(Node->getOperand(0));
4263 assert(Tmp1.getValueType() == NVT);
4264 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4265 if (NoExcessFPPrecision)
4266 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4267 DAG.getValueType(VT));
4268 break;
4269 }
4270
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004271 case ISD::ATOMIC_LCS: {
4272 Tmp2 = PromoteOp(Node->getOperand(2));
4273 Tmp3 = PromoteOp(Node->getOperand(3));
4274 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4275 Node->getOperand(1), Tmp2, Tmp3,
4276 cast<AtomicSDNode>(Node)->getVT());
4277 // Remember that we legalized the chain.
4278 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4279 break;
4280 }
4281 case ISD::ATOMIC_LAS:
4282 case ISD::ATOMIC_SWAP: {
4283 Tmp2 = PromoteOp(Node->getOperand(2));
4284 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4285 Node->getOperand(1), Tmp2,
4286 cast<AtomicSDNode>(Node)->getVT());
4287 // Remember that we legalized the chain.
4288 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4289 break;
4290 }
4291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 case ISD::AND:
4293 case ISD::OR:
4294 case ISD::XOR:
4295 case ISD::ADD:
4296 case ISD::SUB:
4297 case ISD::MUL:
4298 // The input may have strange things in the top bits of the registers, but
4299 // these operations don't care. They may have weird bits going out, but
4300 // that too is okay if they are integer operations.
4301 Tmp1 = PromoteOp(Node->getOperand(0));
4302 Tmp2 = PromoteOp(Node->getOperand(1));
4303 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4304 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4305 break;
4306 case ISD::FADD:
4307 case ISD::FSUB:
4308 case ISD::FMUL:
4309 Tmp1 = PromoteOp(Node->getOperand(0));
4310 Tmp2 = PromoteOp(Node->getOperand(1));
4311 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4312 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4313
4314 // Floating point operations will give excess precision that we may not be
4315 // able to tolerate. If we DO allow excess precision, just leave it,
4316 // otherwise excise it.
4317 // FIXME: Why would we need to round FP ops more than integer ones?
4318 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4319 if (NoExcessFPPrecision)
4320 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4321 DAG.getValueType(VT));
4322 break;
4323
4324 case ISD::SDIV:
4325 case ISD::SREM:
4326 // These operators require that their input be sign extended.
4327 Tmp1 = PromoteOp(Node->getOperand(0));
4328 Tmp2 = PromoteOp(Node->getOperand(1));
4329 if (MVT::isInteger(NVT)) {
4330 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4331 DAG.getValueType(VT));
4332 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4333 DAG.getValueType(VT));
4334 }
4335 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4336
4337 // Perform FP_ROUND: this is probably overly pessimistic.
4338 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
4339 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4340 DAG.getValueType(VT));
4341 break;
4342 case ISD::FDIV:
4343 case ISD::FREM:
4344 case ISD::FCOPYSIGN:
4345 // These operators require that their input be fp extended.
4346 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004347 case Expand: assert(0 && "not implemented");
4348 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4349 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350 }
4351 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004352 case Expand: assert(0 && "not implemented");
4353 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4354 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 }
4356 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4357
4358 // Perform FP_ROUND: this is probably overly pessimistic.
4359 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4360 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4361 DAG.getValueType(VT));
4362 break;
4363
4364 case ISD::UDIV:
4365 case ISD::UREM:
4366 // These operators require that their input be zero extended.
4367 Tmp1 = PromoteOp(Node->getOperand(0));
4368 Tmp2 = PromoteOp(Node->getOperand(1));
4369 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
4370 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4371 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4372 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4373 break;
4374
4375 case ISD::SHL:
4376 Tmp1 = PromoteOp(Node->getOperand(0));
4377 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4378 break;
4379 case ISD::SRA:
4380 // The input value must be properly sign extended.
4381 Tmp1 = PromoteOp(Node->getOperand(0));
4382 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4383 DAG.getValueType(VT));
4384 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4385 break;
4386 case ISD::SRL:
4387 // The input value must be properly zero extended.
4388 Tmp1 = PromoteOp(Node->getOperand(0));
4389 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4390 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4391 break;
4392
4393 case ISD::VAARG:
4394 Tmp1 = Node->getOperand(0); // Get the chain.
4395 Tmp2 = Node->getOperand(1); // Get the pointer.
4396 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4397 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4398 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4399 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004400 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4401 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 // Increment the pointer, VAList, to the next vaarg
4403 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4404 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4405 TLI.getPointerTy()));
4406 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004407 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 // Load the actual argument out of the pointer VAList
4409 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4410 }
4411 // Remember that we legalized the chain.
4412 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4413 break;
4414
4415 case ISD::LOAD: {
4416 LoadSDNode *LD = cast<LoadSDNode>(Node);
4417 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4418 ? ISD::EXTLOAD : LD->getExtensionType();
4419 Result = DAG.getExtLoad(ExtType, NVT,
4420 LD->getChain(), LD->getBasePtr(),
4421 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004422 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 LD->isVolatile(),
4424 LD->getAlignment());
4425 // Remember that we legalized the chain.
4426 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4427 break;
4428 }
4429 case ISD::SELECT:
4430 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4431 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4432 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4433 break;
4434 case ISD::SELECT_CC:
4435 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4436 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4437 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4438 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4439 break;
4440 case ISD::BSWAP:
4441 Tmp1 = Node->getOperand(0);
4442 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4443 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4444 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4445 DAG.getConstant(MVT::getSizeInBits(NVT) -
4446 MVT::getSizeInBits(VT),
4447 TLI.getShiftAmountTy()));
4448 break;
4449 case ISD::CTPOP:
4450 case ISD::CTTZ:
4451 case ISD::CTLZ:
4452 // Zero extend the argument
4453 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4454 // Perform the larger operation, then subtract if needed.
4455 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4456 switch(Node->getOpcode()) {
4457 case ISD::CTPOP:
4458 Result = Tmp1;
4459 break;
4460 case ISD::CTTZ:
4461 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4462 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
4463 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4464 ISD::SETEQ);
4465 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4466 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4467 break;
4468 case ISD::CTLZ:
4469 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4470 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4471 DAG.getConstant(MVT::getSizeInBits(NVT) -
4472 MVT::getSizeInBits(VT), NVT));
4473 break;
4474 }
4475 break;
4476 case ISD::EXTRACT_SUBVECTOR:
4477 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4478 break;
4479 case ISD::EXTRACT_VECTOR_ELT:
4480 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4481 break;
4482 }
4483
4484 assert(Result.Val && "Didn't set a result!");
4485
4486 // Make sure the result is itself legal.
4487 Result = LegalizeOp(Result);
4488
4489 // Remember that we promoted this!
4490 AddPromotedOperand(Op, Result);
4491 return Result;
4492}
4493
4494/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4495/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4496/// based on the vector type. The return type of this matches the element type
4497/// of the vector, which may not be legal for the target.
4498SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4499 // We know that operand #0 is the Vec vector. If the index is a constant
4500 // or if the invec is a supported hardware type, we can use it. Otherwise,
4501 // lower to a store then an indexed load.
4502 SDOperand Vec = Op.getOperand(0);
4503 SDOperand Idx = Op.getOperand(1);
4504
Dan Gohmana0763d92007-09-24 15:54:53 +00004505 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506 unsigned NumElems = MVT::getVectorNumElements(TVT);
4507
4508 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4509 default: assert(0 && "This action is not supported yet!");
4510 case TargetLowering::Custom: {
4511 Vec = LegalizeOp(Vec);
4512 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4513 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4514 if (Tmp3.Val)
4515 return Tmp3;
4516 break;
4517 }
4518 case TargetLowering::Legal:
4519 if (isTypeLegal(TVT)) {
4520 Vec = LegalizeOp(Vec);
4521 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004522 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523 }
4524 break;
4525 case TargetLowering::Expand:
4526 break;
4527 }
4528
4529 if (NumElems == 1) {
4530 // This must be an access of the only element. Return it.
4531 Op = ScalarizeVectorOp(Vec);
4532 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004533 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4535 SDOperand Lo, Hi;
4536 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004537 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538 Vec = Lo;
4539 } else {
4540 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004541 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542 Idx.getValueType());
4543 }
4544
4545 // It's now an extract from the appropriate high or low part. Recurse.
4546 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4547 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4548 } else {
4549 // Store the value to a temporary stack slot, then LOAD the scalar
4550 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004551 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4553
4554 // Add the offset to the index.
4555 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4556 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4557 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004558
4559 if (MVT::getSizeInBits(Idx.getValueType()) >
4560 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004561 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004562 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004563 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4566
4567 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4568 }
4569 return Op;
4570}
4571
4572/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4573/// we assume the operation can be split if it is not already legal.
4574SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4575 // We know that operand #0 is the Vec vector. For now we assume the index
4576 // is a constant and that the extracted result is a supported hardware type.
4577 SDOperand Vec = Op.getOperand(0);
4578 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4579
4580 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4581
4582 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4583 // This must be an access of the desired vector length. Return it.
4584 return Vec;
4585 }
4586
4587 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4588 SDOperand Lo, Hi;
4589 SplitVectorOp(Vec, Lo, Hi);
4590 if (CIdx->getValue() < NumElems/2) {
4591 Vec = Lo;
4592 } else {
4593 Vec = Hi;
4594 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4595 }
4596
4597 // It's now an extract from the appropriate high or low part. Recurse.
4598 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4599 return ExpandEXTRACT_SUBVECTOR(Op);
4600}
4601
4602/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4603/// with condition CC on the current target. This usually involves legalizing
4604/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4605/// there may be no choice but to create a new SetCC node to represent the
4606/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4607/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4608void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4609 SDOperand &RHS,
4610 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004611 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612
4613 switch (getTypeAction(LHS.getValueType())) {
4614 case Legal:
4615 Tmp1 = LegalizeOp(LHS); // LHS
4616 Tmp2 = LegalizeOp(RHS); // RHS
4617 break;
4618 case Promote:
4619 Tmp1 = PromoteOp(LHS); // LHS
4620 Tmp2 = PromoteOp(RHS); // RHS
4621
4622 // If this is an FP compare, the operands have already been extended.
4623 if (MVT::isInteger(LHS.getValueType())) {
4624 MVT::ValueType VT = LHS.getValueType();
4625 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4626
4627 // Otherwise, we have to insert explicit sign or zero extends. Note
4628 // that we could insert sign extends for ALL conditions, but zero extend
4629 // is cheaper on many machines (an AND instead of two shifts), so prefer
4630 // it.
4631 switch (cast<CondCodeSDNode>(CC)->get()) {
4632 default: assert(0 && "Unknown integer comparison!");
4633 case ISD::SETEQ:
4634 case ISD::SETNE:
4635 case ISD::SETUGE:
4636 case ISD::SETUGT:
4637 case ISD::SETULE:
4638 case ISD::SETULT:
4639 // ALL of these operations will work if we either sign or zero extend
4640 // the operands (including the unsigned comparisons!). Zero extend is
4641 // usually a simpler/cheaper operation, so prefer it.
4642 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4643 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4644 break;
4645 case ISD::SETGE:
4646 case ISD::SETGT:
4647 case ISD::SETLT:
4648 case ISD::SETLE:
4649 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4650 DAG.getValueType(VT));
4651 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4652 DAG.getValueType(VT));
4653 break;
4654 }
4655 }
4656 break;
4657 case Expand: {
4658 MVT::ValueType VT = LHS.getValueType();
4659 if (VT == MVT::f32 || VT == MVT::f64) {
4660 // Expand into one or more soft-fp libcall(s).
4661 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4662 switch (cast<CondCodeSDNode>(CC)->get()) {
4663 case ISD::SETEQ:
4664 case ISD::SETOEQ:
4665 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4666 break;
4667 case ISD::SETNE:
4668 case ISD::SETUNE:
4669 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4670 break;
4671 case ISD::SETGE:
4672 case ISD::SETOGE:
4673 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4674 break;
4675 case ISD::SETLT:
4676 case ISD::SETOLT:
4677 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4678 break;
4679 case ISD::SETLE:
4680 case ISD::SETOLE:
4681 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4682 break;
4683 case ISD::SETGT:
4684 case ISD::SETOGT:
4685 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4686 break;
4687 case ISD::SETUO:
4688 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4689 break;
4690 case ISD::SETO:
4691 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4692 break;
4693 default:
4694 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4695 switch (cast<CondCodeSDNode>(CC)->get()) {
4696 case ISD::SETONE:
4697 // SETONE = SETOLT | SETOGT
4698 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4699 // Fallthrough
4700 case ISD::SETUGT:
4701 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4702 break;
4703 case ISD::SETUGE:
4704 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4705 break;
4706 case ISD::SETULT:
4707 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4708 break;
4709 case ISD::SETULE:
4710 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4711 break;
4712 case ISD::SETUEQ:
4713 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4714 break;
4715 default: assert(0 && "Unsupported FP setcc!");
4716 }
4717 }
4718
4719 SDOperand Dummy;
4720 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
4721 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4722 false /*sign irrelevant*/, Dummy);
4723 Tmp2 = DAG.getConstant(0, MVT::i32);
4724 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4725 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
4726 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
4727 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
4728 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4729 false /*sign irrelevant*/, Dummy);
4730 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4731 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4732 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4733 Tmp2 = SDOperand();
4734 }
4735 LHS = Tmp1;
4736 RHS = Tmp2;
4737 return;
4738 }
4739
4740 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4741 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004742 ExpandOp(RHS, RHSLo, RHSHi);
4743 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4744
4745 if (VT==MVT::ppcf128) {
4746 // FIXME: This generated code sucks. We want to generate
4747 // FCMP crN, hi1, hi2
4748 // BNE crN, L:
4749 // FCMP crN, lo1, lo2
4750 // The following can be improved, but not that much.
4751 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4752 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4753 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4754 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4755 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4756 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4757 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4758 Tmp2 = SDOperand();
4759 break;
4760 }
4761
4762 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763 case ISD::SETEQ:
4764 case ISD::SETNE:
4765 if (RHSLo == RHSHi)
4766 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4767 if (RHSCST->isAllOnesValue()) {
4768 // Comparison to -1.
4769 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4770 Tmp2 = RHSLo;
4771 break;
4772 }
4773
4774 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4775 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4776 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4777 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4778 break;
4779 default:
4780 // If this is a comparison of the sign bit, just look at the top part.
4781 // X > -1, x < 0
4782 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4783 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4784 CST->getValue() == 0) || // X < 0
4785 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4786 CST->isAllOnesValue())) { // X > -1
4787 Tmp1 = LHSHi;
4788 Tmp2 = RHSHi;
4789 break;
4790 }
4791
4792 // FIXME: This generated code sucks.
4793 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 switch (CCCode) {
4795 default: assert(0 && "Unknown integer setcc!");
4796 case ISD::SETLT:
4797 case ISD::SETULT: LowCC = ISD::SETULT; break;
4798 case ISD::SETGT:
4799 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4800 case ISD::SETLE:
4801 case ISD::SETULE: LowCC = ISD::SETULE; break;
4802 case ISD::SETGE:
4803 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4804 }
4805
4806 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4807 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4808 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4809
4810 // NOTE: on targets without efficient SELECT of bools, we can always use
4811 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4812 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4813 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4814 false, DagCombineInfo);
4815 if (!Tmp1.Val)
4816 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4817 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4818 CCCode, false, DagCombineInfo);
4819 if (!Tmp2.Val)
Chris Lattner6fb53da2007-10-15 17:48:57 +00004820 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004821
4822 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4823 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4824 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4825 (Tmp2C && Tmp2C->getValue() == 0 &&
4826 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4827 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4828 (Tmp2C && Tmp2C->getValue() == 1 &&
4829 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4830 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4831 // low part is known false, returns high part.
4832 // For LE / GE, if high part is known false, ignore the low part.
4833 // For LT / GT, if high part is known true, ignore the low part.
4834 Tmp1 = Tmp2;
4835 Tmp2 = SDOperand();
4836 } else {
4837 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4838 ISD::SETEQ, false, DagCombineInfo);
4839 if (!Result.Val)
4840 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4841 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4842 Result, Tmp1, Tmp2));
4843 Tmp1 = Result;
4844 Tmp2 = SDOperand();
4845 }
4846 }
4847 }
4848 }
4849 LHS = Tmp1;
4850 RHS = Tmp2;
4851}
4852
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004853/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4854/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4855/// a load from the stack slot to DestVT, extending it if needed.
4856/// The resultant code need not be legal.
4857SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4858 MVT::ValueType SlotVT,
4859 MVT::ValueType DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004860 // Create the stack frame object.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004861 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4862
Dan Gohman20e37962008-02-11 18:58:42 +00004863 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004864 int SPFI = StackPtrFI->getIndex();
4865
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004866 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4867 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4868 unsigned DestSize = MVT::getSizeInBits(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004869
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004870 // Emit a store to the stack slot. Use a truncstore if the input value is
4871 // later than DestVT.
4872 SDOperand Store;
4873 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004874 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004875 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004876 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004877 else {
4878 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004879 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004880 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004881 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004882 }
4883
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004885 if (SlotSize == DestSize)
4886 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4887
4888 assert(SlotSize < DestSize && "Unknown extension!");
4889 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890}
4891
4892SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4893 // Create a vector sized/aligned stack slot, store the value to element #0,
4894 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004895 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00004896
Dan Gohman20e37962008-02-11 18:58:42 +00004897 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004898 int SPFI = StackPtrFI->getIndex();
4899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004901 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman12a9c082008-02-06 22:27:42 +00004902 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004903 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904}
4905
4906
4907/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4908/// support the operation, but do support the resultant vector type.
4909SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4910
4911 // If the only non-undef value is the low element, turn this into a
4912 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4913 unsigned NumElems = Node->getNumOperands();
4914 bool isOnlyLowElement = true;
4915 SDOperand SplatValue = Node->getOperand(0);
4916 std::map<SDOperand, std::vector<unsigned> > Values;
4917 Values[SplatValue].push_back(0);
4918 bool isConstant = true;
4919 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4920 SplatValue.getOpcode() != ISD::UNDEF)
4921 isConstant = false;
4922
4923 for (unsigned i = 1; i < NumElems; ++i) {
4924 SDOperand V = Node->getOperand(i);
4925 Values[V].push_back(i);
4926 if (V.getOpcode() != ISD::UNDEF)
4927 isOnlyLowElement = false;
4928 if (SplatValue != V)
4929 SplatValue = SDOperand(0,0);
4930
4931 // If this isn't a constant element or an undef, we can't use a constant
4932 // pool load.
4933 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4934 V.getOpcode() != ISD::UNDEF)
4935 isConstant = false;
4936 }
4937
4938 if (isOnlyLowElement) {
4939 // If the low element is an undef too, then this whole things is an undef.
4940 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4941 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4942 // Otherwise, turn this into a scalar_to_vector node.
4943 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4944 Node->getOperand(0));
4945 }
4946
4947 // If all elements are constants, create a load from the constant pool.
4948 if (isConstant) {
4949 MVT::ValueType VT = Node->getValueType(0);
4950 const Type *OpNTy =
4951 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4952 std::vector<Constant*> CV;
4953 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4954 if (ConstantFPSDNode *V =
4955 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00004956 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 } else if (ConstantSDNode *V =
4958 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4959 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4960 } else {
4961 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4962 CV.push_back(UndefValue::get(OpNTy));
4963 }
4964 }
4965 Constant *CP = ConstantVector::get(CV);
4966 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004967 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004968 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969 }
4970
4971 if (SplatValue.Val) { // Splat of one value?
4972 // Build the shuffle constant vector: <0, 0, 0, 0>
4973 MVT::ValueType MaskVT =
4974 MVT::getIntVectorWithNumElements(NumElems);
4975 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4976 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4977 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4978 &ZeroVec[0], ZeroVec.size());
4979
4980 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4981 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4982 // Get the splatted value into the low element of a vector register.
4983 SDOperand LowValVec =
4984 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4985
4986 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4987 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4988 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4989 SplatMask);
4990 }
4991 }
4992
4993 // If there are only two unique elements, we may be able to turn this into a
4994 // vector shuffle.
4995 if (Values.size() == 2) {
4996 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4997 MVT::ValueType MaskVT =
4998 MVT::getIntVectorWithNumElements(NumElems);
4999 std::vector<SDOperand> MaskVec(NumElems);
5000 unsigned i = 0;
5001 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5002 E = Values.end(); I != E; ++I) {
5003 for (std::vector<unsigned>::iterator II = I->second.begin(),
5004 EE = I->second.end(); II != EE; ++II)
5005 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
5006 i += NumElems;
5007 }
5008 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5009 &MaskVec[0], MaskVec.size());
5010
5011 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
5012 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5013 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
5014 SmallVector<SDOperand, 8> Ops;
5015 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5016 E = Values.end(); I != E; ++I) {
5017 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5018 I->first);
5019 Ops.push_back(Op);
5020 }
5021 Ops.push_back(ShuffleMask);
5022
5023 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
5024 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5025 &Ops[0], Ops.size());
5026 }
5027 }
5028
5029 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5030 // aligned object on the stack, store each element into it, then load
5031 // the result as a vector.
5032 MVT::ValueType VT = Node->getValueType(0);
5033 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00005034 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005035
5036 // Emit a store of each element to the stack slot.
5037 SmallVector<SDOperand, 8> Stores;
5038 unsigned TypeByteSize =
5039 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
5040 // Store (in the right endianness) the elements to memory.
5041 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5042 // Ignore undef elements.
5043 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5044
5045 unsigned Offset = TypeByteSize*i;
5046
5047 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5048 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5049
5050 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5051 NULL, 0));
5052 }
5053
5054 SDOperand StoreChain;
5055 if (!Stores.empty()) // Not all undef elements?
5056 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5057 &Stores[0], Stores.size());
5058 else
5059 StoreChain = DAG.getEntryNode();
5060
5061 // Result is a load from the stack slot.
5062 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5063}
5064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5066 SDOperand Op, SDOperand Amt,
5067 SDOperand &Lo, SDOperand &Hi) {
5068 // Expand the subcomponents.
5069 SDOperand LHSL, LHSH;
5070 ExpandOp(Op, LHSL, LHSH);
5071
5072 SDOperand Ops[] = { LHSL, LHSH, Amt };
5073 MVT::ValueType VT = LHSL.getValueType();
5074 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5075 Hi = Lo.getValue(1);
5076}
5077
5078
5079/// ExpandShift - Try to find a clever way to expand this shift operation out to
5080/// smaller elements. If we can't find a way that is more efficient than a
5081/// libcall on this target, return false. Otherwise, return true with the
5082/// low-parts expanded into Lo and Hi.
5083bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5084 SDOperand &Lo, SDOperand &Hi) {
5085 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5086 "This is not a shift!");
5087
5088 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
5089 SDOperand ShAmt = LegalizeOp(Amt);
5090 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohmanece0a882008-02-20 16:57:27 +00005091 unsigned ShBits = MVT::getSizeInBits(ShTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5093 unsigned NVTBits = MVT::getSizeInBits(NVT);
5094
Chris Lattner8c931452007-10-14 20:35:12 +00005095 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5097 unsigned Cst = CN->getValue();
5098 // Expand the incoming operand to be shifted, so that we have its parts
5099 SDOperand InL, InH;
5100 ExpandOp(Op, InL, InH);
5101 switch(Opc) {
5102 case ISD::SHL:
5103 if (Cst > VTBits) {
5104 Lo = DAG.getConstant(0, NVT);
5105 Hi = DAG.getConstant(0, NVT);
5106 } else if (Cst > NVTBits) {
5107 Lo = DAG.getConstant(0, NVT);
5108 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5109 } else if (Cst == NVTBits) {
5110 Lo = DAG.getConstant(0, NVT);
5111 Hi = InL;
5112 } else {
5113 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5114 Hi = DAG.getNode(ISD::OR, NVT,
5115 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5116 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5117 }
5118 return true;
5119 case ISD::SRL:
5120 if (Cst > VTBits) {
5121 Lo = DAG.getConstant(0, NVT);
5122 Hi = DAG.getConstant(0, NVT);
5123 } else if (Cst > NVTBits) {
5124 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5125 Hi = DAG.getConstant(0, NVT);
5126 } else if (Cst == NVTBits) {
5127 Lo = InH;
5128 Hi = DAG.getConstant(0, NVT);
5129 } else {
5130 Lo = DAG.getNode(ISD::OR, NVT,
5131 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5132 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5133 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5134 }
5135 return true;
5136 case ISD::SRA:
5137 if (Cst > VTBits) {
5138 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5139 DAG.getConstant(NVTBits-1, ShTy));
5140 } else if (Cst > NVTBits) {
5141 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5142 DAG.getConstant(Cst-NVTBits, ShTy));
5143 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5144 DAG.getConstant(NVTBits-1, ShTy));
5145 } else if (Cst == NVTBits) {
5146 Lo = InH;
5147 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5148 DAG.getConstant(NVTBits-1, ShTy));
5149 } else {
5150 Lo = DAG.getNode(ISD::OR, NVT,
5151 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5152 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5153 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5154 }
5155 return true;
5156 }
5157 }
5158
5159 // Okay, the shift amount isn't constant. However, if we can tell that it is
5160 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005161 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5162 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5164
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005165 // If we know that if any of the high bits of the shift amount are one, then
5166 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005167 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005168 // Mask out the high bit, which we know is set.
5169 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005170 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171
5172 // Expand the incoming operand to be shifted, so that we have its parts
5173 SDOperand InL, InH;
5174 ExpandOp(Op, InL, InH);
5175 switch(Opc) {
5176 case ISD::SHL:
5177 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5178 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5179 return true;
5180 case ISD::SRL:
5181 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5182 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5183 return true;
5184 case ISD::SRA:
5185 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5186 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5187 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5188 return true;
5189 }
5190 }
5191
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005192 // If we know that the high bits of the shift amount are all zero, then we can
5193 // do this as a couple of simple shifts.
5194 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005195 // Compute 32-amt.
5196 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5197 DAG.getConstant(NVTBits, Amt.getValueType()),
5198 Amt);
5199
5200 // Expand the incoming operand to be shifted, so that we have its parts
5201 SDOperand InL, InH;
5202 ExpandOp(Op, InL, InH);
5203 switch(Opc) {
5204 case ISD::SHL:
5205 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5206 Hi = DAG.getNode(ISD::OR, NVT,
5207 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5208 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5209 return true;
5210 case ISD::SRL:
5211 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5212 Lo = DAG.getNode(ISD::OR, NVT,
5213 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5214 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5215 return true;
5216 case ISD::SRA:
5217 Hi = DAG.getNode(ISD::SRA, 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 }
5223 }
5224
5225 return false;
5226}
5227
5228
5229// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5230// does not fit into a register, return the lo part and set the hi part to the
5231// by-reg argument. If it does fit into a single register, return the result
5232// and leave the Hi part unset.
5233SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
5234 bool isSigned, SDOperand &Hi) {
5235 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5236 // The input chain to this libcall is the entry node of the function.
5237 // Legalizing the call will automatically add the previous call to the
5238 // dependence.
5239 SDOperand InChain = DAG.getEntryNode();
5240
5241 TargetLowering::ArgListTy Args;
5242 TargetLowering::ArgListEntry Entry;
5243 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5244 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5245 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
5246 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5247 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005248 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005249 Args.push_back(Entry);
5250 }
5251 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
5252
5253 // Splice the libcall in wherever FindInputOutputChains tells us to.
5254 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
5255 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005256 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5257 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005258
5259 // Legalize the call sequence, starting with the chain. This will advance
5260 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5261 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5262 LegalizeOp(CallInfo.second);
5263 SDOperand Result;
5264 switch (getTypeAction(CallInfo.first.getValueType())) {
5265 default: assert(0 && "Unknown thing");
5266 case Legal:
5267 Result = CallInfo.first;
5268 break;
5269 case Expand:
5270 ExpandOp(CallInfo.first, Result, Hi);
5271 break;
5272 }
5273 return Result;
5274}
5275
5276
5277/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5278///
5279SDOperand SelectionDAGLegalize::
5280ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmanc98645c2008-03-05 01:08:17 +00005281 MVT::ValueType SourceVT = Source.getValueType();
5282 assert(getTypeAction(SourceVT) == Expand &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283 "This is not an expansion!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284
5285 if (!isSigned) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005286 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 // incoming integer is set. To handle this, we dynamically test to see if
5288 // it is set, and, if so, add a fudge factor.
5289 SDOperand Lo, Hi;
5290 ExpandOp(Source, Lo, Hi);
5291
5292 // If this is unsigned, and not supported, first perform the conversion to
5293 // signed, then adjust the result if the sign bit is set.
5294 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
Dan Gohmanc98645c2008-03-05 01:08:17 +00005295 DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296
5297 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5298 DAG.getConstant(0, Hi.getValueType()),
5299 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005300 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5302 SignSet, Four, Zero);
5303 uint64_t FF = 0x5f800000ULL;
5304 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005305 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306
5307 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5308 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5309 SDOperand FudgeInReg;
5310 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005311 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005312 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005313 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005315 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005316 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005317 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005318 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005319 else
5320 assert(0 && "Unexpected conversion");
5321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322 MVT::ValueType SCVT = SignedConv.getValueType();
5323 if (SCVT != DestTy) {
5324 // Destination type needs to be expanded as well. The FADD now we are
5325 // constructing will be expanded into a libcall.
5326 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmanc98645c2008-03-05 01:08:17 +00005327 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5328 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 SignedConv, SignedConv.getValue(1));
5330 }
5331 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5332 }
5333 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5334 }
5335
5336 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005337 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 default: assert(0 && "This action not implemented for this operation!");
5339 case TargetLowering::Legal:
5340 case TargetLowering::Expand:
5341 break; // This case is handled below.
5342 case TargetLowering::Custom: {
5343 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5344 Source), DAG);
5345 if (NV.Val)
5346 return LegalizeOp(NV);
5347 break; // The target decided this was legal after all
5348 }
5349 }
5350
5351 // Expand the source, then glue it back together for the call. We must expand
5352 // the source in case it is shared (this pass of legalize must traverse it).
5353 SDOperand SrcLo, SrcHi;
5354 ExpandOp(Source, SrcLo, SrcHi);
Dan Gohmanc98645c2008-03-05 01:08:17 +00005355 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356
5357 RTLIB::Libcall LC;
Dan Gohmanc98645c2008-03-05 01:08:17 +00005358 if (SourceVT == MVT::i64) {
5359 if (DestTy == MVT::f32)
5360 LC = RTLIB::SINTTOFP_I64_F32;
5361 else {
5362 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5363 LC = RTLIB::SINTTOFP_I64_F64;
5364 }
5365 } else if (SourceVT == MVT::i128) {
5366 if (DestTy == MVT::f32)
5367 LC = RTLIB::SINTTOFP_I128_F32;
5368 else if (DestTy == MVT::f64)
5369 LC = RTLIB::SINTTOFP_I128_F64;
5370 else if (DestTy == MVT::f80)
5371 LC = RTLIB::SINTTOFP_I128_F80;
5372 else {
5373 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5374 LC = RTLIB::SINTTOFP_I128_PPCF128;
5375 }
5376 } else {
5377 assert(0 && "Unknown int value type");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378 }
5379
5380 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
5381 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5382 SDOperand UnusedHiPart;
5383 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5384 UnusedHiPart);
5385}
5386
5387/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5388/// INT_TO_FP operation of the specified operand when the target requests that
5389/// we expand it. At this point, we know that the result and operand types are
5390/// legal for the target.
5391SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5392 SDOperand Op0,
5393 MVT::ValueType DestVT) {
5394 if (Op0.getValueType() == MVT::i32) {
5395 // simple 32-bit [signed|unsigned] integer to float/double expansion
5396
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005397 // Get the stack frame index of a 8 byte buffer.
5398 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5399
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400 // word offset constant for Hi/Lo address computation
5401 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5402 // set up Hi and Lo (into buffer) address based on endian
5403 SDOperand Hi = StackSlot;
5404 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5405 if (TLI.isLittleEndian())
5406 std::swap(Hi, Lo);
5407
5408 // if signed map to unsigned space
5409 SDOperand Op0Mapped;
5410 if (isSigned) {
5411 // constant used to invert sign bit (signed to unsigned mapping)
5412 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5413 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5414 } else {
5415 Op0Mapped = Op0;
5416 }
5417 // store the lo of the constructed double - based on integer input
5418 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5419 Op0Mapped, Lo, NULL, 0);
5420 // initial hi portion of constructed double
5421 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5422 // store the hi of the constructed double - biased exponent
5423 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5424 // load the constructed double
5425 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5426 // FP constant to bias correct the final result
5427 SDOperand Bias = DAG.getConstantFP(isSigned ?
5428 BitsToDouble(0x4330000080000000ULL)
5429 : BitsToDouble(0x4330000000000000ULL),
5430 MVT::f64);
5431 // subtract the bias
5432 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5433 // final result
5434 SDOperand Result;
5435 // handle final rounding
5436 if (DestVT == MVT::f64) {
5437 // do nothing
5438 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005439 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005440 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5441 DAG.getIntPtrConstant(0));
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005442 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5443 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005444 }
5445 return Result;
5446 }
5447 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5448 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5449
5450 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5451 DAG.getConstant(0, Op0.getValueType()),
5452 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005453 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005454 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5455 SignSet, Four, Zero);
5456
5457 // If the sign bit of the integer is set, the large number will be treated
5458 // as a negative number. To counteract this, the dynamic code adds an
5459 // offset depending on the data type.
5460 uint64_t FF;
5461 switch (Op0.getValueType()) {
5462 default: assert(0 && "Unsupported integer type!");
5463 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5464 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5465 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5466 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5467 }
5468 if (TLI.isLittleEndian()) FF <<= 32;
5469 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5470
5471 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5472 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5473 SDOperand FudgeInReg;
5474 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005475 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005476 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005478 FudgeInReg =
5479 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5480 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005481 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005482 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 }
5484
5485 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5486}
5487
5488/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5489/// *INT_TO_FP operation of the specified operand when the target requests that
5490/// we promote it. At this point, we know that the result and operand types are
5491/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5492/// operation that takes a larger input.
5493SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5494 MVT::ValueType DestVT,
5495 bool isSigned) {
5496 // First step, figure out the appropriate *INT_TO_FP operation to use.
5497 MVT::ValueType NewInTy = LegalOp.getValueType();
5498
5499 unsigned OpToUse = 0;
5500
5501 // Scan for the appropriate larger type to use.
5502 while (1) {
5503 NewInTy = (MVT::ValueType)(NewInTy+1);
5504 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5505
5506 // If the target supports SINT_TO_FP of this type, use it.
5507 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5508 default: break;
5509 case TargetLowering::Legal:
5510 if (!TLI.isTypeLegal(NewInTy))
5511 break; // Can't use this datatype.
5512 // FALL THROUGH.
5513 case TargetLowering::Custom:
5514 OpToUse = ISD::SINT_TO_FP;
5515 break;
5516 }
5517 if (OpToUse) break;
5518 if (isSigned) continue;
5519
5520 // If the target supports UINT_TO_FP of this type, use it.
5521 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5522 default: break;
5523 case TargetLowering::Legal:
5524 if (!TLI.isTypeLegal(NewInTy))
5525 break; // Can't use this datatype.
5526 // FALL THROUGH.
5527 case TargetLowering::Custom:
5528 OpToUse = ISD::UINT_TO_FP;
5529 break;
5530 }
5531 if (OpToUse) break;
5532
5533 // Otherwise, try a larger type.
5534 }
5535
5536 // Okay, we found the operation and type to use. Zero extend our input to the
5537 // desired type then run the operation on it.
5538 return DAG.getNode(OpToUse, DestVT,
5539 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5540 NewInTy, LegalOp));
5541}
5542
5543/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5544/// FP_TO_*INT operation of the specified operand when the target requests that
5545/// we promote it. At this point, we know that the result and operand types are
5546/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5547/// operation that returns a larger result.
5548SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5549 MVT::ValueType DestVT,
5550 bool isSigned) {
5551 // First step, figure out the appropriate FP_TO*INT operation to use.
5552 MVT::ValueType NewOutTy = DestVT;
5553
5554 unsigned OpToUse = 0;
5555
5556 // Scan for the appropriate larger type to use.
5557 while (1) {
5558 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5559 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5560
5561 // If the target supports FP_TO_SINT returning this type, use it.
5562 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5563 default: break;
5564 case TargetLowering::Legal:
5565 if (!TLI.isTypeLegal(NewOutTy))
5566 break; // Can't use this datatype.
5567 // FALL THROUGH.
5568 case TargetLowering::Custom:
5569 OpToUse = ISD::FP_TO_SINT;
5570 break;
5571 }
5572 if (OpToUse) break;
5573
5574 // If the target supports FP_TO_UINT of this type, use it.
5575 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5576 default: break;
5577 case TargetLowering::Legal:
5578 if (!TLI.isTypeLegal(NewOutTy))
5579 break; // Can't use this datatype.
5580 // FALL THROUGH.
5581 case TargetLowering::Custom:
5582 OpToUse = ISD::FP_TO_UINT;
5583 break;
5584 }
5585 if (OpToUse) break;
5586
5587 // Otherwise, try a larger type.
5588 }
5589
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005590
5591 // Okay, we found the operation and type to use.
5592 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5593
5594 // If the operation produces an invalid type, it must be custom lowered. Use
5595 // the target lowering hooks to expand it. Just keep the low part of the
5596 // expanded operation, we know that we're truncating anyway.
5597 if (getTypeAction(NewOutTy) == Expand) {
5598 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5599 assert(Operation.Val && "Didn't return anything");
5600 }
5601
5602 // Truncate the result of the extended FP_TO_*INT operation to the desired
5603 // size.
5604 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605}
5606
5607/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5608///
5609SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5610 MVT::ValueType VT = Op.getValueType();
5611 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5612 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5613 switch (VT) {
5614 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5615 case MVT::i16:
5616 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5617 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5618 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5619 case MVT::i32:
5620 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5621 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5622 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5623 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5624 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5625 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5626 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5627 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5628 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5629 case MVT::i64:
5630 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5631 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5632 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5633 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5634 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5635 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5636 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5637 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5638 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5639 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5640 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5641 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5642 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5643 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5644 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5645 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5646 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5647 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5648 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5649 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5650 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5651 }
5652}
5653
5654/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5655///
5656SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5657 switch (Opc) {
5658 default: assert(0 && "Cannot expand this yet!");
5659 case ISD::CTPOP: {
5660 static const uint64_t mask[6] = {
5661 0x5555555555555555ULL, 0x3333333333333333ULL,
5662 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5663 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5664 };
5665 MVT::ValueType VT = Op.getValueType();
5666 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5667 unsigned len = MVT::getSizeInBits(VT);
5668 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5669 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5670 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5671 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5672 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5673 DAG.getNode(ISD::AND, VT,
5674 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5675 }
5676 return Op;
5677 }
5678 case ISD::CTLZ: {
5679 // for now, we do this:
5680 // x = x | (x >> 1);
5681 // x = x | (x >> 2);
5682 // ...
5683 // x = x | (x >>16);
5684 // x = x | (x >>32); // for 64-bit input
5685 // return popcount(~x);
5686 //
5687 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5688 MVT::ValueType VT = Op.getValueType();
5689 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5690 unsigned len = MVT::getSizeInBits(VT);
5691 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5692 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5693 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5694 }
5695 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5696 return DAG.getNode(ISD::CTPOP, VT, Op);
5697 }
5698 case ISD::CTTZ: {
5699 // for now, we use: { return popcount(~x & (x - 1)); }
5700 // unless the target has ctlz but not ctpop, in which case we use:
5701 // { return 32 - nlz(~x & (x-1)); }
5702 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5703 MVT::ValueType VT = Op.getValueType();
5704 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5705 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5706 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5707 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5708 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5709 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5710 TLI.isOperationLegal(ISD::CTLZ, VT))
5711 return DAG.getNode(ISD::SUB, VT,
5712 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5713 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5714 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5715 }
5716 }
5717}
5718
5719/// ExpandOp - Expand the specified SDOperand into its two component pieces
5720/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5721/// LegalizeNodes map is filled in for any results that are not expanded, the
5722/// ExpandedNodes map is filled in for any results that are expanded, and the
5723/// Lo/Hi values are returned.
5724void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5725 MVT::ValueType VT = Op.getValueType();
5726 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5727 SDNode *Node = Op.Val;
5728 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5729 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5730 MVT::isVector(VT)) &&
5731 "Cannot expand to FP value or to larger int value!");
5732
5733 // See if we already expanded it.
5734 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5735 = ExpandedNodes.find(Op);
5736 if (I != ExpandedNodes.end()) {
5737 Lo = I->second.first;
5738 Hi = I->second.second;
5739 return;
5740 }
5741
5742 switch (Node->getOpcode()) {
5743 case ISD::CopyFromReg:
5744 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005745 case ISD::FP_ROUND_INREG:
5746 if (VT == MVT::ppcf128 &&
5747 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5748 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005749 SDOperand SrcLo, SrcHi, Src;
5750 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5751 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5752 SDOperand Result = TLI.LowerOperation(
5753 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005754 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5755 Lo = Result.Val->getOperand(0);
5756 Hi = Result.Val->getOperand(1);
5757 break;
5758 }
5759 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 default:
5761#ifndef NDEBUG
5762 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5763#endif
5764 assert(0 && "Do not know how to expand this operator!");
5765 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00005766 case ISD::EXTRACT_ELEMENT:
5767 ExpandOp(Node->getOperand(0), Lo, Hi);
5768 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5769 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00005770 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005771 case ISD::EXTRACT_VECTOR_ELT:
5772 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5773 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5774 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5775 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776 case ISD::UNDEF:
5777 NVT = TLI.getTypeToExpandTo(VT);
5778 Lo = DAG.getNode(ISD::UNDEF, NVT);
5779 Hi = DAG.getNode(ISD::UNDEF, NVT);
5780 break;
5781 case ISD::Constant: {
Dan Gohman97f1f8e2008-03-03 22:20:46 +00005782 unsigned NVTBits = MVT::getSizeInBits(NVT);
5783 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5784 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5785 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 break;
5787 }
5788 case ISD::ConstantFP: {
5789 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005790 if (CFP->getValueType(0) == MVT::ppcf128) {
5791 APInt api = CFP->getValueAPF().convertToAPInt();
5792 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5793 MVT::f64);
5794 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5795 MVT::f64);
5796 break;
5797 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005798 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5799 if (getTypeAction(Lo.getValueType()) == Expand)
5800 ExpandOp(Lo, Lo, Hi);
5801 break;
5802 }
5803 case ISD::BUILD_PAIR:
5804 // Return the operands.
5805 Lo = Node->getOperand(0);
5806 Hi = Node->getOperand(1);
5807 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005808
5809 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005810 if (Node->getNumValues() == 1) {
5811 ExpandOp(Op.getOperand(0), Lo, Hi);
5812 break;
5813 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005814 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5815 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5816 Op.getValue(1).getValueType() == MVT::Other &&
5817 "unhandled MERGE_VALUES");
5818 ExpandOp(Op.getOperand(0), Lo, Hi);
5819 // Remember that we legalized the chain.
5820 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5821 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822
5823 case ISD::SIGN_EXTEND_INREG:
5824 ExpandOp(Node->getOperand(0), Lo, Hi);
5825 // sext_inreg the low part if needed.
5826 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5827
5828 // The high part gets the sign extension from the lo-part. This handles
5829 // things like sextinreg V:i64 from i8.
5830 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5831 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5832 TLI.getShiftAmountTy()));
5833 break;
5834
5835 case ISD::BSWAP: {
5836 ExpandOp(Node->getOperand(0), Lo, Hi);
5837 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5838 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5839 Lo = TempLo;
5840 break;
5841 }
5842
5843 case ISD::CTPOP:
5844 ExpandOp(Node->getOperand(0), Lo, Hi);
5845 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5846 DAG.getNode(ISD::CTPOP, NVT, Lo),
5847 DAG.getNode(ISD::CTPOP, NVT, Hi));
5848 Hi = DAG.getConstant(0, NVT);
5849 break;
5850
5851 case ISD::CTLZ: {
5852 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5853 ExpandOp(Node->getOperand(0), Lo, Hi);
5854 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5855 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5856 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5857 ISD::SETNE);
5858 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5859 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5860
5861 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5862 Hi = DAG.getConstant(0, NVT);
5863 break;
5864 }
5865
5866 case ISD::CTTZ: {
5867 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5868 ExpandOp(Node->getOperand(0), Lo, Hi);
5869 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5870 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5871 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5872 ISD::SETNE);
5873 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5874 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5875
5876 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5877 Hi = DAG.getConstant(0, NVT);
5878 break;
5879 }
5880
5881 case ISD::VAARG: {
5882 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5883 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5884 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5885 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5886
5887 // Remember that we legalized the chain.
5888 Hi = LegalizeOp(Hi);
5889 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005890 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005891 std::swap(Lo, Hi);
5892 break;
5893 }
5894
5895 case ISD::LOAD: {
5896 LoadSDNode *LD = cast<LoadSDNode>(Node);
5897 SDOperand Ch = LD->getChain(); // Legalize the chain.
5898 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5899 ISD::LoadExtType ExtType = LD->getExtensionType();
5900 int SVOffset = LD->getSrcValueOffset();
5901 unsigned Alignment = LD->getAlignment();
5902 bool isVolatile = LD->isVolatile();
5903
5904 if (ExtType == ISD::NON_EXTLOAD) {
5905 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5906 isVolatile, Alignment);
5907 if (VT == MVT::f32 || VT == MVT::f64) {
5908 // f32->i32 or f64->i64 one to one expansion.
5909 // Remember that we legalized the chain.
5910 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5911 // Recursively expand the new load.
5912 if (getTypeAction(NVT) == Expand)
5913 ExpandOp(Lo, Lo, Hi);
5914 break;
5915 }
5916
5917 // Increment the pointer to the other half.
5918 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5919 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005920 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005921 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005922 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5924 isVolatile, Alignment);
5925
5926 // Build a factor node to remember that this load is independent of the
5927 // other one.
5928 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5929 Hi.getValue(1));
5930
5931 // Remember that we legalized the chain.
5932 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005933 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005934 std::swap(Lo, Hi);
5935 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005936 MVT::ValueType EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005938 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5939 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005940 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5941 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5942 SVOffset, isVolatile, Alignment);
5943 // Remember that we legalized the chain.
5944 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5945 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5946 break;
5947 }
5948
5949 if (EVT == NVT)
5950 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5951 SVOffset, isVolatile, Alignment);
5952 else
5953 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5954 SVOffset, EVT, isVolatile,
5955 Alignment);
5956
5957 // Remember that we legalized the chain.
5958 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5959
5960 if (ExtType == ISD::SEXTLOAD) {
5961 // The high part is obtained by SRA'ing all but one of the bits of the
5962 // lo part.
5963 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5964 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5965 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5966 } else if (ExtType == ISD::ZEXTLOAD) {
5967 // The high part is just a zero.
5968 Hi = DAG.getConstant(0, NVT);
5969 } else /* if (ExtType == ISD::EXTLOAD) */ {
5970 // The high part is undefined.
5971 Hi = DAG.getNode(ISD::UNDEF, NVT);
5972 }
5973 }
5974 break;
5975 }
5976 case ISD::AND:
5977 case ISD::OR:
5978 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5979 SDOperand LL, LH, RL, RH;
5980 ExpandOp(Node->getOperand(0), LL, LH);
5981 ExpandOp(Node->getOperand(1), RL, RH);
5982 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5983 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5984 break;
5985 }
5986 case ISD::SELECT: {
5987 SDOperand LL, LH, RL, RH;
5988 ExpandOp(Node->getOperand(1), LL, LH);
5989 ExpandOp(Node->getOperand(2), RL, RH);
5990 if (getTypeAction(NVT) == Expand)
5991 NVT = TLI.getTypeToExpandTo(NVT);
5992 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5993 if (VT != MVT::f32)
5994 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5995 break;
5996 }
5997 case ISD::SELECT_CC: {
5998 SDOperand TL, TH, FL, FH;
5999 ExpandOp(Node->getOperand(2), TL, TH);
6000 ExpandOp(Node->getOperand(3), FL, FH);
6001 if (getTypeAction(NVT) == Expand)
6002 NVT = TLI.getTypeToExpandTo(NVT);
6003 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6004 Node->getOperand(1), TL, FL, Node->getOperand(4));
6005 if (VT != MVT::f32)
6006 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6007 Node->getOperand(1), TH, FH, Node->getOperand(4));
6008 break;
6009 }
6010 case ISD::ANY_EXTEND:
6011 // The low part is any extension of the input (which degenerates to a copy).
6012 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6013 // The high part is undefined.
6014 Hi = DAG.getNode(ISD::UNDEF, NVT);
6015 break;
6016 case ISD::SIGN_EXTEND: {
6017 // The low part is just a sign extension of the input (which degenerates to
6018 // a copy).
6019 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6020
6021 // The high part is obtained by SRA'ing all but one of the bits of the lo
6022 // part.
6023 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
6024 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6025 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6026 break;
6027 }
6028 case ISD::ZERO_EXTEND:
6029 // The low part is just a zero extension of the input (which degenerates to
6030 // a copy).
6031 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6032
6033 // The high part is just a zero.
6034 Hi = DAG.getConstant(0, NVT);
6035 break;
6036
6037 case ISD::TRUNCATE: {
6038 // The input value must be larger than this value. Expand *it*.
6039 SDOperand NewLo;
6040 ExpandOp(Node->getOperand(0), NewLo, Hi);
6041
6042 // The low part is now either the right size, or it is closer. If not the
6043 // right size, make an illegal truncate so we recursively expand it.
6044 if (NewLo.getValueType() != Node->getValueType(0))
6045 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6046 ExpandOp(NewLo, Lo, Hi);
6047 break;
6048 }
6049
6050 case ISD::BIT_CONVERT: {
6051 SDOperand Tmp;
6052 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6053 // If the target wants to, allow it to lower this itself.
6054 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6055 case Expand: assert(0 && "cannot expand FP!");
6056 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6057 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6058 }
6059 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6060 }
6061
6062 // f32 / f64 must be expanded to i32 / i64.
6063 if (VT == MVT::f32 || VT == MVT::f64) {
6064 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6065 if (getTypeAction(NVT) == Expand)
6066 ExpandOp(Lo, Lo, Hi);
6067 break;
6068 }
6069
6070 // If source operand will be expanded to the same type as VT, i.e.
6071 // i64 <- f64, i32 <- f32, expand the source operand instead.
6072 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6073 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6074 ExpandOp(Node->getOperand(0), Lo, Hi);
6075 break;
6076 }
6077
6078 // Turn this into a load/store pair by default.
6079 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006080 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006081
6082 ExpandOp(Tmp, Lo, Hi);
6083 break;
6084 }
6085
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006086 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006087 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6088 TargetLowering::Custom &&
6089 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006090 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6091 assert(Tmp.Val && "Node must be custom expanded!");
6092 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006093 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006094 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006095 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006096 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097
Andrew Lenharth81580822008-03-05 01:15:49 +00006098 case ISD::ATOMIC_LCS: {
6099 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6100 assert(Tmp.Val && "Node must be custom expanded!");
6101 ExpandOp(Tmp.getValue(0), Lo, Hi);
6102 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6103 LegalizeOp(Tmp.getValue(1)));
6104 break;
6105 }
6106
6107
6108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006109 // These operators cannot be expanded directly, emit them as calls to
6110 // library functions.
6111 case ISD::FP_TO_SINT: {
6112 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6113 SDOperand Op;
6114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6115 case Expand: assert(0 && "cannot expand FP!");
6116 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6117 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6118 }
6119
6120 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6121
6122 // Now that the custom expander is done, expand the result, which is still
6123 // VT.
6124 if (Op.Val) {
6125 ExpandOp(Op, Lo, Hi);
6126 break;
6127 }
6128 }
6129
Dale Johannesenac77b272007-10-05 20:04:43 +00006130 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006131 if (Node->getOperand(0).getValueType() == MVT::f32)
6132 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006133 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006134 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006135 else if (Node->getOperand(0).getValueType() == MVT::f80)
6136 LC = RTLIB::FPTOSINT_F80_I64;
6137 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6138 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006139 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6140 false/*sign irrelevant*/, Hi);
6141 break;
6142 }
6143
6144 case ISD::FP_TO_UINT: {
6145 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6146 SDOperand Op;
6147 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6148 case Expand: assert(0 && "cannot expand FP!");
6149 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6150 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6151 }
6152
6153 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6154
6155 // Now that the custom expander is done, expand the result.
6156 if (Op.Val) {
6157 ExpandOp(Op, Lo, Hi);
6158 break;
6159 }
6160 }
6161
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00006162 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006163 if (Node->getOperand(0).getValueType() == MVT::f32)
6164 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen4e1cf5d2007-09-28 18:44:17 +00006165 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006166 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006167 else if (Node->getOperand(0).getValueType() == MVT::f80)
6168 LC = RTLIB::FPTOUINT_F80_I64;
6169 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6170 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006171 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6172 false/*sign irrelevant*/, Hi);
6173 break;
6174 }
6175
6176 case ISD::SHL: {
6177 // If the target wants custom lowering, do so.
6178 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6179 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6180 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6181 Op = TLI.LowerOperation(Op, DAG);
6182 if (Op.Val) {
6183 // Now that the custom expander is done, expand the result, which is
6184 // still VT.
6185 ExpandOp(Op, Lo, Hi);
6186 break;
6187 }
6188 }
6189
6190 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6191 // this X << 1 as X+X.
6192 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6193 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6194 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6195 SDOperand LoOps[2], HiOps[3];
6196 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6197 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6198 LoOps[1] = LoOps[0];
6199 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6200
6201 HiOps[1] = HiOps[0];
6202 HiOps[2] = Lo.getValue(1);
6203 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6204 break;
6205 }
6206 }
6207
6208 // If we can emit an efficient shift operation, do so now.
6209 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6210 break;
6211
6212 // If this target supports SHL_PARTS, use it.
6213 TargetLowering::LegalizeAction Action =
6214 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6215 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6216 Action == TargetLowering::Custom) {
6217 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6218 break;
6219 }
6220
6221 // Otherwise, emit a libcall.
6222 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6223 false/*left shift=unsigned*/, Hi);
6224 break;
6225 }
6226
6227 case ISD::SRA: {
6228 // If the target wants custom lowering, do so.
6229 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6230 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6231 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6232 Op = TLI.LowerOperation(Op, DAG);
6233 if (Op.Val) {
6234 // Now that the custom expander is done, expand the result, which is
6235 // still VT.
6236 ExpandOp(Op, Lo, Hi);
6237 break;
6238 }
6239 }
6240
6241 // If we can emit an efficient shift operation, do so now.
6242 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6243 break;
6244
6245 // If this target supports SRA_PARTS, use it.
6246 TargetLowering::LegalizeAction Action =
6247 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6248 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6249 Action == TargetLowering::Custom) {
6250 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6251 break;
6252 }
6253
6254 // Otherwise, emit a libcall.
6255 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6256 true/*ashr is signed*/, Hi);
6257 break;
6258 }
6259
6260 case ISD::SRL: {
6261 // If the target wants custom lowering, do so.
6262 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6263 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6264 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6265 Op = TLI.LowerOperation(Op, DAG);
6266 if (Op.Val) {
6267 // Now that the custom expander is done, expand the result, which is
6268 // still VT.
6269 ExpandOp(Op, Lo, Hi);
6270 break;
6271 }
6272 }
6273
6274 // If we can emit an efficient shift operation, do so now.
6275 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6276 break;
6277
6278 // If this target supports SRL_PARTS, use it.
6279 TargetLowering::LegalizeAction Action =
6280 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6281 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6282 Action == TargetLowering::Custom) {
6283 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6284 break;
6285 }
6286
6287 // Otherwise, emit a libcall.
6288 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6289 false/*lshr is unsigned*/, Hi);
6290 break;
6291 }
6292
6293 case ISD::ADD:
6294 case ISD::SUB: {
6295 // If the target wants to custom expand this, let them.
6296 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6297 TargetLowering::Custom) {
6298 Op = TLI.LowerOperation(Op, DAG);
6299 if (Op.Val) {
6300 ExpandOp(Op, Lo, Hi);
6301 break;
6302 }
6303 }
6304
6305 // Expand the subcomponents.
6306 SDOperand LHSL, LHSH, RHSL, RHSH;
6307 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6308 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6309 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6310 SDOperand LoOps[2], HiOps[3];
6311 LoOps[0] = LHSL;
6312 LoOps[1] = RHSL;
6313 HiOps[0] = LHSH;
6314 HiOps[1] = RHSH;
6315 if (Node->getOpcode() == ISD::ADD) {
6316 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6317 HiOps[2] = Lo.getValue(1);
6318 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6319 } else {
6320 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6321 HiOps[2] = Lo.getValue(1);
6322 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6323 }
6324 break;
6325 }
6326
6327 case ISD::ADDC:
6328 case ISD::SUBC: {
6329 // Expand the subcomponents.
6330 SDOperand LHSL, LHSH, RHSL, RHSH;
6331 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6332 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6333 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6334 SDOperand LoOps[2] = { LHSL, RHSL };
6335 SDOperand HiOps[3] = { LHSH, RHSH };
6336
6337 if (Node->getOpcode() == ISD::ADDC) {
6338 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6339 HiOps[2] = Lo.getValue(1);
6340 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6341 } else {
6342 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6343 HiOps[2] = Lo.getValue(1);
6344 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6345 }
6346 // Remember that we legalized the flag.
6347 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6348 break;
6349 }
6350 case ISD::ADDE:
6351 case ISD::SUBE: {
6352 // Expand the subcomponents.
6353 SDOperand LHSL, LHSH, RHSL, RHSH;
6354 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6355 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6356 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6357 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6358 SDOperand HiOps[3] = { LHSH, RHSH };
6359
6360 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6361 HiOps[2] = Lo.getValue(1);
6362 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6363
6364 // Remember that we legalized the flag.
6365 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6366 break;
6367 }
6368 case ISD::MUL: {
6369 // If the target wants to custom expand this, let them.
6370 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6371 SDOperand New = TLI.LowerOperation(Op, DAG);
6372 if (New.Val) {
6373 ExpandOp(New, Lo, Hi);
6374 break;
6375 }
6376 }
6377
6378 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6379 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006380 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6381 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6382 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006383 SDOperand LL, LH, RL, RH;
6384 ExpandOp(Node->getOperand(0), LL, LH);
6385 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006386 unsigned OuterBitSize = Op.getValueSizeInBits();
6387 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006388 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6389 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman07961cd2008-02-25 21:11:39 +00006390 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6391 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6392 DAG.MaskedValueIsZero(Op.getOperand(1),
6393 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohman5a199552007-10-08 18:33:35 +00006394 // The inputs are both zero-extended.
6395 if (HasUMUL_LOHI) {
6396 // We can emit a umul_lohi.
6397 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6398 Hi = SDOperand(Lo.Val, 1);
6399 break;
6400 }
6401 if (HasMULHU) {
6402 // We can emit a mulhu+mul.
6403 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6404 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6405 break;
6406 }
Dan Gohman5a199552007-10-08 18:33:35 +00006407 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006408 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006409 // The input values are both sign-extended.
6410 if (HasSMUL_LOHI) {
6411 // We can emit a smul_lohi.
6412 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6413 Hi = SDOperand(Lo.Val, 1);
6414 break;
6415 }
6416 if (HasMULHS) {
6417 // We can emit a mulhs+mul.
6418 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6419 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6420 break;
6421 }
6422 }
6423 if (HasUMUL_LOHI) {
6424 // Lo,Hi = umul LHS, RHS.
6425 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6426 DAG.getVTList(NVT, NVT), LL, RL);
6427 Lo = UMulLOHI;
6428 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006429 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6430 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6431 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6432 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6433 break;
6434 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006435 if (HasMULHU) {
6436 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6437 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6438 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6439 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6440 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6441 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6442 break;
6443 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 }
6445
Dan Gohman5a199552007-10-08 18:33:35 +00006446 // If nothing else, we can make a libcall.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006447 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6448 false/*sign irrelevant*/, Hi);
6449 break;
6450 }
6451 case ISD::SDIV:
6452 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6453 break;
6454 case ISD::UDIV:
6455 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6456 break;
6457 case ISD::SREM:
6458 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6459 break;
6460 case ISD::UREM:
6461 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6462 break;
6463
6464 case ISD::FADD:
Duncan Sands37a3f472008-01-10 10:28:30 +00006465 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6466 RTLIB::ADD_F64,
6467 RTLIB::ADD_F80,
6468 RTLIB::ADD_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006469 Node, false, Hi);
6470 break;
6471 case ISD::FSUB:
Duncan Sands37a3f472008-01-10 10:28:30 +00006472 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6473 RTLIB::SUB_F64,
6474 RTLIB::SUB_F80,
6475 RTLIB::SUB_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006476 Node, false, Hi);
6477 break;
6478 case ISD::FMUL:
Duncan Sands37a3f472008-01-10 10:28:30 +00006479 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6480 RTLIB::MUL_F64,
6481 RTLIB::MUL_F80,
6482 RTLIB::MUL_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006483 Node, false, Hi);
6484 break;
6485 case ISD::FDIV:
Duncan Sands37a3f472008-01-10 10:28:30 +00006486 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6487 RTLIB::DIV_F64,
6488 RTLIB::DIV_F80,
6489 RTLIB::DIV_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006490 Node, false, Hi);
6491 break;
6492 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006493 if (VT == MVT::ppcf128) {
6494 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6495 Node->getOperand(0).getValueType()==MVT::f64);
6496 const uint64_t zero = 0;
6497 if (Node->getOperand(0).getValueType()==MVT::f32)
6498 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6499 else
6500 Hi = Node->getOperand(0);
6501 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6502 break;
6503 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006504 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
6505 break;
6506 case ISD::FP_ROUND:
6507 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
6508 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006509 case ISD::FPOWI:
Duncan Sands37a3f472008-01-10 10:28:30 +00006510 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6511 RTLIB::POWI_F64,
6512 RTLIB::POWI_F80,
6513 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006514 Node, false, Hi);
6515 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006516 case ISD::FSQRT:
6517 case ISD::FSIN:
6518 case ISD::FCOS: {
6519 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6520 switch(Node->getOpcode()) {
6521 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006522 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6523 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006524 break;
6525 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006526 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6527 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006528 break;
6529 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006530 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6531 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 break;
6533 default: assert(0 && "Unreachable!");
6534 }
6535 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
6536 break;
6537 }
6538 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006539 if (VT == MVT::ppcf128) {
6540 SDOperand Tmp;
6541 ExpandOp(Node->getOperand(0), Lo, Tmp);
6542 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6543 // lo = hi==fabs(hi) ? lo : -lo;
6544 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6545 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6546 DAG.getCondCode(ISD::SETEQ));
6547 break;
6548 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006549 SDOperand Mask = (VT == MVT::f64)
6550 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6551 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6552 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6553 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6554 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6555 if (getTypeAction(NVT) == Expand)
6556 ExpandOp(Lo, Lo, Hi);
6557 break;
6558 }
6559 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006560 if (VT == MVT::ppcf128) {
6561 ExpandOp(Node->getOperand(0), Lo, Hi);
6562 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6563 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6564 break;
6565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006566 SDOperand Mask = (VT == MVT::f64)
6567 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6568 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6569 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6570 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6571 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6572 if (getTypeAction(NVT) == Expand)
6573 ExpandOp(Lo, Lo, Hi);
6574 break;
6575 }
6576 case ISD::FCOPYSIGN: {
6577 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6578 if (getTypeAction(NVT) == Expand)
6579 ExpandOp(Lo, Lo, Hi);
6580 break;
6581 }
6582 case ISD::SINT_TO_FP:
6583 case ISD::UINT_TO_FP: {
6584 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6585 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006586 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohman84d00962008-02-25 21:39:34 +00006587 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00006588 if (isSigned) {
6589 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6590 Node->getOperand(0)));
6591 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6592 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00006593 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00006594 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6595 Node->getOperand(0)));
6596 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6597 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006598 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006599 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6600 DAG.getConstant(0, MVT::i32),
6601 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6602 DAG.getConstantFP(
6603 APFloat(APInt(128, 2, TwoE32)),
6604 MVT::ppcf128)),
6605 Hi,
6606 DAG.getCondCode(ISD::SETLT)),
6607 Lo, Hi);
6608 }
6609 break;
6610 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006611 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6612 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00006613 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006614 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6615 Lo, Hi);
6616 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6617 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6618 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6619 DAG.getConstant(0, MVT::i64),
6620 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6621 DAG.getConstantFP(
6622 APFloat(APInt(128, 2, TwoE64)),
6623 MVT::ppcf128)),
6624 Hi,
6625 DAG.getCondCode(ISD::SETLT)),
6626 Lo, Hi);
6627 break;
6628 }
Evan Cheng20186812007-09-27 07:35:39 +00006629 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006630 if (Node->getOperand(0).getValueType() == MVT::i64) {
6631 if (VT == MVT::f32)
6632 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006633 else if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006634 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006635 else if (VT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00006636 assert(isSigned);
Dale Johannesenac77b272007-10-05 20:04:43 +00006637 LC = RTLIB::SINTTOFP_I64_F80;
6638 }
6639 else if (VT == MVT::ppcf128) {
6640 assert(isSigned);
6641 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006642 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006643 } else {
6644 if (VT == MVT::f32)
6645 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
6646 else
6647 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
6648 }
6649
6650 // Promote the operand if needed.
6651 if (getTypeAction(SrcVT) == Promote) {
6652 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6653 Tmp = isSigned
6654 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6655 DAG.getValueType(SrcVT))
6656 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6657 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6658 }
6659
6660 const char *LibCall = TLI.getLibcallName(LC);
6661 if (LibCall)
6662 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6663 else {
6664 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6665 Node->getOperand(0));
6666 if (getTypeAction(Lo.getValueType()) == Expand)
6667 ExpandOp(Lo, Lo, Hi);
6668 }
6669 break;
6670 }
6671 }
6672
6673 // Make sure the resultant values have been legalized themselves, unless this
6674 // is a type that requires multi-step expansion.
6675 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6676 Lo = LegalizeOp(Lo);
6677 if (Hi.Val)
6678 // Don't legalize the high part if it is expanded to a single node.
6679 Hi = LegalizeOp(Hi);
6680 }
6681
6682 // Remember in a map if the values will be reused later.
6683 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6684 assert(isNew && "Value already expanded?!?");
6685}
6686
6687/// SplitVectorOp - Given an operand of vector type, break it down into
6688/// two smaller values, still of vector type.
6689void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6690 SDOperand &Hi) {
6691 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6692 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006693 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006695
Dan Gohmana0763d92007-09-24 15:54:53 +00006696 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006697
6698 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6699 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6700
6701 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6702 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6703
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006704 // See if we already split it.
6705 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6706 = SplitNodes.find(Op);
6707 if (I != SplitNodes.end()) {
6708 Lo = I->second.first;
6709 Hi = I->second.second;
6710 return;
6711 }
6712
6713 switch (Node->getOpcode()) {
6714 default:
6715#ifndef NDEBUG
6716 Node->dump(&DAG);
6717#endif
6718 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006719 case ISD::UNDEF:
6720 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6721 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6722 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006723 case ISD::BUILD_PAIR:
6724 Lo = Node->getOperand(0);
6725 Hi = Node->getOperand(1);
6726 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006727 case ISD::INSERT_VECTOR_ELT: {
6728 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6729 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6730 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006731 if (Index < NewNumElts_Lo)
6732 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006733 DAG.getConstant(Index, TLI.getPointerTy()));
6734 else
Nate Begeman4a365ad2007-11-15 21:15:26 +00006735 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6736 DAG.getConstant(Index - NewNumElts_Lo,
6737 TLI.getPointerTy()));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006738 break;
6739 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006740 case ISD::VECTOR_SHUFFLE: {
6741 // Build the low part.
6742 SDOperand Mask = Node->getOperand(2);
6743 SmallVector<SDOperand, 8> Ops;
6744 MVT::ValueType PtrVT = TLI.getPointerTy();
6745
6746 // Insert all of the elements from the input that are needed. We use
6747 // buildvector of extractelement here because the input vectors will have
6748 // to be legalized, so this makes the code simpler.
6749 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6750 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6751 SDOperand InVec = Node->getOperand(0);
6752 if (Idx >= NumElements) {
6753 InVec = Node->getOperand(1);
6754 Idx -= NumElements;
6755 }
6756 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6757 DAG.getConstant(Idx, PtrVT)));
6758 }
6759 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6760 Ops.clear();
6761
6762 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6763 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6764 SDOperand InVec = Node->getOperand(0);
6765 if (Idx >= NumElements) {
6766 InVec = Node->getOperand(1);
6767 Idx -= NumElements;
6768 }
6769 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6770 DAG.getConstant(Idx, PtrVT)));
6771 }
6772 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6773 break;
6774 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006775 case ISD::BUILD_VECTOR: {
6776 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006777 Node->op_begin()+NewNumElts_Lo);
6778 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006779
Nate Begeman4a365ad2007-11-15 21:15:26 +00006780 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006781 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006782 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783 break;
6784 }
6785 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006786 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006787 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6788 if (NewNumSubvectors == 1) {
6789 Lo = Node->getOperand(0);
6790 Hi = Node->getOperand(1);
6791 } else {
6792 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6793 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006794 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006795
6796 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6797 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006798 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006799 }
6800 break;
6801 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006802 case ISD::SELECT: {
6803 SDOperand Cond = Node->getOperand(0);
6804
6805 SDOperand LL, LH, RL, RH;
6806 SplitVectorOp(Node->getOperand(1), LL, LH);
6807 SplitVectorOp(Node->getOperand(2), RL, RH);
6808
6809 if (MVT::isVector(Cond.getValueType())) {
6810 // Handle a vector merge.
6811 SDOperand CL, CH;
6812 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006813 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6814 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006815 } else {
6816 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006817 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6818 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006819 }
6820 break;
6821 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006822 case ISD::ADD:
6823 case ISD::SUB:
6824 case ISD::MUL:
6825 case ISD::FADD:
6826 case ISD::FSUB:
6827 case ISD::FMUL:
6828 case ISD::SDIV:
6829 case ISD::UDIV:
6830 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006831 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006832 case ISD::AND:
6833 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006834 case ISD::XOR:
6835 case ISD::UREM:
6836 case ISD::SREM:
6837 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006838 SDOperand LL, LH, RL, RH;
6839 SplitVectorOp(Node->getOperand(0), LL, LH);
6840 SplitVectorOp(Node->getOperand(1), RL, RH);
6841
Nate Begeman4a365ad2007-11-15 21:15:26 +00006842 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6843 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006844 break;
6845 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006846 case ISD::FPOWI: {
6847 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, Node->getOperand(1));
6851 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006852 break;
6853 }
6854 case ISD::CTTZ:
6855 case ISD::CTLZ:
6856 case ISD::CTPOP:
6857 case ISD::FNEG:
6858 case ISD::FABS:
6859 case ISD::FSQRT:
6860 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006861 case ISD::FCOS:
6862 case ISD::FP_TO_SINT:
6863 case ISD::FP_TO_UINT:
6864 case ISD::SINT_TO_FP:
6865 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006866 SDOperand L, H;
6867 SplitVectorOp(Node->getOperand(0), L, H);
6868
Nate Begeman4a365ad2007-11-15 21:15:26 +00006869 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6870 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006871 break;
6872 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006873 case ISD::LOAD: {
6874 LoadSDNode *LD = cast<LoadSDNode>(Node);
6875 SDOperand Ch = LD->getChain();
6876 SDOperand Ptr = LD->getBasePtr();
6877 const Value *SV = LD->getSrcValue();
6878 int SVOffset = LD->getSrcValueOffset();
6879 unsigned Alignment = LD->getAlignment();
6880 bool isVolatile = LD->isVolatile();
6881
Nate Begeman4a365ad2007-11-15 21:15:26 +00006882 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6883 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006884 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006885 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006886 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006887 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006888 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006889
6890 // Build a factor node to remember that this load is independent of the
6891 // other one.
6892 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6893 Hi.getValue(1));
6894
6895 // Remember that we legalized the chain.
6896 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6897 break;
6898 }
6899 case ISD::BIT_CONVERT: {
6900 // We know the result is a vector. The input may be either a vector or a
6901 // scalar value.
6902 SDOperand InOp = Node->getOperand(0);
6903 if (!MVT::isVector(InOp.getValueType()) ||
6904 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6905 // The input is a scalar or single-element vector.
6906 // Lower to a store/load so that it can be split.
6907 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006908 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman20e37962008-02-11 18:58:42 +00006909 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006910
6911 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006912 InOp, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006913 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006914 FI->getIndex());
6915 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006916 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006917 FI->getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006918 }
6919 // Split the vector and convert each of the pieces now.
6920 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006921 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6922 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006923 break;
6924 }
6925 }
6926
6927 // Remember in a map if the values will be reused later.
6928 bool isNew =
6929 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6930 assert(isNew && "Value already split?!?");
6931}
6932
6933
6934/// ScalarizeVectorOp - Given an operand of single-element vector type
6935/// (e.g. v1f32), convert it into the equivalent operation that returns a
6936/// scalar (e.g. f32) value.
6937SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6938 assert(MVT::isVector(Op.getValueType()) &&
6939 "Bad ScalarizeVectorOp invocation!");
6940 SDNode *Node = Op.Val;
6941 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6942 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6943
6944 // See if we already scalarized it.
6945 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6946 if (I != ScalarizedNodes.end()) return I->second;
6947
6948 SDOperand Result;
6949 switch (Node->getOpcode()) {
6950 default:
6951#ifndef NDEBUG
6952 Node->dump(&DAG); cerr << "\n";
6953#endif
6954 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6955 case ISD::ADD:
6956 case ISD::FADD:
6957 case ISD::SUB:
6958 case ISD::FSUB:
6959 case ISD::MUL:
6960 case ISD::FMUL:
6961 case ISD::SDIV:
6962 case ISD::UDIV:
6963 case ISD::FDIV:
6964 case ISD::SREM:
6965 case ISD::UREM:
6966 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006967 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006968 case ISD::AND:
6969 case ISD::OR:
6970 case ISD::XOR:
6971 Result = DAG.getNode(Node->getOpcode(),
6972 NewVT,
6973 ScalarizeVectorOp(Node->getOperand(0)),
6974 ScalarizeVectorOp(Node->getOperand(1)));
6975 break;
6976 case ISD::FNEG:
6977 case ISD::FABS:
6978 case ISD::FSQRT:
6979 case ISD::FSIN:
6980 case ISD::FCOS:
6981 Result = DAG.getNode(Node->getOpcode(),
6982 NewVT,
6983 ScalarizeVectorOp(Node->getOperand(0)));
6984 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006985 case ISD::FPOWI:
6986 Result = DAG.getNode(Node->getOpcode(),
6987 NewVT,
6988 ScalarizeVectorOp(Node->getOperand(0)),
6989 Node->getOperand(1));
6990 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006991 case ISD::LOAD: {
6992 LoadSDNode *LD = cast<LoadSDNode>(Node);
6993 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6994 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6995
6996 const Value *SV = LD->getSrcValue();
6997 int SVOffset = LD->getSrcValueOffset();
6998 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6999 LD->isVolatile(), LD->getAlignment());
7000
7001 // Remember that we legalized the chain.
7002 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7003 break;
7004 }
7005 case ISD::BUILD_VECTOR:
7006 Result = Node->getOperand(0);
7007 break;
7008 case ISD::INSERT_VECTOR_ELT:
7009 // Returning the inserted scalar element.
7010 Result = Node->getOperand(1);
7011 break;
7012 case ISD::CONCAT_VECTORS:
7013 assert(Node->getOperand(0).getValueType() == NewVT &&
7014 "Concat of non-legal vectors not yet supported!");
7015 Result = Node->getOperand(0);
7016 break;
7017 case ISD::VECTOR_SHUFFLE: {
7018 // Figure out if the scalar is the LHS or RHS and return it.
7019 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7020 if (cast<ConstantSDNode>(EltNum)->getValue())
7021 Result = ScalarizeVectorOp(Node->getOperand(1));
7022 else
7023 Result = ScalarizeVectorOp(Node->getOperand(0));
7024 break;
7025 }
7026 case ISD::EXTRACT_SUBVECTOR:
7027 Result = Node->getOperand(0);
7028 assert(Result.getValueType() == NewVT);
7029 break;
7030 case ISD::BIT_CONVERT:
7031 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
7032 break;
7033 case ISD::SELECT:
7034 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7035 ScalarizeVectorOp(Op.getOperand(1)),
7036 ScalarizeVectorOp(Op.getOperand(2)));
7037 break;
7038 }
7039
7040 if (TLI.isTypeLegal(NewVT))
7041 Result = LegalizeOp(Result);
7042 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7043 assert(isNew && "Value already scalarized?");
7044 return Result;
7045}
7046
7047
7048// SelectionDAG::Legalize - This is the entry point for the file.
7049//
7050void SelectionDAG::Legalize() {
7051 if (ViewLegalizeDAGs) viewGraph();
7052
7053 /// run - This is the main entry point to this class.
7054 ///
7055 SelectionDAGLegalize(*this).LegalizeDAG();
7056}
7057