blob: 5f260ece552a0c42c4b6504e1884a646762e803f [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"
Dan Gohmane8b391e2008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/ADT/SmallPtrSet.h"
35#include <map>
36using namespace llvm;
37
38#ifndef NDEBUG
39static cl::opt<bool>
40ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
41 cl::desc("Pop up a window to show dags before legalize"));
42#else
43static const bool ViewLegalizeDAGs = 0;
44#endif
45
46//===----------------------------------------------------------------------===//
47/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
48/// hacks on it until the target machine can handle it. This involves
49/// eliminating value sizes the machine cannot handle (promoting small sizes to
50/// large sizes or splitting up large values into small values) as well as
51/// eliminating operations the machine cannot handle.
52///
53/// This code also does a small amount of optimization and recognition of idioms
54/// as part of its processing. For example, if a target does not support a
55/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
56/// will attempt merge setcc and brc instructions into brcc's.
57///
58namespace {
59class VISIBILITY_HIDDEN SelectionDAGLegalize {
60 TargetLowering &TLI;
61 SelectionDAG &DAG;
62
63 // Libcall insertion helpers.
64
65 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
66 /// legalized. We use this to ensure that calls are properly serialized
67 /// against each other, including inserted libcalls.
68 SDOperand LastCALLSEQ_END;
69
70 /// IsLegalizingCall - This member is used *only* for purposes of providing
71 /// helpful assertions that a libcall isn't created while another call is
72 /// being legalized (which could lead to non-serialized call sequences).
73 bool IsLegalizingCall;
74
75 enum LegalizeAction {
76 Legal, // The target natively supports this operation.
77 Promote, // This operation should be executed in a larger type.
78 Expand // Try to expand this to other ops, otherwise use a libcall.
79 };
80
81 /// ValueTypeActions - This is a bitvector that contains two bits for each
82 /// value type, where the two bits correspond to the LegalizeAction enum.
83 /// This can be queried with "getTypeAction(VT)".
84 TargetLowering::ValueTypeActionImpl ValueTypeActions;
85
86 /// LegalizedNodes - For nodes that are of legal width, and that have more
87 /// than one use, this map indicates what regularized operand to use. This
88 /// allows us to avoid legalizing the same thing more than once.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +000089 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090
91 /// PromotedNodes - For nodes that are below legal width, and that have more
92 /// than one use, this map indicates what promoted value to use. This allows
93 /// us to avoid promoting the same thing more than once.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +000094 DenseMap<SDOperand, SDOperand> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 /// ExpandedNodes - For nodes that need to be expanded this map indicates
97 /// which which operands are the expanded version of the input. This allows
98 /// us to avoid expanding the same node more than once.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +000099 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 /// SplitNodes - For vector nodes that need to be split, this map indicates
102 /// which which operands are the split version of the input. This allows us
103 /// to avoid splitting the same node more than once.
104 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
105
106 /// ScalarizedNodes - For nodes that need to be converted from vector types to
107 /// scalar types, this contains the mapping of ones we have already
108 /// processed to the result.
109 std::map<SDOperand, SDOperand> ScalarizedNodes;
110
111 void AddLegalizedOperand(SDOperand From, SDOperand To) {
112 LegalizedNodes.insert(std::make_pair(From, To));
113 // If someone requests legalization of the new node, return itself.
114 if (From != To)
115 LegalizedNodes.insert(std::make_pair(To, To));
116 }
117 void AddPromotedOperand(SDOperand From, SDOperand To) {
118 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
119 assert(isNew && "Got into the map somehow?");
120 // If someone requests legalization of the new node, return itself.
121 LegalizedNodes.insert(std::make_pair(To, To));
122 }
123
124public:
125
126 SelectionDAGLegalize(SelectionDAG &DAG);
127
128 /// getTypeAction - Return how we should legalize values of this type, either
129 /// it is already legal or we need to expand it into multiple registers of
130 /// smaller integer type, or we need to promote it to a larger type.
131 LegalizeAction getTypeAction(MVT::ValueType VT) const {
132 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
133 }
134
135 /// isTypeLegal - Return true if this type is legal on this target.
136 ///
137 bool isTypeLegal(MVT::ValueType VT) const {
138 return getTypeAction(VT) == Legal;
139 }
140
141 void LegalizeDAG();
142
143private:
144 /// HandleOp - Legalize, Promote, or Expand the specified operand as
145 /// appropriate for its type.
146 void HandleOp(SDOperand Op);
147
148 /// LegalizeOp - We know that the specified value has a legal type.
149 /// Recursively ensure that the operands have legal types, then return the
150 /// result.
151 SDOperand LegalizeOp(SDOperand O);
152
Dan Gohman6d05cac2007-10-11 23:57:53 +0000153 /// UnrollVectorOp - We know that the given vector has a legal type, however
154 /// the operation it performs is not legal and is an operation that we have
155 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
156 /// operating on each element individually.
157 SDOperand UnrollVectorOp(SDOperand O);
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 /// PromoteOp - Given an operation that produces a value in an invalid type,
160 /// promote it to compute the value into a larger type. The produced value
161 /// will have the correct bits for the low portion of the register, but no
162 /// guarantee is made about the top bits: it may be zero, sign-extended, or
163 /// garbage.
164 SDOperand PromoteOp(SDOperand O);
165
166 /// ExpandOp - Expand the specified SDOperand into its two component pieces
167 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
168 /// the LegalizeNodes map is filled in for any results that are not expanded,
169 /// the ExpandedNodes map is filled in for any results that are expanded, and
170 /// the Lo/Hi values are returned. This applies to integer types and Vector
171 /// types.
172 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
173
174 /// SplitVectorOp - Given an operand of vector type, break it down into
175 /// two smaller values.
176 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
177
178 /// ScalarizeVectorOp - Given an operand of single-element vector type
179 /// (e.g. v1f32), convert it into the equivalent operation that returns a
180 /// scalar (e.g. f32) value.
181 SDOperand ScalarizeVectorOp(SDOperand O);
182
183 /// isShuffleLegal - Return true if a vector shuffle is legal with the
184 /// specified mask and type. Targets can specify exactly which masks they
185 /// support and the code generator is tasked with not creating illegal masks.
186 ///
187 /// Note that this will also return true for shuffles that are promoted to a
188 /// different type.
189 ///
190 /// If this is a legal shuffle, this method returns the (possibly promoted)
191 /// build_vector Mask. If it's not a legal shuffle, it returns null.
192 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
193
194 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
195 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
196
197 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
198
Duncan Sandsf1db7c82008-04-12 17:14:18 +0000199 SDOperand ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 SDOperand &Hi);
201 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
202 SDOperand Source);
203
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +0000204 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
205 MVT::ValueType DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
207 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
208 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
209 SDOperand LegalOp,
210 MVT::ValueType DestVT);
211 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
213 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
214 bool isSigned);
215
216 SDOperand ExpandBSWAP(SDOperand Op);
217 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
218 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
220 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
221 SDOperand &Lo, SDOperand &Hi);
222
223 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
224 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225};
226}
227
228/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
229/// specified mask and type. Targets can specify exactly which masks they
230/// support and the code generator is tasked with not creating illegal masks.
231///
232/// Note that this will also return true for shuffles that are promoted to a
233/// different type.
234SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
235 SDOperand Mask) const {
236 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
237 default: return 0;
238 case TargetLowering::Legal:
239 case TargetLowering::Custom:
240 break;
241 case TargetLowering::Promote: {
242 // If this is promoted to a different type, convert the shuffle mask and
243 // ask if it is legal in the promoted type!
244 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
245
246 // If we changed # elements, change the shuffle mask.
247 unsigned NumEltsGrowth =
248 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
249 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
250 if (NumEltsGrowth > 1) {
251 // Renumber the elements.
252 SmallVector<SDOperand, 8> Ops;
253 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
254 SDOperand InOp = Mask.getOperand(i);
255 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
256 if (InOp.getOpcode() == ISD::UNDEF)
257 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
258 else {
259 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
260 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
261 }
262 }
263 }
264 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
265 }
266 VT = NVT;
267 break;
268 }
269 }
270 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
271}
272
273SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
274 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
275 ValueTypeActions(TLI.getValueTypeActions()) {
276 assert(MVT::LAST_VALUETYPE <= 32 &&
277 "Too many value types for ValueTypeActions to hold!");
278}
279
280/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
281/// contains all of a nodes operands before it contains the node.
282static void ComputeTopDownOrdering(SelectionDAG &DAG,
283 SmallVector<SDNode*, 64> &Order) {
284
285 DenseMap<SDNode*, unsigned> Visited;
286 std::vector<SDNode*> Worklist;
287 Worklist.reserve(128);
288
289 // Compute ordering from all of the leaves in the graphs, those (like the
290 // entry node) that have no operands.
291 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
292 E = DAG.allnodes_end(); I != E; ++I) {
293 if (I->getNumOperands() == 0) {
294 Visited[I] = 0 - 1U;
295 Worklist.push_back(I);
296 }
297 }
298
299 while (!Worklist.empty()) {
300 SDNode *N = Worklist.back();
301 Worklist.pop_back();
302
303 if (++Visited[N] != N->getNumOperands())
304 continue; // Haven't visited all operands yet
305
306 Order.push_back(N);
307
308 // Now that we have N in, add anything that uses it if all of their operands
309 // are now done.
310 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
311 UI != E; ++UI)
Roman Levenstein05650fd2008-04-07 10:06:32 +0000312 Worklist.push_back(UI->getUser());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 }
314
315 assert(Order.size() == Visited.size() &&
316 Order.size() ==
317 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
318 "Error: DAG is cyclic!");
319}
320
321
322void SelectionDAGLegalize::LegalizeDAG() {
323 LastCALLSEQ_END = DAG.getEntryNode();
324 IsLegalizingCall = false;
325
326 // The legalize process is inherently a bottom-up recursive process (users
327 // legalize their uses before themselves). Given infinite stack space, we
328 // could just start legalizing on the root and traverse the whole graph. In
329 // practice however, this causes us to run out of stack space on large basic
330 // blocks. To avoid this problem, compute an ordering of the nodes where each
331 // node is only legalized after all of its operands are legalized.
332 SmallVector<SDNode*, 64> Order;
333 ComputeTopDownOrdering(DAG, Order);
334
335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
337
338 // Finally, it's possible the root changed. Get the new root.
339 SDOperand OldRoot = DAG.getRoot();
340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
345 PromotedNodes.clear();
346 SplitNodes.clear();
347 ScalarizedNodes.clear();
348
349 // Remove dead nodes now.
350 DAG.RemoveDeadNodes();
351}
352
353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
Roman Levenstein05650fd2008-04-07 10:06:32 +0000385 SDNode *User = UI->getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
408///
409/// Keep track of the nodes we fine that actually do lead to Dest in
410/// NodesLeadingTo. This avoids retraversing them exponential number of times.
411///
412bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
413 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
414 if (N == Dest) return true; // N certainly leads to Dest :)
415
416 // If we've already processed this node and it does lead to Dest, there is no
417 // need to reprocess it.
418 if (NodesLeadingTo.count(N)) return true;
419
420 // If the first result of this node has been already legalized, then it cannot
421 // reach N.
422 switch (getTypeAction(N->getValueType(0))) {
423 case Legal:
424 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
425 break;
426 case Promote:
427 if (PromotedNodes.count(SDOperand(N, 0))) return false;
428 break;
429 case Expand:
430 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 }
433
434 // Okay, this node has not already been legalized. Check and legalize all
435 // operands. If none lead to Dest, then we can legalize this node.
436 bool OperandsLeadToDest = false;
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
439 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
440
441 if (OperandsLeadToDest) {
442 NodesLeadingTo.insert(N);
443 return true;
444 }
445
446 // Okay, this node looks safe, legalize it and return false.
447 HandleOp(SDOperand(N, 0));
448 return false;
449}
450
451/// HandleOp - Legalize, Promote, or Expand the specified operand as
452/// appropriate for its type.
453void SelectionDAGLegalize::HandleOp(SDOperand Op) {
454 MVT::ValueType VT = Op.getValueType();
455 switch (getTypeAction(VT)) {
456 default: assert(0 && "Bad type action!");
457 case Legal: (void)LegalizeOp(Op); break;
458 case Promote: (void)PromoteOp(Op); break;
459 case Expand:
460 if (!MVT::isVector(VT)) {
461 // If this is an illegal scalar, expand it into its two component
462 // pieces.
463 SDOperand X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000464 if (Op.getOpcode() == ISD::TargetConstant)
465 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 ExpandOp(Op, X, Y);
467 } else if (MVT::getVectorNumElements(VT) == 1) {
468 // If this is an illegal single element vector, convert it to a
469 // scalar operation.
470 (void)ScalarizeVectorOp(Op);
471 } else {
472 // Otherwise, this is an illegal multiple element vector.
473 // Split it in half and legalize both parts.
474 SDOperand X, Y;
475 SplitVectorOp(Op, X, Y);
476 }
477 break;
478 }
479}
480
481/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
482/// a load from the constant pool.
483static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
484 SelectionDAG &DAG, TargetLowering &TLI) {
485 bool Extend = false;
486
487 // If a FP immediate is precise when represented as a float and if the
488 // target can do an extending load from float to double, we put it into
489 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000490 // double. This shrinks FP constants and canonicalizes them for targets where
491 // an FP extending load is the same cost as a normal load (such as on the x87
492 // fp stack or PPC FP unit).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 MVT::ValueType VT = CFP->getValueType(0);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000494 ConstantFP *LLVMC = ConstantFP::get(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");
Dan Gohman39509762008-03-11 00:11:06 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
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.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000786 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 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:
Duncan Sandsc93fae32008-03-21 09:14:45 +0000811 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000813 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 "This must be legal!");
815 break;
816 default:
817 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
818 // If this is a target node, legalize it by legalizing the operands then
819 // passing it through.
820 SmallVector<SDOperand, 8> Ops;
821 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
822 Ops.push_back(LegalizeOp(Node->getOperand(i)));
823
824 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
825
826 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
827 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
828 return Result.getValue(Op.ResNo);
829 }
830 // Otherwise this is an unhandled builtin node. splat.
831#ifndef NDEBUG
832 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
833#endif
834 assert(0 && "Do not know how to legalize this operator!");
835 abort();
836 case ISD::GLOBAL_OFFSET_TABLE:
837 case ISD::GlobalAddress:
838 case ISD::GlobalTLSAddress:
839 case ISD::ExternalSymbol:
840 case ISD::ConstantPool:
841 case ISD::JumpTable: // Nothing to do.
842 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
843 default: assert(0 && "This action is not supported yet!");
844 case TargetLowering::Custom:
845 Tmp1 = TLI.LowerOperation(Op, DAG);
846 if (Tmp1.Val) Result = Tmp1;
847 // FALLTHROUGH if the target doesn't want to lower this op after all.
848 case TargetLowering::Legal:
849 break;
850 }
851 break;
852 case ISD::FRAMEADDR:
853 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 // The only option for these nodes is to custom lower them. If the target
855 // does not custom lower them, then return zero.
856 Tmp1 = TLI.LowerOperation(Op, DAG);
857 if (Tmp1.Val)
858 Result = Tmp1;
859 else
860 Result = DAG.getConstant(0, TLI.getPointerTy());
861 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000862 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000863 MVT::ValueType VT = Node->getValueType(0);
864 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
865 default: assert(0 && "This action is not supported yet!");
866 case TargetLowering::Custom:
867 Result = TLI.LowerOperation(Op, DAG);
868 if (Result.Val) break;
869 // Fall Thru
870 case TargetLowering::Legal:
871 Result = DAG.getConstant(0, VT);
872 break;
873 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000874 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000875 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 case ISD::EXCEPTIONADDR: {
877 Tmp1 = LegalizeOp(Node->getOperand(0));
878 MVT::ValueType VT = Node->getValueType(0);
879 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
880 default: assert(0 && "This action is not supported yet!");
881 case TargetLowering::Expand: {
882 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000883 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 }
885 break;
886 case TargetLowering::Custom:
887 Result = TLI.LowerOperation(Op, DAG);
888 if (Result.Val) break;
889 // Fall Thru
890 case TargetLowering::Legal: {
891 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
892 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000893 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000894 break;
895 }
896 }
897 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000898 if (Result.Val->getNumValues() == 1) break;
899
900 assert(Result.Val->getNumValues() == 2 &&
901 "Cannot return more than two values!");
902
903 // Since we produced two values, make sure to remember that we
904 // legalized both of them.
905 Tmp1 = LegalizeOp(Result);
906 Tmp2 = LegalizeOp(Result.getValue(1));
907 AddLegalizedOperand(Op.getValue(0), Tmp1);
908 AddLegalizedOperand(Op.getValue(1), Tmp2);
909 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 case ISD::EHSELECTION: {
911 Tmp1 = LegalizeOp(Node->getOperand(0));
912 Tmp2 = LegalizeOp(Node->getOperand(1));
913 MVT::ValueType VT = Node->getValueType(0);
914 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
915 default: assert(0 && "This action is not supported yet!");
916 case TargetLowering::Expand: {
917 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000918 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
920 break;
921 case TargetLowering::Custom:
922 Result = TLI.LowerOperation(Op, DAG);
923 if (Result.Val) break;
924 // Fall Thru
925 case TargetLowering::Legal: {
926 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
927 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000928 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 break;
930 }
931 }
932 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000933 if (Result.Val->getNumValues() == 1) break;
934
935 assert(Result.Val->getNumValues() == 2 &&
936 "Cannot return more than two values!");
937
938 // Since we produced two values, make sure to remember that we
939 // legalized both of them.
940 Tmp1 = LegalizeOp(Result);
941 Tmp2 = LegalizeOp(Result.getValue(1));
942 AddLegalizedOperand(Op.getValue(0), Tmp1);
943 AddLegalizedOperand(Op.getValue(1), Tmp2);
944 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 case ISD::EH_RETURN: {
946 MVT::ValueType VT = Node->getValueType(0);
947 // The only "good" option for this node is to custom lower it.
948 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
949 default: assert(0 && "This action is not supported at all!");
950 case TargetLowering::Custom:
951 Result = TLI.LowerOperation(Op, DAG);
952 if (Result.Val) break;
953 // Fall Thru
954 case TargetLowering::Legal:
955 // Target does not know, how to lower this, lower to noop
956 Result = LegalizeOp(Node->getOperand(0));
957 break;
958 }
959 }
960 break;
961 case ISD::AssertSext:
962 case ISD::AssertZext:
963 Tmp1 = LegalizeOp(Node->getOperand(0));
964 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
965 break;
966 case ISD::MERGE_VALUES:
967 // Legalize eliminates MERGE_VALUES nodes.
968 Result = Node->getOperand(Op.ResNo);
969 break;
970 case ISD::CopyFromReg:
971 Tmp1 = LegalizeOp(Node->getOperand(0));
972 Result = Op.getValue(0);
973 if (Node->getNumValues() == 2) {
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
975 } else {
976 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
977 if (Node->getNumOperands() == 3) {
978 Tmp2 = LegalizeOp(Node->getOperand(2));
979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
980 } else {
981 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
982 }
983 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
984 }
985 // Since CopyFromReg produces two values, make sure to remember that we
986 // legalized both of them.
987 AddLegalizedOperand(Op.getValue(0), Result);
988 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
989 return Result.getValue(Op.ResNo);
990 case ISD::UNDEF: {
991 MVT::ValueType VT = Op.getValueType();
992 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
993 default: assert(0 && "This action is not supported yet!");
994 case TargetLowering::Expand:
995 if (MVT::isInteger(VT))
996 Result = DAG.getConstant(0, VT);
997 else if (MVT::isFloatingPoint(VT))
Dale Johannesen20b76352007-09-26 17:26:49 +0000998 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
999 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 else
1001 assert(0 && "Unknown value type!");
1002 break;
1003 case TargetLowering::Legal:
1004 break;
1005 }
1006 break;
1007 }
1008
1009 case ISD::INTRINSIC_W_CHAIN:
1010 case ISD::INTRINSIC_WO_CHAIN:
1011 case ISD::INTRINSIC_VOID: {
1012 SmallVector<SDOperand, 8> Ops;
1013 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1014 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1015 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1016
1017 // Allow the target to custom lower its intrinsics if it wants to.
1018 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1019 TargetLowering::Custom) {
1020 Tmp3 = TLI.LowerOperation(Result, DAG);
1021 if (Tmp3.Val) Result = Tmp3;
1022 }
1023
1024 if (Result.Val->getNumValues() == 1) break;
1025
1026 // Must have return value and chain result.
1027 assert(Result.Val->getNumValues() == 2 &&
1028 "Cannot return more than two values!");
1029
1030 // Since loads produce two values, make sure to remember that we
1031 // legalized both of them.
1032 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1033 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1034 return Result.getValue(Op.ResNo);
1035 }
1036
1037 case ISD::LOCATION:
1038 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1040
1041 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1042 case TargetLowering::Promote:
1043 default: assert(0 && "This action is not supported yet!");
1044 case TargetLowering::Expand: {
1045 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1046 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
1047 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
1048
1049 if (MMI && (useDEBUG_LOC || useLABEL)) {
1050 const std::string &FName =
1051 cast<StringSDNode>(Node->getOperand(3))->getValue();
1052 const std::string &DirName =
1053 cast<StringSDNode>(Node->getOperand(4))->getValue();
1054 unsigned SrcFile = MMI->RecordSource(DirName, FName);
1055
1056 SmallVector<SDOperand, 8> Ops;
1057 Ops.push_back(Tmp1); // chain
1058 SDOperand LineOp = Node->getOperand(1);
1059 SDOperand ColOp = Node->getOperand(2);
1060
1061 if (useDEBUG_LOC) {
1062 Ops.push_back(LineOp); // line #
1063 Ops.push_back(ColOp); // col #
1064 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
1065 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
1066 } else {
1067 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1068 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng69eda822008-02-01 02:05:57 +00001069 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng13d1c292008-01-31 09:59:15 +00001071 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1072 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 }
1074 } else {
1075 Result = Tmp1; // chain
1076 }
1077 break;
1078 }
1079 case TargetLowering::Legal:
1080 if (Tmp1 != Node->getOperand(0) ||
1081 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
1082 SmallVector<SDOperand, 8> Ops;
1083 Ops.push_back(Tmp1);
1084 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1085 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1086 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1087 } else {
1088 // Otherwise promote them.
1089 Ops.push_back(PromoteOp(Node->getOperand(1)));
1090 Ops.push_back(PromoteOp(Node->getOperand(2)));
1091 }
1092 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1093 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1094 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1095 }
1096 break;
1097 }
1098 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001099
1100 case ISD::DECLARE:
1101 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1102 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1103 default: assert(0 && "This action is not supported yet!");
1104 case TargetLowering::Legal:
1105 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1106 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1107 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1108 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1109 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001110 case TargetLowering::Expand:
1111 Result = LegalizeOp(Node->getOperand(0));
1112 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001113 }
1114 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115
1116 case ISD::DEBUG_LOC:
1117 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1118 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1119 default: assert(0 && "This action is not supported yet!");
1120 case TargetLowering::Legal:
1121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1123 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1124 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1126 break;
1127 }
1128 break;
1129
1130 case ISD::LABEL:
Evan Cheng13d1c292008-01-31 09:59:15 +00001131 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Legal:
1135 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1136 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng13d1c292008-01-31 09:59:15 +00001137 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1138 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139 break;
1140 case TargetLowering::Expand:
1141 Result = LegalizeOp(Node->getOperand(0));
1142 break;
1143 }
1144 break;
1145
Evan Chengd1d68072008-03-08 00:58:38 +00001146 case ISD::PREFETCH:
1147 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1148 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1149 default: assert(0 && "This action is not supported yet!");
1150 case TargetLowering::Legal:
1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1152 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1153 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1154 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1155 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1156 break;
1157 case TargetLowering::Expand:
1158 // It's a noop.
1159 Result = LegalizeOp(Node->getOperand(0));
1160 break;
1161 }
1162 break;
1163
Andrew Lenharth785610d2008-02-16 01:24:58 +00001164 case ISD::MEMBARRIER: {
1165 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001166 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1167 default: assert(0 && "This action is not supported yet!");
1168 case TargetLowering::Legal: {
1169 SDOperand Ops[6];
1170 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001171 for (int x = 1; x < 6; ++x) {
1172 Ops[x] = Node->getOperand(x);
1173 if (!isTypeLegal(Ops[x].getValueType()))
1174 Ops[x] = PromoteOp(Ops[x]);
1175 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1177 break;
1178 }
1179 case TargetLowering::Expand:
1180 //There is no libgcc call for this op
1181 Result = Node->getOperand(0); // Noop
1182 break;
1183 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001184 break;
1185 }
1186
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001187 case ISD::ATOMIC_LCS:
1188 case ISD::ATOMIC_LAS:
1189 case ISD::ATOMIC_SWAP: {
1190 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1191 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1192 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001193 "Invalid Atomic node!");
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001194 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001195 SDOperand Ops[4];
1196 for (int x = 0; x < num; ++x)
1197 Ops[x] = LegalizeOp(Node->getOperand(x));
1198 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1199
1200 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001201 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001202 case TargetLowering::Custom:
1203 Result = TLI.LowerOperation(Result, DAG);
1204 break;
1205 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001206 break;
1207 }
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001208 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1209 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1210 return Result.getValue(Op.ResNo);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001211 }
1212
Scott Michelf2e2b702007-08-08 23:23:31 +00001213 case ISD::Constant: {
1214 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1215 unsigned opAction =
1216 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001218 // We know we don't need to expand constants here, constants only have one
1219 // value and we check that it is fine above.
1220
Scott Michelf2e2b702007-08-08 23:23:31 +00001221 if (opAction == TargetLowering::Custom) {
1222 Tmp1 = TLI.LowerOperation(Result, DAG);
1223 if (Tmp1.Val)
1224 Result = Tmp1;
1225 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001227 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 case ISD::ConstantFP: {
1229 // Spill FP immediates to the constant pool if the target cannot directly
1230 // codegen them. Targets often have some immediate values that can be
1231 // efficiently generated into an FP register without a load. We explicitly
1232 // leave these constants as ConstantFP nodes for the target to deal with.
1233 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1234
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001235 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1236 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001237 case TargetLowering::Legal:
1238 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 case TargetLowering::Custom:
1240 Tmp3 = TLI.LowerOperation(Result, DAG);
1241 if (Tmp3.Val) {
1242 Result = Tmp3;
1243 break;
1244 }
1245 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001246 case TargetLowering::Expand: {
1247 // Check to see if this FP immediate is already legal.
1248 bool isLegal = false;
1249 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1250 E = TLI.legal_fpimm_end(); I != E; ++I) {
1251 if (CFP->isExactlyValue(*I)) {
1252 isLegal = true;
1253 break;
1254 }
1255 }
1256 // If this is a legal constant, turn it into a TargetConstantFP node.
1257 if (isLegal)
1258 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1260 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001261 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 break;
1263 }
1264 case ISD::TokenFactor:
1265 if (Node->getNumOperands() == 2) {
1266 Tmp1 = LegalizeOp(Node->getOperand(0));
1267 Tmp2 = LegalizeOp(Node->getOperand(1));
1268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1269 } else if (Node->getNumOperands() == 3) {
1270 Tmp1 = LegalizeOp(Node->getOperand(0));
1271 Tmp2 = LegalizeOp(Node->getOperand(1));
1272 Tmp3 = LegalizeOp(Node->getOperand(2));
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1274 } else {
1275 SmallVector<SDOperand, 8> Ops;
1276 // Legalize the operands.
1277 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1278 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1279 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1280 }
1281 break;
1282
1283 case ISD::FORMAL_ARGUMENTS:
1284 case ISD::CALL:
1285 // The only option for this is to custom lower it.
1286 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1287 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001288 // A call within a calling sequence must be legalized to something
1289 // other than the normal CALLSEQ_END. Violating this gets Legalize
1290 // into an infinite loop.
1291 assert ((!IsLegalizingCall ||
1292 Node->getOpcode() != ISD::CALL ||
1293 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1294 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001295
1296 // The number of incoming and outgoing values should match; unless the final
1297 // outgoing value is a flag.
1298 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1299 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1300 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1301 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 "Lowering call/formal_arguments produced unexpected # results!");
1303
1304 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1305 // remember that we legalized all of them, so it doesn't get relegalized.
1306 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001307 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1308 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001309 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1310 if (Op.ResNo == i)
1311 Tmp2 = Tmp1;
1312 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1313 }
1314 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001315 case ISD::EXTRACT_SUBREG: {
1316 Tmp1 = LegalizeOp(Node->getOperand(0));
1317 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1318 assert(idx && "Operand must be a constant");
1319 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1321 }
1322 break;
1323 case ISD::INSERT_SUBREG: {
1324 Tmp1 = LegalizeOp(Node->getOperand(0));
1325 Tmp2 = LegalizeOp(Node->getOperand(1));
1326 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1327 assert(idx && "Operand must be a constant");
1328 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1330 }
1331 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001332 case ISD::BUILD_VECTOR:
1333 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1334 default: assert(0 && "This action is not supported yet!");
1335 case TargetLowering::Custom:
1336 Tmp3 = TLI.LowerOperation(Result, DAG);
1337 if (Tmp3.Val) {
1338 Result = Tmp3;
1339 break;
1340 }
1341 // FALLTHROUGH
1342 case TargetLowering::Expand:
1343 Result = ExpandBUILD_VECTOR(Result.Val);
1344 break;
1345 }
1346 break;
1347 case ISD::INSERT_VECTOR_ELT:
1348 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001350
1351 // The type of the value to insert may not be legal, even though the vector
1352 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1353 // here.
1354 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1355 default: assert(0 && "Cannot expand insert element operand");
1356 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1357 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1360
1361 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1362 Node->getValueType(0))) {
1363 default: assert(0 && "This action is not supported yet!");
1364 case TargetLowering::Legal:
1365 break;
1366 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001367 Tmp4 = TLI.LowerOperation(Result, DAG);
1368 if (Tmp4.Val) {
1369 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 break;
1371 }
1372 // FALLTHROUGH
1373 case TargetLowering::Expand: {
1374 // If the insert index is a constant, codegen this as a scalar_to_vector,
1375 // then a shuffle that inserts it into the right position in the vector.
1376 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001377 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1378 // match the element type of the vector being created.
1379 if (Tmp2.getValueType() ==
1380 MVT::getVectorElementType(Op.getValueType())) {
1381 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1382 Tmp1.getValueType(), Tmp2);
1383
1384 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1385 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1386 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1387
1388 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1389 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1390 // elt 0 of the RHS.
1391 SmallVector<SDOperand, 8> ShufOps;
1392 for (unsigned i = 0; i != NumElts; ++i) {
1393 if (i != InsertPos->getValue())
1394 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1395 else
1396 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1397 }
1398 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1399 &ShufOps[0], ShufOps.size());
1400
1401 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1402 Tmp1, ScVec, ShufMask);
1403 Result = LegalizeOp(Result);
1404 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 }
1407
1408 // If the target doesn't support this, we have to spill the input vector
1409 // to a temporary stack slot, update the element, then reload it. This is
1410 // badness. We could also load the value into a vector register (either
1411 // with a "move to register" or "extload into register" instruction, then
1412 // permute it into place, if the idx is a constant and if the idx is
1413 // supported by the target.
1414 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001415 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 MVT::ValueType IdxVT = Tmp3.getValueType();
1417 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001418 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman12a9c082008-02-06 22:27:42 +00001419
Dan Gohman20e37962008-02-11 18:58:42 +00001420 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman12a9c082008-02-06 22:27:42 +00001421 int SPFI = StackPtrFI->getIndex();
1422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 // Store the vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001424 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001425 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001426 SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427
1428 // Truncate or zero extend offset to target pointer type.
1429 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1430 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1431 // Add the offset to the index.
1432 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1433 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1434 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1435 // Store the scalar value.
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001436 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1437 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 // Load the updated vector.
Dan Gohman12a9c082008-02-06 22:27:42 +00001439 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001440 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 break;
1442 }
1443 }
1444 break;
1445 case ISD::SCALAR_TO_VECTOR:
1446 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1447 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1448 break;
1449 }
1450
1451 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1452 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1453 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1454 Node->getValueType(0))) {
1455 default: assert(0 && "This action is not supported yet!");
1456 case TargetLowering::Legal:
1457 break;
1458 case TargetLowering::Custom:
1459 Tmp3 = TLI.LowerOperation(Result, DAG);
1460 if (Tmp3.Val) {
1461 Result = Tmp3;
1462 break;
1463 }
1464 // FALLTHROUGH
1465 case TargetLowering::Expand:
1466 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1467 break;
1468 }
1469 break;
1470 case ISD::VECTOR_SHUFFLE:
1471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1472 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1474
1475 // Allow targets to custom lower the SHUFFLEs they support.
1476 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1477 default: assert(0 && "Unknown operation action!");
1478 case TargetLowering::Legal:
1479 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1480 "vector shuffle should not be created if not legal!");
1481 break;
1482 case TargetLowering::Custom:
1483 Tmp3 = TLI.LowerOperation(Result, DAG);
1484 if (Tmp3.Val) {
1485 Result = Tmp3;
1486 break;
1487 }
1488 // FALLTHROUGH
1489 case TargetLowering::Expand: {
1490 MVT::ValueType VT = Node->getValueType(0);
1491 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1492 MVT::ValueType PtrVT = TLI.getPointerTy();
1493 SDOperand Mask = Node->getOperand(2);
1494 unsigned NumElems = Mask.getNumOperands();
1495 SmallVector<SDOperand,8> Ops;
1496 for (unsigned i = 0; i != NumElems; ++i) {
1497 SDOperand Arg = Mask.getOperand(i);
1498 if (Arg.getOpcode() == ISD::UNDEF) {
1499 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1500 } else {
1501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1502 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1503 if (Idx < NumElems)
1504 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1505 DAG.getConstant(Idx, PtrVT)));
1506 else
1507 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1508 DAG.getConstant(Idx - NumElems, PtrVT)));
1509 }
1510 }
1511 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1512 break;
1513 }
1514 case TargetLowering::Promote: {
1515 // Change base type to a different vector type.
1516 MVT::ValueType OVT = Node->getValueType(0);
1517 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1518
1519 // Cast the two input vectors.
1520 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1521 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1522
1523 // Convert the shuffle mask to the right # elements.
1524 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1525 assert(Tmp3.Val && "Shuffle not legal?");
1526 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1527 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1528 break;
1529 }
1530 }
1531 break;
1532
1533 case ISD::EXTRACT_VECTOR_ELT:
1534 Tmp1 = Node->getOperand(0);
1535 Tmp2 = LegalizeOp(Node->getOperand(1));
1536 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1537 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1538 break;
1539
1540 case ISD::EXTRACT_SUBVECTOR:
1541 Tmp1 = Node->getOperand(0);
1542 Tmp2 = LegalizeOp(Node->getOperand(1));
1543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1544 Result = ExpandEXTRACT_SUBVECTOR(Result);
1545 break;
1546
1547 case ISD::CALLSEQ_START: {
1548 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1549
1550 // Recursively Legalize all of the inputs of the call end that do not lead
1551 // to this call start. This ensures that any libcalls that need be inserted
1552 // are inserted *before* the CALLSEQ_START.
1553 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1554 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1555 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1556 NodesLeadingTo);
1557 }
1558
1559 // Now that we legalized all of the inputs (which may have inserted
1560 // libcalls) create the new CALLSEQ_START node.
1561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1562
1563 // Merge in the last call, to ensure that this call start after the last
1564 // call ended.
1565 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1566 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1567 Tmp1 = LegalizeOp(Tmp1);
1568 }
1569
1570 // Do not try to legalize the target-specific arguments (#1+).
1571 if (Tmp1 != Node->getOperand(0)) {
1572 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1573 Ops[0] = Tmp1;
1574 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1575 }
1576
1577 // Remember that the CALLSEQ_START is legalized.
1578 AddLegalizedOperand(Op.getValue(0), Result);
1579 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1580 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1581
1582 // Now that the callseq_start and all of the non-call nodes above this call
1583 // sequence have been legalized, legalize the call itself. During this
1584 // process, no libcalls can/will be inserted, guaranteeing that no calls
1585 // can overlap.
1586 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1587 SDOperand InCallSEQ = LastCALLSEQ_END;
1588 // Note that we are selecting this call!
1589 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1590 IsLegalizingCall = true;
1591
1592 // Legalize the call, starting from the CALLSEQ_END.
1593 LegalizeOp(LastCALLSEQ_END);
1594 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1595 return Result;
1596 }
1597 case ISD::CALLSEQ_END:
1598 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1599 // will cause this node to be legalized as well as handling libcalls right.
1600 if (LastCALLSEQ_END.Val != Node) {
1601 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001602 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 assert(I != LegalizedNodes.end() &&
1604 "Legalizing the call start should have legalized this node!");
1605 return I->second;
1606 }
1607
1608 // Otherwise, the call start has been legalized and everything is going
1609 // according to plan. Just legalize ourselves normally here.
1610 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1611 // Do not try to legalize the target-specific arguments (#1+), except for
1612 // an optional flag input.
1613 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1614 if (Tmp1 != Node->getOperand(0)) {
1615 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1616 Ops[0] = Tmp1;
1617 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1618 }
1619 } else {
1620 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1621 if (Tmp1 != Node->getOperand(0) ||
1622 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1623 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1624 Ops[0] = Tmp1;
1625 Ops.back() = Tmp2;
1626 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1627 }
1628 }
1629 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1630 // This finishes up call legalization.
1631 IsLegalizingCall = false;
1632
1633 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1634 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1635 if (Node->getNumValues() == 2)
1636 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1637 return Result.getValue(Op.ResNo);
1638 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001639 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1642 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1644
1645 Tmp1 = Result.getValue(0);
1646 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001647 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001648 default: assert(0 && "This action is not supported yet!");
1649 case TargetLowering::Expand: {
1650 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1651 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1652 " not tell us which reg is the stack pointer!");
1653 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001654
1655 // Chain the dynamic stack allocation so that it doesn't modify the stack
1656 // pointer when other instructions are using the stack.
1657 Chain = DAG.getCALLSEQ_START(Chain,
1658 DAG.getConstant(0, TLI.getPointerTy()));
1659
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001660 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001661 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1662 Chain = SP.getValue(1);
1663 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1664 unsigned StackAlign =
1665 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1666 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001667 SP = DAG.getNode(ISD::AND, VT, SP,
1668 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001669 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001670 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1671
1672 Tmp2 =
1673 DAG.getCALLSEQ_END(Chain,
1674 DAG.getConstant(0, TLI.getPointerTy()),
1675 DAG.getConstant(0, TLI.getPointerTy()),
1676 SDOperand());
1677
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 Tmp1 = LegalizeOp(Tmp1);
1679 Tmp2 = LegalizeOp(Tmp2);
1680 break;
1681 }
1682 case TargetLowering::Custom:
1683 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1684 if (Tmp3.Val) {
1685 Tmp1 = LegalizeOp(Tmp3);
1686 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1687 }
1688 break;
1689 case TargetLowering::Legal:
1690 break;
1691 }
1692 // Since this op produce two values, make sure to remember that we
1693 // legalized both of them.
1694 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1695 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1696 return Op.ResNo ? Tmp2 : Tmp1;
1697 }
1698 case ISD::INLINEASM: {
1699 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1700 bool Changed = false;
1701 // Legalize all of the operands of the inline asm, in case they are nodes
1702 // that need to be expanded or something. Note we skip the asm string and
1703 // all of the TargetConstant flags.
1704 SDOperand Op = LegalizeOp(Ops[0]);
1705 Changed = Op != Ops[0];
1706 Ops[0] = Op;
1707
1708 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1709 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1710 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1711 for (++i; NumVals; ++i, --NumVals) {
1712 SDOperand Op = LegalizeOp(Ops[i]);
1713 if (Op != Ops[i]) {
1714 Changed = true;
1715 Ops[i] = Op;
1716 }
1717 }
1718 }
1719
1720 if (HasInFlag) {
1721 Op = LegalizeOp(Ops.back());
1722 Changed |= Op != Ops.back();
1723 Ops.back() = Op;
1724 }
1725
1726 if (Changed)
1727 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1728
1729 // INLINE asm returns a chain and flag, make sure to add both to the map.
1730 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1731 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1732 return Result.getValue(Op.ResNo);
1733 }
1734 case ISD::BR:
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1736 // Ensure that libcalls are emitted before a branch.
1737 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1738 Tmp1 = LegalizeOp(Tmp1);
1739 LastCALLSEQ_END = DAG.getEntryNode();
1740
1741 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1742 break;
1743 case ISD::BRIND:
1744 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1745 // Ensure that libcalls are emitted before a branch.
1746 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1747 Tmp1 = LegalizeOp(Tmp1);
1748 LastCALLSEQ_END = DAG.getEntryNode();
1749
1750 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1751 default: assert(0 && "Indirect target must be legal type (pointer)!");
1752 case Legal:
1753 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1754 break;
1755 }
1756 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1757 break;
1758 case ISD::BR_JT:
1759 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1760 // Ensure that libcalls are emitted before a branch.
1761 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1762 Tmp1 = LegalizeOp(Tmp1);
1763 LastCALLSEQ_END = DAG.getEntryNode();
1764
1765 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1766 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1767
1768 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1769 default: assert(0 && "This action is not supported yet!");
1770 case TargetLowering::Legal: break;
1771 case TargetLowering::Custom:
1772 Tmp1 = TLI.LowerOperation(Result, DAG);
1773 if (Tmp1.Val) Result = Tmp1;
1774 break;
1775 case TargetLowering::Expand: {
1776 SDOperand Chain = Result.getOperand(0);
1777 SDOperand Table = Result.getOperand(1);
1778 SDOperand Index = Result.getOperand(2);
1779
1780 MVT::ValueType PTy = TLI.getPointerTy();
1781 MachineFunction &MF = DAG.getMachineFunction();
1782 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1783 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1784 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1785
1786 SDOperand LD;
1787 switch (EntrySize) {
1788 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001789 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001790 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001791 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001792 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793 }
1794
Evan Cheng6fb06762007-11-09 01:32:10 +00001795 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1797 // For PIC, the sequence is:
1798 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001799 // RelocBase can be JumpTable, GOT or some sort of global base.
1800 if (PTy != MVT::i32)
1801 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1802 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1803 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001804 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001805 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806 }
1807 }
1808 break;
1809 case ISD::BRCOND:
1810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1811 // Ensure that libcalls are emitted before a return.
1812 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1813 Tmp1 = LegalizeOp(Tmp1);
1814 LastCALLSEQ_END = DAG.getEntryNode();
1815
1816 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1817 case Expand: assert(0 && "It's impossible to expand bools");
1818 case Legal:
1819 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1820 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001821 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1823
1824 // The top bits of the promoted condition are not necessarily zero, ensure
1825 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001826 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001827 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001828 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1830 break;
1831 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001832 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833
1834 // Basic block destination (Op#2) is always legal.
1835 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1836
1837 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1838 default: assert(0 && "This action is not supported yet!");
1839 case TargetLowering::Legal: break;
1840 case TargetLowering::Custom:
1841 Tmp1 = TLI.LowerOperation(Result, DAG);
1842 if (Tmp1.Val) Result = Tmp1;
1843 break;
1844 case TargetLowering::Expand:
1845 // Expand brcond's setcc into its constituent parts and create a BR_CC
1846 // Node.
1847 if (Tmp2.getOpcode() == ISD::SETCC) {
1848 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1849 Tmp2.getOperand(0), Tmp2.getOperand(1),
1850 Node->getOperand(2));
1851 } else {
1852 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1853 DAG.getCondCode(ISD::SETNE), Tmp2,
1854 DAG.getConstant(0, Tmp2.getValueType()),
1855 Node->getOperand(2));
1856 }
1857 break;
1858 }
1859 break;
1860 case ISD::BR_CC:
1861 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1862 // Ensure that libcalls are emitted before a branch.
1863 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1864 Tmp1 = LegalizeOp(Tmp1);
1865 Tmp2 = Node->getOperand(2); // LHS
1866 Tmp3 = Node->getOperand(3); // RHS
1867 Tmp4 = Node->getOperand(1); // CC
1868
1869 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1870 LastCALLSEQ_END = DAG.getEntryNode();
1871
1872 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1873 // the LHS is a legal SETCC itself. In this case, we need to compare
1874 // the result against zero to select between true and false values.
1875 if (Tmp3.Val == 0) {
1876 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1877 Tmp4 = DAG.getCondCode(ISD::SETNE);
1878 }
1879
1880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1881 Node->getOperand(4));
1882
1883 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1884 default: assert(0 && "Unexpected action for BR_CC!");
1885 case TargetLowering::Legal: break;
1886 case TargetLowering::Custom:
1887 Tmp4 = TLI.LowerOperation(Result, DAG);
1888 if (Tmp4.Val) Result = Tmp4;
1889 break;
1890 }
1891 break;
1892 case ISD::LOAD: {
1893 LoadSDNode *LD = cast<LoadSDNode>(Node);
1894 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1895 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1896
1897 ISD::LoadExtType ExtType = LD->getExtensionType();
1898 if (ExtType == ISD::NON_EXTLOAD) {
1899 MVT::ValueType VT = Node->getValueType(0);
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1901 Tmp3 = Result.getValue(0);
1902 Tmp4 = Result.getValue(1);
1903
1904 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1905 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001906 case TargetLowering::Legal:
1907 // If this is an unaligned load and the target doesn't support it,
1908 // expand it.
1909 if (!TLI.allowsUnalignedMemoryAccesses()) {
1910 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001911 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001912 if (LD->getAlignment() < ABIAlignment){
1913 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1914 TLI);
1915 Tmp3 = Result.getOperand(0);
1916 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001917 Tmp3 = LegalizeOp(Tmp3);
1918 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001919 }
1920 }
1921 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001922 case TargetLowering::Custom:
1923 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1924 if (Tmp1.Val) {
1925 Tmp3 = LegalizeOp(Tmp1);
1926 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1927 }
1928 break;
1929 case TargetLowering::Promote: {
1930 // Only promote a load of vector type to another.
1931 assert(MVT::isVector(VT) && "Cannot promote this load!");
1932 // Change base type to a different vector type.
1933 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1934
1935 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1936 LD->getSrcValueOffset(),
1937 LD->isVolatile(), LD->getAlignment());
1938 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1939 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1940 break;
1941 }
1942 }
1943 // Since loads produce two values, make sure to remember that we
1944 // legalized both of them.
1945 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1946 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1947 return Op.ResNo ? Tmp4 : Tmp3;
1948 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001949 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands082524c2008-01-23 20:39:46 +00001950 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1951 int SVOffset = LD->getSrcValueOffset();
1952 unsigned Alignment = LD->getAlignment();
1953 bool isVolatile = LD->isVolatile();
1954
1955 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1956 // Some targets pretend to have an i1 loading operation, and actually
1957 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1958 // bits are guaranteed to be zero; it helps the optimizers understand
1959 // that these bits are zero. It is also useful for EXTLOAD, since it
1960 // tells the optimizers that those bits are undefined. It would be
1961 // nice to have an effective generic way of getting these benefits...
1962 // Until such a way is found, don't insist on promoting i1 here.
1963 (SrcVT != MVT::i1 ||
1964 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1965 // Promote to a byte-sized load if not loading an integral number of
1966 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1967 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1968 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1969 SDOperand Ch;
1970
1971 // The extra bits are guaranteed to be zero, since we stored them that
1972 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1973
1974 ISD::LoadExtType NewExtType =
1975 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1976
1977 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1978 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1979 NVT, isVolatile, Alignment);
1980
1981 Ch = Result.getValue(1); // The chain.
1982
1983 if (ExtType == ISD::SEXTLOAD)
1984 // Having the top bits zero doesn't help when sign extending.
1985 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1986 Result, DAG.getValueType(SrcVT));
1987 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1988 // All the top bits are guaranteed to be zero - inform the optimizers.
1989 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1990 DAG.getValueType(SrcVT));
1991
1992 Tmp1 = LegalizeOp(Result);
1993 Tmp2 = LegalizeOp(Ch);
1994 } else if (SrcWidth & (SrcWidth - 1)) {
1995 // If not loading a power-of-2 number of bits, expand as two loads.
1996 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1997 "Unsupported extload!");
1998 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1999 assert(RoundWidth < SrcWidth);
2000 unsigned ExtraWidth = SrcWidth - RoundWidth;
2001 assert(ExtraWidth < RoundWidth);
2002 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2003 "Load size not an integral number of bytes!");
2004 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2005 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2006 SDOperand Lo, Hi, Ch;
2007 unsigned IncrementSize;
2008
2009 if (TLI.isLittleEndian()) {
2010 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2011 // Load the bottom RoundWidth bits.
2012 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2013 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2014 Alignment);
2015
2016 // Load the remaining ExtraWidth bits.
2017 IncrementSize = RoundWidth / 8;
2018 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2019 DAG.getIntPtrConstant(IncrementSize));
2020 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2021 LD->getSrcValue(), SVOffset + IncrementSize,
2022 ExtraVT, isVolatile,
2023 MinAlign(Alignment, IncrementSize));
2024
2025 // Build a factor node to remember that this load is independent of the
2026 // other one.
2027 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2028 Hi.getValue(1));
2029
2030 // Move the top bits to the right place.
2031 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2032 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2033
2034 // Join the hi and lo parts.
2035 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002036 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002037 // Big endian - avoid unaligned loads.
2038 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2039 // Load the top RoundWidth bits.
2040 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2041 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2042 Alignment);
2043
2044 // Load the remaining ExtraWidth bits.
2045 IncrementSize = RoundWidth / 8;
2046 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2047 DAG.getIntPtrConstant(IncrementSize));
2048 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2049 LD->getSrcValue(), SVOffset + IncrementSize,
2050 ExtraVT, isVolatile,
2051 MinAlign(Alignment, IncrementSize));
2052
2053 // Build a factor node to remember that this load is independent of the
2054 // other one.
2055 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2056 Hi.getValue(1));
2057
2058 // Move the top bits to the right place.
2059 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2060 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2061
2062 // Join the hi and lo parts.
2063 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2064 }
2065
2066 Tmp1 = LegalizeOp(Result);
2067 Tmp2 = LegalizeOp(Ch);
2068 } else {
2069 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2070 default: assert(0 && "This action is not supported yet!");
2071 case TargetLowering::Custom:
2072 isCustom = true;
2073 // FALLTHROUGH
2074 case TargetLowering::Legal:
2075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2076 Tmp1 = Result.getValue(0);
2077 Tmp2 = Result.getValue(1);
2078
2079 if (isCustom) {
2080 Tmp3 = TLI.LowerOperation(Result, DAG);
2081 if (Tmp3.Val) {
2082 Tmp1 = LegalizeOp(Tmp3);
2083 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2084 }
2085 } else {
2086 // If this is an unaligned load and the target doesn't support it,
2087 // expand it.
2088 if (!TLI.allowsUnalignedMemoryAccesses()) {
2089 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002090 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands082524c2008-01-23 20:39:46 +00002091 if (LD->getAlignment() < ABIAlignment){
2092 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2093 TLI);
2094 Tmp1 = Result.getOperand(0);
2095 Tmp2 = Result.getOperand(1);
2096 Tmp1 = LegalizeOp(Tmp1);
2097 Tmp2 = LegalizeOp(Tmp2);
2098 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002099 }
2100 }
Duncan Sands082524c2008-01-23 20:39:46 +00002101 break;
2102 case TargetLowering::Expand:
2103 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2104 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2105 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2106 LD->getSrcValueOffset(),
2107 LD->isVolatile(), LD->getAlignment());
2108 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2109 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2110 Tmp2 = LegalizeOp(Load.getValue(1));
2111 break;
2112 }
2113 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2114 // Turn the unsupported load into an EXTLOAD followed by an explicit
2115 // zero/sign extend inreg.
2116 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2117 Tmp1, Tmp2, LD->getSrcValue(),
2118 LD->getSrcValueOffset(), SrcVT,
2119 LD->isVolatile(), LD->getAlignment());
2120 SDOperand ValRes;
2121 if (ExtType == ISD::SEXTLOAD)
2122 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2123 Result, DAG.getValueType(SrcVT));
2124 else
2125 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2126 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2127 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002128 break;
2129 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 }
Duncan Sands082524c2008-01-23 20:39:46 +00002131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 // Since loads produce two values, make sure to remember that we legalized
2133 // both of them.
2134 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2135 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2136 return Op.ResNo ? Tmp2 : Tmp1;
2137 }
2138 }
2139 case ISD::EXTRACT_ELEMENT: {
2140 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2141 switch (getTypeAction(OpTy)) {
2142 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2143 case Legal:
2144 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2145 // 1 -> Hi
2146 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2147 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2148 TLI.getShiftAmountTy()));
2149 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2150 } else {
2151 // 0 -> Lo
2152 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2153 Node->getOperand(0));
2154 }
2155 break;
2156 case Expand:
2157 // Get both the low and high parts.
2158 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2159 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2160 Result = Tmp2; // 1 -> Hi
2161 else
2162 Result = Tmp1; // 0 -> Lo
2163 break;
2164 }
2165 break;
2166 }
2167
2168 case ISD::CopyToReg:
2169 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2170
2171 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2172 "Register type must be legal!");
2173 // Legalize the incoming value (must be a legal type).
2174 Tmp2 = LegalizeOp(Node->getOperand(2));
2175 if (Node->getNumValues() == 1) {
2176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2177 } else {
2178 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2179 if (Node->getNumOperands() == 4) {
2180 Tmp3 = LegalizeOp(Node->getOperand(3));
2181 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2182 Tmp3);
2183 } else {
2184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2185 }
2186
2187 // Since this produces two values, make sure to remember that we legalized
2188 // both of them.
2189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2191 return Result;
2192 }
2193 break;
2194
2195 case ISD::RET:
2196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2197
2198 // Ensure that libcalls are emitted before a return.
2199 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2200 Tmp1 = LegalizeOp(Tmp1);
2201 LastCALLSEQ_END = DAG.getEntryNode();
2202
2203 switch (Node->getNumOperands()) {
2204 case 3: // ret val
2205 Tmp2 = Node->getOperand(1);
2206 Tmp3 = Node->getOperand(2); // Signness
2207 switch (getTypeAction(Tmp2.getValueType())) {
2208 case Legal:
2209 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2210 break;
2211 case Expand:
2212 if (!MVT::isVector(Tmp2.getValueType())) {
2213 SDOperand Lo, Hi;
2214 ExpandOp(Tmp2, Lo, Hi);
2215
2216 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002217 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 std::swap(Lo, Hi);
2219
2220 if (Hi.Val)
2221 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2222 else
2223 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2224 Result = LegalizeOp(Result);
2225 } else {
2226 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002227 int InIx = Tmp2.ResNo;
2228 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2229 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002230
2231 // Figure out if there is a simple type corresponding to this Vector
2232 // type. If so, convert to the vector type.
2233 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2234 if (TLI.isTypeLegal(TVT)) {
2235 // Turn this into a return of the vector type.
2236 Tmp2 = LegalizeOp(Tmp2);
2237 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2238 } else if (NumElems == 1) {
2239 // Turn this into a return of the scalar type.
2240 Tmp2 = ScalarizeVectorOp(Tmp2);
2241 Tmp2 = LegalizeOp(Tmp2);
2242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2243
2244 // FIXME: Returns of gcc generic vectors smaller than a legal type
2245 // should be returned in integer registers!
2246
2247 // The scalarized value type may not be legal, e.g. it might require
2248 // promotion or expansion. Relegalize the return.
2249 Result = LegalizeOp(Result);
2250 } else {
2251 // FIXME: Returns of gcc generic vectors larger than a legal vector
2252 // type should be returned by reference!
2253 SDOperand Lo, Hi;
2254 SplitVectorOp(Tmp2, Lo, Hi);
2255 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2256 Result = LegalizeOp(Result);
2257 }
2258 }
2259 break;
2260 case Promote:
2261 Tmp2 = PromoteOp(Node->getOperand(1));
2262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2263 Result = LegalizeOp(Result);
2264 break;
2265 }
2266 break;
2267 case 1: // ret void
2268 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2269 break;
2270 default: { // ret <values>
2271 SmallVector<SDOperand, 8> NewValues;
2272 NewValues.push_back(Tmp1);
2273 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2274 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2275 case Legal:
2276 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2277 NewValues.push_back(Node->getOperand(i+1));
2278 break;
2279 case Expand: {
2280 SDOperand Lo, Hi;
2281 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
2282 "FIXME: TODO: implement returning non-legal vector types!");
2283 ExpandOp(Node->getOperand(i), Lo, Hi);
2284 NewValues.push_back(Lo);
2285 NewValues.push_back(Node->getOperand(i+1));
2286 if (Hi.Val) {
2287 NewValues.push_back(Hi);
2288 NewValues.push_back(Node->getOperand(i+1));
2289 }
2290 break;
2291 }
2292 case Promote:
2293 assert(0 && "Can't promote multiple return value yet!");
2294 }
2295
2296 if (NewValues.size() == Node->getNumOperands())
2297 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2298 else
2299 Result = DAG.getNode(ISD::RET, MVT::Other,
2300 &NewValues[0], NewValues.size());
2301 break;
2302 }
2303 }
2304
2305 if (Result.getOpcode() == ISD::RET) {
2306 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2307 default: assert(0 && "This action is not supported yet!");
2308 case TargetLowering::Legal: break;
2309 case TargetLowering::Custom:
2310 Tmp1 = TLI.LowerOperation(Result, DAG);
2311 if (Tmp1.Val) Result = Tmp1;
2312 break;
2313 }
2314 }
2315 break;
2316 case ISD::STORE: {
2317 StoreSDNode *ST = cast<StoreSDNode>(Node);
2318 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2319 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2320 int SVOffset = ST->getSrcValueOffset();
2321 unsigned Alignment = ST->getAlignment();
2322 bool isVolatile = ST->isVolatile();
2323
2324 if (!ST->isTruncatingStore()) {
2325 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2326 // FIXME: We shouldn't do this for TargetConstantFP's.
2327 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2328 // to phase ordering between legalized code and the dag combiner. This
2329 // probably means that we need to integrate dag combiner and legalizer
2330 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002331 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002332 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002333 if (CFP->getValueType(0) == MVT::f32 &&
2334 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002335 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2336 convertToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002337 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002338 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2339 SVOffset, isVolatile, Alignment);
2340 break;
2341 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002342 // If this target supports 64-bit registers, do a single 64-bit store.
2343 if (getTypeAction(MVT::i64) == Legal) {
2344 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002345 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002346 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2347 SVOffset, isVolatile, Alignment);
2348 break;
2349 } else if (getTypeAction(MVT::i32) == Legal) {
2350 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2351 // stores. If the target supports neither 32- nor 64-bits, this
2352 // xform is certainly not worth it.
Dan Gohman39509762008-03-11 00:11:06 +00002353 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
2354 SDOperand Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2355 SDOperand Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002356 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002357
2358 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2359 SVOffset, isVolatile, Alignment);
2360 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002361 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002362 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002363 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002364
2365 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2366 break;
2367 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002368 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 }
2370
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002371 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002372 case Legal: {
2373 Tmp3 = LegalizeOp(ST->getValue());
2374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2375 ST->getOffset());
2376
2377 MVT::ValueType VT = Tmp3.getValueType();
2378 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2379 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002380 case TargetLowering::Legal:
2381 // If this is an unaligned store and the target doesn't support it,
2382 // expand it.
2383 if (!TLI.allowsUnalignedMemoryAccesses()) {
2384 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002385 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002386 if (ST->getAlignment() < ABIAlignment)
2387 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2388 TLI);
2389 }
2390 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 case TargetLowering::Custom:
2392 Tmp1 = TLI.LowerOperation(Result, DAG);
2393 if (Tmp1.Val) Result = Tmp1;
2394 break;
2395 case TargetLowering::Promote:
2396 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2397 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2398 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2399 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2400 ST->getSrcValue(), SVOffset, isVolatile,
2401 Alignment);
2402 break;
2403 }
2404 break;
2405 }
2406 case Promote:
2407 // Truncate the value and store the result.
2408 Tmp3 = PromoteOp(ST->getValue());
2409 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002410 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002411 isVolatile, Alignment);
2412 break;
2413
2414 case Expand:
2415 unsigned IncrementSize = 0;
2416 SDOperand Lo, Hi;
2417
2418 // If this is a vector type, then we have to calculate the increment as
2419 // the product of the element size in bytes, and the number of elements
2420 // in the high half of the vector.
2421 if (MVT::isVector(ST->getValue().getValueType())) {
2422 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002423 int InIx = ST->getValue().ResNo;
Chris Lattner5872a362008-01-17 07:00:52 +00002424 MVT::ValueType InVT = InVal->getValueType(InIx);
2425 unsigned NumElems = MVT::getVectorNumElements(InVT);
2426 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427
2428 // Figure out if there is a simple type corresponding to this Vector
2429 // type. If so, convert to the vector type.
2430 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2431 if (TLI.isTypeLegal(TVT)) {
2432 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002433 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2435 SVOffset, isVolatile, Alignment);
2436 Result = LegalizeOp(Result);
2437 break;
2438 } else if (NumElems == 1) {
2439 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002440 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2442 SVOffset, isVolatile, Alignment);
2443 // The scalarized value type may not be legal, e.g. it might require
2444 // promotion or expansion. Relegalize the scalar store.
2445 Result = LegalizeOp(Result);
2446 break;
2447 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002448 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00002449 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2450 MVT::getSizeInBits(EVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 }
2452 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002453 ExpandOp(ST->getValue(), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2455
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002456 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 std::swap(Lo, Hi);
2458 }
2459
2460 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2461 SVOffset, isVolatile, Alignment);
2462
2463 if (Hi.Val == NULL) {
2464 // Must be int <-> float one-to-one expansion.
2465 Result = Lo;
2466 break;
2467 }
2468
2469 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002470 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 assert(isTypeLegal(Tmp2.getValueType()) &&
2472 "Pointers must be legal!");
2473 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002474 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2476 SVOffset, isVolatile, Alignment);
2477 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2478 break;
2479 }
2480 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002481 switch (getTypeAction(ST->getValue().getValueType())) {
2482 case Legal:
2483 Tmp3 = LegalizeOp(ST->getValue());
2484 break;
2485 case Promote:
2486 // We can promote the value, the truncstore will still take care of it.
2487 Tmp3 = PromoteOp(ST->getValue());
2488 break;
2489 case Expand:
2490 // Just store the low part. This may become a non-trunc store, so make
2491 // sure to use getTruncStore, not UpdateNodeOperands below.
2492 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2493 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2494 SVOffset, MVT::i8, isVolatile, Alignment);
2495 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002497 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands40676662008-01-22 07:17:34 +00002498 unsigned StWidth = MVT::getSizeInBits(StVT);
2499
2500 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2501 // Promote to a byte-sized store with upper bits zero if not
2502 // storing an integral number of bytes. For example, promote
2503 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2504 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2505 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2506 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2507 SVOffset, NVT, isVolatile, Alignment);
2508 } else if (StWidth & (StWidth - 1)) {
2509 // If not storing a power-of-2 number of bits, expand as two stores.
2510 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2511 "Unsupported truncstore!");
2512 unsigned RoundWidth = 1 << Log2_32(StWidth);
2513 assert(RoundWidth < StWidth);
2514 unsigned ExtraWidth = StWidth - RoundWidth;
2515 assert(ExtraWidth < RoundWidth);
2516 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2517 "Store size not an integral number of bytes!");
2518 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2519 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2520 SDOperand Lo, Hi;
2521 unsigned IncrementSize;
2522
2523 if (TLI.isLittleEndian()) {
2524 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2525 // Store the bottom RoundWidth bits.
2526 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2527 SVOffset, RoundVT,
2528 isVolatile, Alignment);
2529
2530 // Store the remaining ExtraWidth bits.
2531 IncrementSize = RoundWidth / 8;
2532 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2533 DAG.getIntPtrConstant(IncrementSize));
2534 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2535 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2536 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2537 SVOffset + IncrementSize, ExtraVT, isVolatile,
2538 MinAlign(Alignment, IncrementSize));
2539 } else {
2540 // Big endian - avoid unaligned stores.
2541 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2542 // Store the top RoundWidth bits.
2543 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2544 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2545 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2546 RoundVT, isVolatile, Alignment);
2547
2548 // Store the remaining ExtraWidth bits.
2549 IncrementSize = RoundWidth / 8;
2550 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2551 DAG.getIntPtrConstant(IncrementSize));
2552 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2553 SVOffset + IncrementSize, ExtraVT, isVolatile,
2554 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002555 }
Duncan Sands40676662008-01-22 07:17:34 +00002556
2557 // The order of the stores doesn't matter.
2558 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2559 } else {
2560 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2561 Tmp2 != ST->getBasePtr())
2562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2563 ST->getOffset());
2564
2565 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2566 default: assert(0 && "This action is not supported yet!");
2567 case TargetLowering::Legal:
2568 // If this is an unaligned store and the target doesn't support it,
2569 // expand it.
2570 if (!TLI.allowsUnalignedMemoryAccesses()) {
2571 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002572 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands40676662008-01-22 07:17:34 +00002573 if (ST->getAlignment() < ABIAlignment)
2574 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2575 TLI);
2576 }
2577 break;
2578 case TargetLowering::Custom:
2579 Result = TLI.LowerOperation(Result, DAG);
2580 break;
2581 case Expand:
2582 // TRUNCSTORE:i16 i32 -> STORE i16
2583 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2584 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2585 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2586 isVolatile, Alignment);
2587 break;
2588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 }
2590 }
2591 break;
2592 }
2593 case ISD::PCMARKER:
2594 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2596 break;
2597 case ISD::STACKSAVE:
2598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2599 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2600 Tmp1 = Result.getValue(0);
2601 Tmp2 = Result.getValue(1);
2602
2603 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2604 default: assert(0 && "This action is not supported yet!");
2605 case TargetLowering::Legal: break;
2606 case TargetLowering::Custom:
2607 Tmp3 = TLI.LowerOperation(Result, DAG);
2608 if (Tmp3.Val) {
2609 Tmp1 = LegalizeOp(Tmp3);
2610 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2611 }
2612 break;
2613 case TargetLowering::Expand:
2614 // Expand to CopyFromReg if the target set
2615 // StackPointerRegisterToSaveRestore.
2616 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2617 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2618 Node->getValueType(0));
2619 Tmp2 = Tmp1.getValue(1);
2620 } else {
2621 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2622 Tmp2 = Node->getOperand(0);
2623 }
2624 break;
2625 }
2626
2627 // Since stacksave produce two values, make sure to remember that we
2628 // legalized both of them.
2629 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2630 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2631 return Op.ResNo ? Tmp2 : Tmp1;
2632
2633 case ISD::STACKRESTORE:
2634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2637
2638 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2639 default: assert(0 && "This action is not supported yet!");
2640 case TargetLowering::Legal: break;
2641 case TargetLowering::Custom:
2642 Tmp1 = TLI.LowerOperation(Result, DAG);
2643 if (Tmp1.Val) Result = Tmp1;
2644 break;
2645 case TargetLowering::Expand:
2646 // Expand to CopyToReg if the target set
2647 // StackPointerRegisterToSaveRestore.
2648 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2649 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2650 } else {
2651 Result = Tmp1;
2652 }
2653 break;
2654 }
2655 break;
2656
2657 case ISD::READCYCLECOUNTER:
2658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2659 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2660 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2661 Node->getValueType(0))) {
2662 default: assert(0 && "This action is not supported yet!");
2663 case TargetLowering::Legal:
2664 Tmp1 = Result.getValue(0);
2665 Tmp2 = Result.getValue(1);
2666 break;
2667 case TargetLowering::Custom:
2668 Result = TLI.LowerOperation(Result, DAG);
2669 Tmp1 = LegalizeOp(Result.getValue(0));
2670 Tmp2 = LegalizeOp(Result.getValue(1));
2671 break;
2672 }
2673
2674 // Since rdcc produce two values, make sure to remember that we legalized
2675 // both of them.
2676 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2677 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2678 return Result;
2679
2680 case ISD::SELECT:
2681 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2682 case Expand: assert(0 && "It's impossible to expand bools");
2683 case Legal:
2684 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2685 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002686 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2688 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002689 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002690 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002691 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2693 break;
2694 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002695 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002696 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2697 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2698
2699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2700
2701 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2702 default: assert(0 && "This action is not supported yet!");
2703 case TargetLowering::Legal: break;
2704 case TargetLowering::Custom: {
2705 Tmp1 = TLI.LowerOperation(Result, DAG);
2706 if (Tmp1.Val) Result = Tmp1;
2707 break;
2708 }
2709 case TargetLowering::Expand:
2710 if (Tmp1.getOpcode() == ISD::SETCC) {
2711 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2712 Tmp2, Tmp3,
2713 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2714 } else {
2715 Result = DAG.getSelectCC(Tmp1,
2716 DAG.getConstant(0, Tmp1.getValueType()),
2717 Tmp2, Tmp3, ISD::SETNE);
2718 }
2719 break;
2720 case TargetLowering::Promote: {
2721 MVT::ValueType NVT =
2722 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2723 unsigned ExtOp, TruncOp;
2724 if (MVT::isVector(Tmp2.getValueType())) {
2725 ExtOp = ISD::BIT_CONVERT;
2726 TruncOp = ISD::BIT_CONVERT;
2727 } else if (MVT::isInteger(Tmp2.getValueType())) {
2728 ExtOp = ISD::ANY_EXTEND;
2729 TruncOp = ISD::TRUNCATE;
2730 } else {
2731 ExtOp = ISD::FP_EXTEND;
2732 TruncOp = ISD::FP_ROUND;
2733 }
2734 // Promote each of the values to the new type.
2735 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2736 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2737 // Perform the larger operation, then round down.
2738 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002739 if (TruncOp != ISD::FP_ROUND)
2740 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2741 else
2742 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2743 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 break;
2745 }
2746 }
2747 break;
2748 case ISD::SELECT_CC: {
2749 Tmp1 = Node->getOperand(0); // LHS
2750 Tmp2 = Node->getOperand(1); // RHS
2751 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2752 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2753 SDOperand CC = Node->getOperand(4);
2754
2755 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2756
2757 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2758 // the LHS is a legal SETCC itself. In this case, we need to compare
2759 // the result against zero to select between true and false values.
2760 if (Tmp2.Val == 0) {
2761 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2762 CC = DAG.getCondCode(ISD::SETNE);
2763 }
2764 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2765
2766 // Everything is legal, see if we should expand this op or something.
2767 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2768 default: assert(0 && "This action is not supported yet!");
2769 case TargetLowering::Legal: break;
2770 case TargetLowering::Custom:
2771 Tmp1 = TLI.LowerOperation(Result, DAG);
2772 if (Tmp1.Val) Result = Tmp1;
2773 break;
2774 }
2775 break;
2776 }
2777 case ISD::SETCC:
2778 Tmp1 = Node->getOperand(0);
2779 Tmp2 = Node->getOperand(1);
2780 Tmp3 = Node->getOperand(2);
2781 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2782
2783 // If we had to Expand the SetCC operands into a SELECT node, then it may
2784 // not always be possible to return a true LHS & RHS. In this case, just
2785 // return the value we legalized, returned in the LHS
2786 if (Tmp2.Val == 0) {
2787 Result = Tmp1;
2788 break;
2789 }
2790
2791 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2792 default: assert(0 && "Cannot handle this action for SETCC yet!");
2793 case TargetLowering::Custom:
2794 isCustom = true;
2795 // FALLTHROUGH.
2796 case TargetLowering::Legal:
2797 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2798 if (isCustom) {
2799 Tmp4 = TLI.LowerOperation(Result, DAG);
2800 if (Tmp4.Val) Result = Tmp4;
2801 }
2802 break;
2803 case TargetLowering::Promote: {
2804 // First step, figure out the appropriate operation to use.
2805 // Allow SETCC to not be supported for all legal data types
2806 // Mostly this targets FP
2807 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2808 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2809
2810 // Scan for the appropriate larger type to use.
2811 while (1) {
2812 NewInTy = (MVT::ValueType)(NewInTy+1);
2813
2814 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2815 "Fell off of the edge of the integer world");
2816 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2817 "Fell off of the edge of the floating point world");
2818
2819 // If the target supports SETCC of this type, use it.
2820 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2821 break;
2822 }
2823 if (MVT::isInteger(NewInTy))
2824 assert(0 && "Cannot promote Legal Integer SETCC yet");
2825 else {
2826 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2827 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2828 }
2829 Tmp1 = LegalizeOp(Tmp1);
2830 Tmp2 = LegalizeOp(Tmp2);
2831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2832 Result = LegalizeOp(Result);
2833 break;
2834 }
2835 case TargetLowering::Expand:
2836 // Expand a setcc node into a select_cc of the same condition, lhs, and
2837 // rhs that selects between const 1 (true) and const 0 (false).
2838 MVT::ValueType VT = Node->getValueType(0);
2839 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2840 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2841 Tmp3);
2842 break;
2843 }
2844 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845
2846 case ISD::SHL_PARTS:
2847 case ISD::SRA_PARTS:
2848 case ISD::SRL_PARTS: {
2849 SmallVector<SDOperand, 8> Ops;
2850 bool Changed = false;
2851 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2852 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2853 Changed |= Ops.back() != Node->getOperand(i);
2854 }
2855 if (Changed)
2856 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2857
2858 switch (TLI.getOperationAction(Node->getOpcode(),
2859 Node->getValueType(0))) {
2860 default: assert(0 && "This action is not supported yet!");
2861 case TargetLowering::Legal: break;
2862 case TargetLowering::Custom:
2863 Tmp1 = TLI.LowerOperation(Result, DAG);
2864 if (Tmp1.Val) {
2865 SDOperand Tmp2, RetVal(0, 0);
2866 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2867 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2868 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2869 if (i == Op.ResNo)
2870 RetVal = Tmp2;
2871 }
2872 assert(RetVal.Val && "Illegal result number");
2873 return RetVal;
2874 }
2875 break;
2876 }
2877
2878 // Since these produce multiple values, make sure to remember that we
2879 // legalized all of them.
2880 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2881 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2882 return Result.getValue(Op.ResNo);
2883 }
2884
2885 // Binary operators
2886 case ISD::ADD:
2887 case ISD::SUB:
2888 case ISD::MUL:
2889 case ISD::MULHS:
2890 case ISD::MULHU:
2891 case ISD::UDIV:
2892 case ISD::SDIV:
2893 case ISD::AND:
2894 case ISD::OR:
2895 case ISD::XOR:
2896 case ISD::SHL:
2897 case ISD::SRL:
2898 case ISD::SRA:
2899 case ISD::FADD:
2900 case ISD::FSUB:
2901 case ISD::FMUL:
2902 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002903 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2905 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2906 case Expand: assert(0 && "Not possible");
2907 case Legal:
2908 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2909 break;
2910 case Promote:
2911 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2912 break;
2913 }
2914
2915 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2916
2917 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2918 default: assert(0 && "BinOp legalize operation not supported");
2919 case TargetLowering::Legal: break;
2920 case TargetLowering::Custom:
2921 Tmp1 = TLI.LowerOperation(Result, DAG);
2922 if (Tmp1.Val) Result = Tmp1;
2923 break;
2924 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00002925 MVT::ValueType VT = Op.getValueType();
2926
2927 // See if multiply or divide can be lowered using two-result operations.
2928 SDVTList VTs = DAG.getVTList(VT, VT);
2929 if (Node->getOpcode() == ISD::MUL) {
2930 // We just need the low half of the multiply; try both the signed
2931 // and unsigned forms. If the target supports both SMUL_LOHI and
2932 // UMUL_LOHI, form a preference by checking which forms of plain
2933 // MULH it supports.
2934 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2935 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2936 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2937 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2938 unsigned OpToUse = 0;
2939 if (HasSMUL_LOHI && !HasMULHS) {
2940 OpToUse = ISD::SMUL_LOHI;
2941 } else if (HasUMUL_LOHI && !HasMULHU) {
2942 OpToUse = ISD::UMUL_LOHI;
2943 } else if (HasSMUL_LOHI) {
2944 OpToUse = ISD::SMUL_LOHI;
2945 } else if (HasUMUL_LOHI) {
2946 OpToUse = ISD::UMUL_LOHI;
2947 }
2948 if (OpToUse) {
2949 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2950 break;
2951 }
2952 }
2953 if (Node->getOpcode() == ISD::MULHS &&
2954 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2955 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2956 break;
2957 }
2958 if (Node->getOpcode() == ISD::MULHU &&
2959 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2960 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2961 break;
2962 }
2963 if (Node->getOpcode() == ISD::SDIV &&
2964 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2965 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2966 break;
2967 }
2968 if (Node->getOpcode() == ISD::UDIV &&
2969 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2970 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2971 break;
2972 }
2973
Dan Gohman6d05cac2007-10-11 23:57:53 +00002974 // Check to see if we have a libcall for this operator.
2975 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2976 bool isSigned = false;
2977 switch (Node->getOpcode()) {
2978 case ISD::UDIV:
2979 case ISD::SDIV:
2980 if (VT == MVT::i32) {
2981 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00002983 isSigned = Node->getOpcode() == ISD::SDIV;
2984 }
2985 break;
2986 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00002987 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2988 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00002989 break;
2990 default: break;
2991 }
2992 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2993 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00002994 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 break;
2996 }
2997
2998 assert(MVT::isVector(Node->getValueType(0)) &&
2999 "Cannot expand this binary operator!");
3000 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003001 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 break;
3003 }
3004 case TargetLowering::Promote: {
3005 switch (Node->getOpcode()) {
3006 default: assert(0 && "Do not know how to promote this BinOp!");
3007 case ISD::AND:
3008 case ISD::OR:
3009 case ISD::XOR: {
3010 MVT::ValueType OVT = Node->getValueType(0);
3011 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3012 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3013 // Bit convert each of the values to the new type.
3014 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3015 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3016 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3017 // Bit convert the result back the original type.
3018 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3019 break;
3020 }
3021 }
3022 }
3023 }
3024 break;
3025
Dan Gohman475cd732007-10-05 14:17:22 +00003026 case ISD::SMUL_LOHI:
3027 case ISD::UMUL_LOHI:
3028 case ISD::SDIVREM:
3029 case ISD::UDIVREM:
3030 // These nodes will only be produced by target-specific lowering, so
3031 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003032 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003033 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003034
3035 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3036 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3037 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003038 break;
3039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3041 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3042 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3043 case Expand: assert(0 && "Not possible");
3044 case Legal:
3045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3046 break;
3047 case Promote:
3048 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3049 break;
3050 }
3051
3052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3053
3054 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3055 default: assert(0 && "Operation not supported");
3056 case TargetLowering::Custom:
3057 Tmp1 = TLI.LowerOperation(Result, DAG);
3058 if (Tmp1.Val) Result = Tmp1;
3059 break;
3060 case TargetLowering::Legal: break;
3061 case TargetLowering::Expand: {
3062 // If this target supports fabs/fneg natively and select is cheap,
3063 // do this efficiently.
3064 if (!TLI.isSelectExpensive() &&
3065 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3066 TargetLowering::Legal &&
3067 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3068 TargetLowering::Legal) {
3069 // Get the sign bit of the RHS.
3070 MVT::ValueType IVT =
3071 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3072 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003073 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3075 // Get the absolute value of the result.
3076 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3077 // Select between the nabs and abs value based on the sign bit of
3078 // the input.
3079 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3080 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3081 AbsVal),
3082 AbsVal);
3083 Result = LegalizeOp(Result);
3084 break;
3085 }
3086
3087 // Otherwise, do bitwise ops!
3088 MVT::ValueType NVT =
3089 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3090 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3091 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3092 Result = LegalizeOp(Result);
3093 break;
3094 }
3095 }
3096 break;
3097
3098 case ISD::ADDC:
3099 case ISD::SUBC:
3100 Tmp1 = LegalizeOp(Node->getOperand(0));
3101 Tmp2 = LegalizeOp(Node->getOperand(1));
3102 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3103 // Since this produces two values, make sure to remember that we legalized
3104 // both of them.
3105 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3106 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3107 return Result;
3108
3109 case ISD::ADDE:
3110 case ISD::SUBE:
3111 Tmp1 = LegalizeOp(Node->getOperand(0));
3112 Tmp2 = LegalizeOp(Node->getOperand(1));
3113 Tmp3 = LegalizeOp(Node->getOperand(2));
3114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3115 // Since this produces two values, make sure to remember that we legalized
3116 // both of them.
3117 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3118 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3119 return Result;
3120
3121 case ISD::BUILD_PAIR: {
3122 MVT::ValueType PairTy = Node->getValueType(0);
3123 // TODO: handle the case where the Lo and Hi operands are not of legal type
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3126 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3127 case TargetLowering::Promote:
3128 case TargetLowering::Custom:
3129 assert(0 && "Cannot promote/custom this yet!");
3130 case TargetLowering::Legal:
3131 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3132 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3133 break;
3134 case TargetLowering::Expand:
3135 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3136 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3137 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3138 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3139 TLI.getShiftAmountTy()));
3140 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3141 break;
3142 }
3143 break;
3144 }
3145
3146 case ISD::UREM:
3147 case ISD::SREM:
3148 case ISD::FREM:
3149 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3150 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3151
3152 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3153 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3154 case TargetLowering::Custom:
3155 isCustom = true;
3156 // FALLTHROUGH
3157 case TargetLowering::Legal:
3158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3159 if (isCustom) {
3160 Tmp1 = TLI.LowerOperation(Result, DAG);
3161 if (Tmp1.Val) Result = Tmp1;
3162 }
3163 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003164 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3166 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00003167 MVT::ValueType VT = Node->getValueType(0);
3168
3169 // See if remainder can be lowered using two-result operations.
3170 SDVTList VTs = DAG.getVTList(VT, VT);
3171 if (Node->getOpcode() == ISD::SREM &&
3172 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3173 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3174 break;
3175 }
3176 if (Node->getOpcode() == ISD::UREM &&
3177 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3178 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3179 break;
3180 }
3181
3182 if (MVT::isInteger(VT)) {
3183 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 TargetLowering::Legal) {
3185 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3187 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3188 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003189 } else if (MVT::isVector(VT)) {
3190 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003191 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003192 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193 "Cannot expand this binary operator!");
3194 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3195 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3196 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003197 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003198 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003199 } else {
3200 assert(MVT::isFloatingPoint(VT) &&
3201 "remainder op must have integer or floating-point type");
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003202 if (MVT::isVector(VT)) {
3203 Result = LegalizeOp(UnrollVectorOp(Op));
3204 } else {
3205 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003206 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3207 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003208 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003209 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003210 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211 }
3212 break;
3213 }
Dan Gohman5a199552007-10-08 18:33:35 +00003214 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 break;
3216 case ISD::VAARG: {
3217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3218 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3219
3220 MVT::ValueType VT = Node->getValueType(0);
3221 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3222 default: assert(0 && "This action is not supported yet!");
3223 case TargetLowering::Custom:
3224 isCustom = true;
3225 // FALLTHROUGH
3226 case TargetLowering::Legal:
3227 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3228 Result = Result.getValue(0);
3229 Tmp1 = Result.getValue(1);
3230
3231 if (isCustom) {
3232 Tmp2 = TLI.LowerOperation(Result, DAG);
3233 if (Tmp2.Val) {
3234 Result = LegalizeOp(Tmp2);
3235 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3236 }
3237 }
3238 break;
3239 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003240 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3241 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003242 // Increment the pointer, VAList, to the next vaarg
3243 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3244 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3245 TLI.getPointerTy()));
3246 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003247 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 // Load the actual argument out of the pointer VAList
3249 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3250 Tmp1 = LegalizeOp(Result.getValue(1));
3251 Result = LegalizeOp(Result);
3252 break;
3253 }
3254 }
3255 // Since VAARG produces two values, make sure to remember that we
3256 // legalized both of them.
3257 AddLegalizedOperand(SDOperand(Node, 0), Result);
3258 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3259 return Op.ResNo ? Tmp1 : Result;
3260 }
3261
3262 case ISD::VACOPY:
3263 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3264 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3265 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3266
3267 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3268 default: assert(0 && "This action is not supported yet!");
3269 case TargetLowering::Custom:
3270 isCustom = true;
3271 // FALLTHROUGH
3272 case TargetLowering::Legal:
3273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3274 Node->getOperand(3), Node->getOperand(4));
3275 if (isCustom) {
3276 Tmp1 = TLI.LowerOperation(Result, DAG);
3277 if (Tmp1.Val) Result = Tmp1;
3278 }
3279 break;
3280 case TargetLowering::Expand:
3281 // This defaults to loading a pointer from the input and storing it to the
3282 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003283 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3284 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003285 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3286 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003287 break;
3288 }
3289 break;
3290
3291 case ISD::VAEND:
3292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3294
3295 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3296 default: assert(0 && "This action is not supported yet!");
3297 case TargetLowering::Custom:
3298 isCustom = true;
3299 // FALLTHROUGH
3300 case TargetLowering::Legal:
3301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3302 if (isCustom) {
3303 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3304 if (Tmp1.Val) Result = Tmp1;
3305 }
3306 break;
3307 case TargetLowering::Expand:
3308 Result = Tmp1; // Default to a no-op, return the chain
3309 break;
3310 }
3311 break;
3312
3313 case ISD::VASTART:
3314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3315 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3316
3317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3318
3319 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3320 default: assert(0 && "This action is not supported yet!");
3321 case TargetLowering::Legal: break;
3322 case TargetLowering::Custom:
3323 Tmp1 = TLI.LowerOperation(Result, DAG);
3324 if (Tmp1.Val) Result = Tmp1;
3325 break;
3326 }
3327 break;
3328
3329 case ISD::ROTL:
3330 case ISD::ROTR:
3331 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3332 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3334 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3335 default:
3336 assert(0 && "ROTL/ROTR legalize operation not supported");
3337 break;
3338 case TargetLowering::Legal:
3339 break;
3340 case TargetLowering::Custom:
3341 Tmp1 = TLI.LowerOperation(Result, DAG);
3342 if (Tmp1.Val) Result = Tmp1;
3343 break;
3344 case TargetLowering::Promote:
3345 assert(0 && "Do not know how to promote ROTL/ROTR");
3346 break;
3347 case TargetLowering::Expand:
3348 assert(0 && "Do not know how to expand ROTL/ROTR");
3349 break;
3350 }
3351 break;
3352
3353 case ISD::BSWAP:
3354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3355 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3356 case TargetLowering::Custom:
3357 assert(0 && "Cannot custom legalize this yet!");
3358 case TargetLowering::Legal:
3359 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3360 break;
3361 case TargetLowering::Promote: {
3362 MVT::ValueType OVT = Tmp1.getValueType();
3363 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3364 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3365
3366 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3367 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3368 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3369 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3370 break;
3371 }
3372 case TargetLowering::Expand:
3373 Result = ExpandBSWAP(Tmp1);
3374 break;
3375 }
3376 break;
3377
3378 case ISD::CTPOP:
3379 case ISD::CTTZ:
3380 case ISD::CTLZ:
3381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3382 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003383 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384 case TargetLowering::Legal:
3385 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003386 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003387 TargetLowering::Custom) {
3388 Tmp1 = TLI.LowerOperation(Result, DAG);
3389 if (Tmp1.Val) {
3390 Result = Tmp1;
3391 }
Scott Michel48b63e62007-07-30 21:00:31 +00003392 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393 break;
3394 case TargetLowering::Promote: {
3395 MVT::ValueType OVT = Tmp1.getValueType();
3396 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3397
3398 // Zero extend the argument.
3399 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3400 // Perform the larger operation, then subtract if needed.
3401 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3402 switch (Node->getOpcode()) {
3403 case ISD::CTPOP:
3404 Result = Tmp1;
3405 break;
3406 case ISD::CTTZ:
3407 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003408 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3410 ISD::SETEQ);
3411 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003412 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 break;
3414 case ISD::CTLZ:
3415 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3416 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3417 DAG.getConstant(MVT::getSizeInBits(NVT) -
3418 MVT::getSizeInBits(OVT), NVT));
3419 break;
3420 }
3421 break;
3422 }
3423 case TargetLowering::Expand:
3424 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3425 break;
3426 }
3427 break;
3428
3429 // Unary operators
3430 case ISD::FABS:
3431 case ISD::FNEG:
3432 case ISD::FSQRT:
3433 case ISD::FSIN:
3434 case ISD::FCOS:
3435 Tmp1 = LegalizeOp(Node->getOperand(0));
3436 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3437 case TargetLowering::Promote:
3438 case TargetLowering::Custom:
3439 isCustom = true;
3440 // FALLTHROUGH
3441 case TargetLowering::Legal:
3442 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3443 if (isCustom) {
3444 Tmp1 = TLI.LowerOperation(Result, DAG);
3445 if (Tmp1.Val) Result = Tmp1;
3446 }
3447 break;
3448 case TargetLowering::Expand:
3449 switch (Node->getOpcode()) {
3450 default: assert(0 && "Unreachable!");
3451 case ISD::FNEG:
3452 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3453 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3454 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3455 break;
3456 case ISD::FABS: {
3457 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3458 MVT::ValueType VT = Node->getValueType(0);
3459 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003460 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003461 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3463 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3464 break;
3465 }
3466 case ISD::FSQRT:
3467 case ISD::FSIN:
3468 case ISD::FCOS: {
3469 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003470
3471 // Expand unsupported unary vector operators by unrolling them.
3472 if (MVT::isVector(VT)) {
3473 Result = LegalizeOp(UnrollVectorOp(Op));
3474 break;
3475 }
3476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3478 switch(Node->getOpcode()) {
3479 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003480 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3481 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003482 break;
3483 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003484 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3485 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486 break;
3487 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003488 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3489 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003490 break;
3491 default: assert(0 && "Unreachable!");
3492 }
3493 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003494 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 break;
3496 }
3497 }
3498 break;
3499 }
3500 break;
3501 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003502 MVT::ValueType VT = Node->getValueType(0);
3503
3504 // Expand unsupported unary vector operators by unrolling them.
3505 if (MVT::isVector(VT)) {
3506 Result = LegalizeOp(UnrollVectorOp(Op));
3507 break;
3508 }
3509
3510 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003511 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3512 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003514 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 break;
3516 }
3517 case ISD::BIT_CONVERT:
3518 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003519 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3520 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003521 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3522 // The input has to be a vector type, we have to either scalarize it, pack
3523 // it, or convert it based on whether the input vector type is legal.
3524 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003525 int InIx = Node->getOperand(0).ResNo;
3526 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3527 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528
3529 // Figure out if there is a simple type corresponding to this Vector
3530 // type. If so, convert to the vector type.
3531 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3532 if (TLI.isTypeLegal(TVT)) {
3533 // Turn this into a bit convert of the vector input.
3534 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3535 LegalizeOp(Node->getOperand(0)));
3536 break;
3537 } else if (NumElems == 1) {
3538 // Turn this into a bit convert of the scalar input.
3539 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3540 ScalarizeVectorOp(Node->getOperand(0)));
3541 break;
3542 } else {
3543 // FIXME: UNIMP! Store then reload
3544 assert(0 && "Cast from unsupported vector type not implemented yet!");
3545 }
3546 } else {
3547 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3548 Node->getOperand(0).getValueType())) {
3549 default: assert(0 && "Unknown operation action!");
3550 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003551 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3552 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003553 break;
3554 case TargetLowering::Legal:
3555 Tmp1 = LegalizeOp(Node->getOperand(0));
3556 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3557 break;
3558 }
3559 }
3560 break;
3561
3562 // Conversion operators. The source and destination have different types.
3563 case ISD::SINT_TO_FP:
3564 case ISD::UINT_TO_FP: {
3565 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3567 case Legal:
3568 switch (TLI.getOperationAction(Node->getOpcode(),
3569 Node->getOperand(0).getValueType())) {
3570 default: assert(0 && "Unknown operation action!");
3571 case TargetLowering::Custom:
3572 isCustom = true;
3573 // FALLTHROUGH
3574 case TargetLowering::Legal:
3575 Tmp1 = LegalizeOp(Node->getOperand(0));
3576 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3577 if (isCustom) {
3578 Tmp1 = TLI.LowerOperation(Result, DAG);
3579 if (Tmp1.Val) Result = Tmp1;
3580 }
3581 break;
3582 case TargetLowering::Expand:
3583 Result = ExpandLegalINT_TO_FP(isSigned,
3584 LegalizeOp(Node->getOperand(0)),
3585 Node->getValueType(0));
3586 break;
3587 case TargetLowering::Promote:
3588 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3589 Node->getValueType(0),
3590 isSigned);
3591 break;
3592 }
3593 break;
3594 case Expand:
3595 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3596 Node->getValueType(0), Node->getOperand(0));
3597 break;
3598 case Promote:
3599 Tmp1 = PromoteOp(Node->getOperand(0));
3600 if (isSigned) {
3601 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3602 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3603 } else {
3604 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3605 Node->getOperand(0).getValueType());
3606 }
3607 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3608 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3609 break;
3610 }
3611 break;
3612 }
3613 case ISD::TRUNCATE:
3614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3615 case Legal:
3616 Tmp1 = LegalizeOp(Node->getOperand(0));
3617 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3618 break;
3619 case Expand:
3620 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3621
3622 // Since the result is legal, we should just be able to truncate the low
3623 // part of the source.
3624 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3625 break;
3626 case Promote:
3627 Result = PromoteOp(Node->getOperand(0));
3628 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3629 break;
3630 }
3631 break;
3632
3633 case ISD::FP_TO_SINT:
3634 case ISD::FP_TO_UINT:
3635 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3636 case Legal:
3637 Tmp1 = LegalizeOp(Node->getOperand(0));
3638
3639 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3640 default: assert(0 && "Unknown operation action!");
3641 case TargetLowering::Custom:
3642 isCustom = true;
3643 // FALLTHROUGH
3644 case TargetLowering::Legal:
3645 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3646 if (isCustom) {
3647 Tmp1 = TLI.LowerOperation(Result, DAG);
3648 if (Tmp1.Val) Result = Tmp1;
3649 }
3650 break;
3651 case TargetLowering::Promote:
3652 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3653 Node->getOpcode() == ISD::FP_TO_SINT);
3654 break;
3655 case TargetLowering::Expand:
3656 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3657 SDOperand True, False;
3658 MVT::ValueType VT = Node->getOperand(0).getValueType();
3659 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003660 const uint64_t zero[] = {0, 0};
3661 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohman88ae8c52008-02-29 01:44:25 +00003662 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3663 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003664 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003665 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003666 Node->getOperand(0), Tmp2, ISD::SETLT);
3667 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3668 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3669 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3670 Tmp2));
3671 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003672 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003673 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3674 break;
3675 } else {
3676 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3677 }
3678 break;
3679 }
3680 break;
3681 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003682 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003683 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003684 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003685 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003686 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3687 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3688 Node->getOperand(0), DAG.getValueType(MVT::f64));
3689 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3690 DAG.getIntPtrConstant(1));
3691 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3692 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003693 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3694 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3695 Tmp2 = DAG.getConstantFP(apf, OVT);
3696 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3697 // FIXME: generated code sucks.
3698 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3699 DAG.getNode(ISD::ADD, MVT::i32,
3700 DAG.getNode(ISD::FP_TO_SINT, VT,
3701 DAG.getNode(ISD::FSUB, OVT,
3702 Node->getOperand(0), Tmp2)),
3703 DAG.getConstant(0x80000000, MVT::i32)),
3704 DAG.getNode(ISD::FP_TO_SINT, VT,
3705 Node->getOperand(0)),
3706 DAG.getCondCode(ISD::SETGE));
3707 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003708 break;
3709 }
Dan Gohmanec51f642008-03-10 23:03:31 +00003710 // Convert f32 / f64 to i32 / i64 / i128.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003711 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3712 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003713 case ISD::FP_TO_SINT: {
Dan Gohmanec51f642008-03-10 23:03:31 +00003714 if (VT == MVT::i32) {
3715 if (OVT == MVT::f32)
3716 LC = RTLIB::FPTOSINT_F32_I32;
3717 else if (OVT == MVT::f64)
3718 LC = RTLIB::FPTOSINT_F64_I32;
3719 else
3720 assert(0 && "Unexpected i32-to-fp conversion!");
3721 } else if (VT == MVT::i64) {
3722 if (OVT == MVT::f32)
3723 LC = RTLIB::FPTOSINT_F32_I64;
3724 else if (OVT == MVT::f64)
3725 LC = RTLIB::FPTOSINT_F64_I64;
3726 else if (OVT == MVT::f80)
3727 LC = RTLIB::FPTOSINT_F80_I64;
3728 else if (OVT == MVT::ppcf128)
3729 LC = RTLIB::FPTOSINT_PPCF128_I64;
3730 else
3731 assert(0 && "Unexpected i64-to-fp conversion!");
3732 } else if (VT == MVT::i128) {
3733 if (OVT == MVT::f32)
3734 LC = RTLIB::FPTOSINT_F32_I128;
3735 else if (OVT == MVT::f64)
3736 LC = RTLIB::FPTOSINT_F64_I128;
3737 else if (OVT == MVT::f80)
3738 LC = RTLIB::FPTOSINT_F80_I128;
3739 else if (OVT == MVT::ppcf128)
3740 LC = RTLIB::FPTOSINT_PPCF128_I128;
3741 else
3742 assert(0 && "Unexpected i128-to-fp conversion!");
3743 } else {
3744 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen958b08b2007-09-19 23:55:34 +00003745 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003747 }
3748 case ISD::FP_TO_UINT: {
Dan Gohmanec51f642008-03-10 23:03:31 +00003749 if (VT == MVT::i32) {
3750 if (OVT == MVT::f32)
3751 LC = RTLIB::FPTOUINT_F32_I32;
3752 else if (OVT == MVT::f64)
3753 LC = RTLIB::FPTOUINT_F64_I32;
3754 else if (OVT == MVT::f80)
3755 LC = RTLIB::FPTOUINT_F80_I32;
3756 else
3757 assert(0 && "Unexpected i32-to-fp conversion!");
3758 } else if (VT == MVT::i64) {
3759 if (OVT == MVT::f32)
3760 LC = RTLIB::FPTOUINT_F32_I64;
3761 else if (OVT == MVT::f64)
3762 LC = RTLIB::FPTOUINT_F64_I64;
3763 else if (OVT == MVT::f80)
3764 LC = RTLIB::FPTOUINT_F80_I64;
3765 else if (OVT == MVT::ppcf128)
3766 LC = RTLIB::FPTOUINT_PPCF128_I64;
3767 else
3768 assert(0 && "Unexpected i64-to-fp conversion!");
3769 } else if (VT == MVT::i128) {
3770 if (OVT == MVT::f32)
3771 LC = RTLIB::FPTOUINT_F32_I128;
3772 else if (OVT == MVT::f64)
3773 LC = RTLIB::FPTOUINT_F64_I128;
3774 else if (OVT == MVT::f80)
3775 LC = RTLIB::FPTOUINT_F80_I128;
3776 else if (OVT == MVT::ppcf128)
3777 LC = RTLIB::FPTOUINT_PPCF128_I128;
3778 else
3779 assert(0 && "Unexpected i128-to-fp conversion!");
3780 } else {
3781 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen958b08b2007-09-19 23:55:34 +00003782 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003783 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003784 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 default: assert(0 && "Unreachable!");
3786 }
3787 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003788 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 break;
3790 }
3791 case Promote:
3792 Tmp1 = PromoteOp(Node->getOperand(0));
3793 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3794 Result = LegalizeOp(Result);
3795 break;
3796 }
3797 break;
3798
Chris Lattner56ecde32008-01-16 06:57:07 +00003799 case ISD::FP_EXTEND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003800 MVT::ValueType DstVT = Op.getValueType();
3801 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3802 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3803 // The only other way we can lower this is to turn it into a STORE,
3804 // LOAD pair, targetting a temporary location (a stack slot).
3805 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3806 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003807 }
3808 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3809 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3810 case Legal:
3811 Tmp1 = LegalizeOp(Node->getOperand(0));
3812 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3813 break;
3814 case Promote:
3815 Tmp1 = PromoteOp(Node->getOperand(0));
3816 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3817 break;
3818 }
3819 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003820 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003821 case ISD::FP_ROUND: {
Chris Lattner5872a362008-01-17 07:00:52 +00003822 MVT::ValueType DstVT = Op.getValueType();
3823 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3824 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3825 if (SrcVT == MVT::ppcf128) {
Dale Johannesena0d36082008-01-20 01:18:38 +00003826 SDOperand Lo;
3827 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003828 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003829 if (DstVT!=MVT::f64)
3830 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003831 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003832 }
Chris Lattner5872a362008-01-17 07:00:52 +00003833 // The only other way we can lower this is to turn it into a STORE,
3834 // LOAD pair, targetting a temporary location (a stack slot).
3835 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3836 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3839 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3840 case Legal:
3841 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003843 break;
3844 case Promote:
3845 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003846 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3847 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003848 break;
3849 }
3850 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003851 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 case ISD::ANY_EXTEND:
3853 case ISD::ZERO_EXTEND:
3854 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3856 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3857 case Legal:
3858 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac7091c2008-02-15 23:05:48 +00003859 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3860 TargetLowering::Custom) {
3861 Tmp2 = TLI.LowerOperation(Result, DAG);
3862 if (Tmp2.Val) {
3863 Tmp1 = Tmp2;
3864 }
3865 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003866 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3867 break;
3868 case Promote:
3869 switch (Node->getOpcode()) {
3870 case ISD::ANY_EXTEND:
3871 Tmp1 = PromoteOp(Node->getOperand(0));
3872 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3873 break;
3874 case ISD::ZERO_EXTEND:
3875 Result = PromoteOp(Node->getOperand(0));
3876 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3877 Result = DAG.getZeroExtendInReg(Result,
3878 Node->getOperand(0).getValueType());
3879 break;
3880 case ISD::SIGN_EXTEND:
3881 Result = PromoteOp(Node->getOperand(0));
3882 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3883 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3884 Result,
3885 DAG.getValueType(Node->getOperand(0).getValueType()));
3886 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 }
3888 }
3889 break;
3890 case ISD::FP_ROUND_INREG:
3891 case ISD::SIGN_EXTEND_INREG: {
3892 Tmp1 = LegalizeOp(Node->getOperand(0));
3893 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3894
3895 // If this operation is not supported, convert it to a shl/shr or load/store
3896 // pair.
3897 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3898 default: assert(0 && "This action not supported for this op yet!");
3899 case TargetLowering::Legal:
3900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3901 break;
3902 case TargetLowering::Expand:
3903 // If this is an integer extend and shifts are supported, do that.
3904 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3905 // NOTE: we could fall back on load/store here too for targets without
3906 // SAR. However, it is doubtful that any exist.
3907 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3908 MVT::getSizeInBits(ExtraVT);
3909 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3910 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3911 Node->getOperand(0), ShiftCst);
3912 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3913 Result, ShiftCst);
3914 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3915 // The only way we can lower this is to turn it into a TRUNCSTORE,
3916 // EXTLOAD pair, targetting a temporary location (a stack slot).
3917
3918 // NOTE: there is a choice here between constantly creating new stack
3919 // slots and always reusing the same one. We currently always create
3920 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003921 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3922 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 } else {
3924 assert(0 && "Unknown op");
3925 }
3926 break;
3927 }
3928 break;
3929 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003930 case ISD::TRAMPOLINE: {
3931 SDOperand Ops[6];
3932 for (unsigned i = 0; i != 6; ++i)
3933 Ops[i] = LegalizeOp(Node->getOperand(i));
3934 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3935 // The only option for this node is to custom lower it.
3936 Result = TLI.LowerOperation(Result, DAG);
3937 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003938
3939 // Since trampoline produces two values, make sure to remember that we
3940 // legalized both of them.
3941 Tmp1 = LegalizeOp(Result.getValue(1));
3942 Result = LegalizeOp(Result);
3943 AddLegalizedOperand(SDOperand(Node, 0), Result);
3944 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3945 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003946 }
Dan Gohman819574c2008-01-31 00:41:03 +00003947 case ISD::FLT_ROUNDS_: {
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003948 MVT::ValueType VT = Node->getValueType(0);
3949 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3950 default: assert(0 && "This action not supported for this op yet!");
3951 case TargetLowering::Custom:
3952 Result = TLI.LowerOperation(Op, DAG);
3953 if (Result.Val) break;
3954 // Fall Thru
3955 case TargetLowering::Legal:
3956 // If this operation is not supported, lower it to constant 1
3957 Result = DAG.getConstant(1, VT);
3958 break;
3959 }
3960 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003961 case ISD::TRAP: {
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003962 MVT::ValueType VT = Node->getValueType(0);
3963 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3964 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00003965 case TargetLowering::Legal:
3966 Tmp1 = LegalizeOp(Node->getOperand(0));
3967 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3968 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003969 case TargetLowering::Custom:
3970 Result = TLI.LowerOperation(Op, DAG);
3971 if (Result.Val) break;
3972 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00003973 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003974 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00003975 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003976 TargetLowering::ArgListTy Args;
3977 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00003978 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3979 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00003980 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3981 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003982 Result = CallResult.second;
3983 break;
3984 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003985 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003986 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 }
3988
3989 assert(Result.getValueType() == Op.getValueType() &&
3990 "Bad legalization!");
3991
3992 // Make sure that the generated code is itself legal.
3993 if (Result != Op)
3994 Result = LegalizeOp(Result);
3995
3996 // Note that LegalizeOp may be reentered even from single-use nodes, which
3997 // means that we always must cache transformed nodes.
3998 AddLegalizedOperand(Op, Result);
3999 return Result;
4000}
4001
4002/// PromoteOp - Given an operation that produces a value in an invalid type,
4003/// promote it to compute the value into a larger type. The produced value will
4004/// have the correct bits for the low portion of the register, but no guarantee
4005/// is made about the top bits: it may be zero, sign-extended, or garbage.
4006SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4007 MVT::ValueType VT = Op.getValueType();
4008 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4009 assert(getTypeAction(VT) == Promote &&
4010 "Caller should expand or legalize operands that are not promotable!");
4011 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4012 "Cannot promote to smaller type!");
4013
4014 SDOperand Tmp1, Tmp2, Tmp3;
4015 SDOperand Result;
4016 SDNode *Node = Op.Val;
4017
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004018 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004019 if (I != PromotedNodes.end()) return I->second;
4020
4021 switch (Node->getOpcode()) {
4022 case ISD::CopyFromReg:
4023 assert(0 && "CopyFromReg must be legal!");
4024 default:
4025#ifndef NDEBUG
4026 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4027#endif
4028 assert(0 && "Do not know how to promote this operator!");
4029 abort();
4030 case ISD::UNDEF:
4031 Result = DAG.getNode(ISD::UNDEF, NVT);
4032 break;
4033 case ISD::Constant:
4034 if (VT != MVT::i1)
4035 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4036 else
4037 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4038 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4039 break;
4040 case ISD::ConstantFP:
4041 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4042 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4043 break;
4044
4045 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004046 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004047 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004048 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004049 TLI.getSetCCResultType(Node->getOperand(0)),
4050 Node->getOperand(0), Node->getOperand(1),
4051 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004052 break;
4053
4054 case ISD::TRUNCATE:
4055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4056 case Legal:
4057 Result = LegalizeOp(Node->getOperand(0));
4058 assert(Result.getValueType() >= NVT &&
4059 "This truncation doesn't make sense!");
4060 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4061 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4062 break;
4063 case Promote:
4064 // The truncation is not required, because we don't guarantee anything
4065 // about high bits anyway.
4066 Result = PromoteOp(Node->getOperand(0));
4067 break;
4068 case Expand:
4069 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4070 // Truncate the low part of the expanded value to the result type
4071 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4072 }
4073 break;
4074 case ISD::SIGN_EXTEND:
4075 case ISD::ZERO_EXTEND:
4076 case ISD::ANY_EXTEND:
4077 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4078 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4079 case Legal:
4080 // Input is legal? Just do extend all the way to the larger type.
4081 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4082 break;
4083 case Promote:
4084 // Promote the reg if it's smaller.
4085 Result = PromoteOp(Node->getOperand(0));
4086 // The high bits are not guaranteed to be anything. Insert an extend.
4087 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4088 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4089 DAG.getValueType(Node->getOperand(0).getValueType()));
4090 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4091 Result = DAG.getZeroExtendInReg(Result,
4092 Node->getOperand(0).getValueType());
4093 break;
4094 }
4095 break;
4096 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004097 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4098 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004099 Result = PromoteOp(Result);
4100 break;
4101
4102 case ISD::FP_EXTEND:
4103 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4104 case ISD::FP_ROUND:
4105 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4106 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4107 case Promote: assert(0 && "Unreachable with 2 FP types!");
4108 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004109 if (Node->getConstantOperandVal(1) == 0) {
4110 // Input is legal? Do an FP_ROUND_INREG.
4111 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4112 DAG.getValueType(VT));
4113 } else {
4114 // Just remove the truncate, it isn't affecting the value.
4115 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4116 Node->getOperand(1));
4117 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 break;
4119 }
4120 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121 case ISD::SINT_TO_FP:
4122 case ISD::UINT_TO_FP:
4123 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4124 case Legal:
4125 // No extra round required here.
4126 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4127 break;
4128
4129 case Promote:
4130 Result = PromoteOp(Node->getOperand(0));
4131 if (Node->getOpcode() == ISD::SINT_TO_FP)
4132 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4133 Result,
4134 DAG.getValueType(Node->getOperand(0).getValueType()));
4135 else
4136 Result = DAG.getZeroExtendInReg(Result,
4137 Node->getOperand(0).getValueType());
4138 // No extra round required here.
4139 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4140 break;
4141 case Expand:
4142 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4143 Node->getOperand(0));
4144 // Round if we cannot tolerate excess precision.
4145 if (NoExcessFPPrecision)
4146 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4147 DAG.getValueType(VT));
4148 break;
4149 }
4150 break;
4151
4152 case ISD::SIGN_EXTEND_INREG:
4153 Result = PromoteOp(Node->getOperand(0));
4154 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4155 Node->getOperand(1));
4156 break;
4157 case ISD::FP_TO_SINT:
4158 case ISD::FP_TO_UINT:
4159 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4160 case Legal:
4161 case Expand:
4162 Tmp1 = Node->getOperand(0);
4163 break;
4164 case Promote:
4165 // The input result is prerounded, so we don't have to do anything
4166 // special.
4167 Tmp1 = PromoteOp(Node->getOperand(0));
4168 break;
4169 }
4170 // If we're promoting a UINT to a larger size, check to see if the new node
4171 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4172 // we can use that instead. This allows us to generate better code for
4173 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4174 // legal, such as PowerPC.
4175 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4176 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4177 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4178 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4179 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4180 } else {
4181 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4182 }
4183 break;
4184
4185 case ISD::FABS:
4186 case ISD::FNEG:
4187 Tmp1 = PromoteOp(Node->getOperand(0));
4188 assert(Tmp1.getValueType() == NVT);
4189 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4190 // NOTE: we do not have to do any extra rounding here for
4191 // NoExcessFPPrecision, because we know the input will have the appropriate
4192 // precision, and these operations don't modify precision at all.
4193 break;
4194
4195 case ISD::FSQRT:
4196 case ISD::FSIN:
4197 case ISD::FCOS:
4198 Tmp1 = PromoteOp(Node->getOperand(0));
4199 assert(Tmp1.getValueType() == NVT);
4200 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4201 if (NoExcessFPPrecision)
4202 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4203 DAG.getValueType(VT));
4204 break;
4205
4206 case ISD::FPOWI: {
4207 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4208 // directly as well, which may be better.
4209 Tmp1 = PromoteOp(Node->getOperand(0));
4210 assert(Tmp1.getValueType() == NVT);
4211 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4212 if (NoExcessFPPrecision)
4213 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4214 DAG.getValueType(VT));
4215 break;
4216 }
4217
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004218 case ISD::ATOMIC_LCS: {
4219 Tmp2 = PromoteOp(Node->getOperand(2));
4220 Tmp3 = PromoteOp(Node->getOperand(3));
4221 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4222 Node->getOperand(1), Tmp2, Tmp3,
4223 cast<AtomicSDNode>(Node)->getVT());
4224 // Remember that we legalized the chain.
4225 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4226 break;
4227 }
4228 case ISD::ATOMIC_LAS:
4229 case ISD::ATOMIC_SWAP: {
4230 Tmp2 = PromoteOp(Node->getOperand(2));
4231 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4232 Node->getOperand(1), Tmp2,
4233 cast<AtomicSDNode>(Node)->getVT());
4234 // Remember that we legalized the chain.
4235 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4236 break;
4237 }
4238
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239 case ISD::AND:
4240 case ISD::OR:
4241 case ISD::XOR:
4242 case ISD::ADD:
4243 case ISD::SUB:
4244 case ISD::MUL:
4245 // The input may have strange things in the top bits of the registers, but
4246 // these operations don't care. They may have weird bits going out, but
4247 // that too is okay if they are integer operations.
4248 Tmp1 = PromoteOp(Node->getOperand(0));
4249 Tmp2 = PromoteOp(Node->getOperand(1));
4250 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4251 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4252 break;
4253 case ISD::FADD:
4254 case ISD::FSUB:
4255 case ISD::FMUL:
4256 Tmp1 = PromoteOp(Node->getOperand(0));
4257 Tmp2 = PromoteOp(Node->getOperand(1));
4258 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4259 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4260
4261 // Floating point operations will give excess precision that we may not be
4262 // able to tolerate. If we DO allow excess precision, just leave it,
4263 // otherwise excise it.
4264 // FIXME: Why would we need to round FP ops more than integer ones?
4265 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4266 if (NoExcessFPPrecision)
4267 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4268 DAG.getValueType(VT));
4269 break;
4270
4271 case ISD::SDIV:
4272 case ISD::SREM:
4273 // These operators require that their input be sign extended.
4274 Tmp1 = PromoteOp(Node->getOperand(0));
4275 Tmp2 = PromoteOp(Node->getOperand(1));
4276 if (MVT::isInteger(NVT)) {
4277 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4278 DAG.getValueType(VT));
4279 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4280 DAG.getValueType(VT));
4281 }
4282 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4283
4284 // Perform FP_ROUND: this is probably overly pessimistic.
4285 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
4286 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4287 DAG.getValueType(VT));
4288 break;
4289 case ISD::FDIV:
4290 case ISD::FREM:
4291 case ISD::FCOPYSIGN:
4292 // These operators require that their input be fp extended.
4293 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004294 case Expand: assert(0 && "not implemented");
4295 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4296 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004297 }
4298 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004299 case Expand: assert(0 && "not implemented");
4300 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4301 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302 }
4303 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4304
4305 // Perform FP_ROUND: this is probably overly pessimistic.
4306 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4307 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4308 DAG.getValueType(VT));
4309 break;
4310
4311 case ISD::UDIV:
4312 case ISD::UREM:
4313 // These operators require that their input be zero extended.
4314 Tmp1 = PromoteOp(Node->getOperand(0));
4315 Tmp2 = PromoteOp(Node->getOperand(1));
4316 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
4317 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4318 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4319 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4320 break;
4321
4322 case ISD::SHL:
4323 Tmp1 = PromoteOp(Node->getOperand(0));
4324 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4325 break;
4326 case ISD::SRA:
4327 // The input value must be properly sign extended.
4328 Tmp1 = PromoteOp(Node->getOperand(0));
4329 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4330 DAG.getValueType(VT));
4331 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4332 break;
4333 case ISD::SRL:
4334 // The input value must be properly zero extended.
4335 Tmp1 = PromoteOp(Node->getOperand(0));
4336 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4337 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4338 break;
4339
4340 case ISD::VAARG:
4341 Tmp1 = Node->getOperand(0); // Get the chain.
4342 Tmp2 = Node->getOperand(1); // Get the pointer.
4343 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4344 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4345 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4346 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004347 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4348 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349 // Increment the pointer, VAList, to the next vaarg
4350 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4351 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4352 TLI.getPointerTy()));
4353 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004354 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 // Load the actual argument out of the pointer VAList
4356 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4357 }
4358 // Remember that we legalized the chain.
4359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4360 break;
4361
4362 case ISD::LOAD: {
4363 LoadSDNode *LD = cast<LoadSDNode>(Node);
4364 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4365 ? ISD::EXTLOAD : LD->getExtensionType();
4366 Result = DAG.getExtLoad(ExtType, NVT,
4367 LD->getChain(), LD->getBasePtr(),
4368 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004369 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004370 LD->isVolatile(),
4371 LD->getAlignment());
4372 // Remember that we legalized the chain.
4373 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4374 break;
4375 }
4376 case ISD::SELECT:
4377 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4378 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4379 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4380 break;
4381 case ISD::SELECT_CC:
4382 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4383 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4384 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4385 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4386 break;
4387 case ISD::BSWAP:
4388 Tmp1 = Node->getOperand(0);
4389 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4390 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4391 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4392 DAG.getConstant(MVT::getSizeInBits(NVT) -
4393 MVT::getSizeInBits(VT),
4394 TLI.getShiftAmountTy()));
4395 break;
4396 case ISD::CTPOP:
4397 case ISD::CTTZ:
4398 case ISD::CTLZ:
4399 // Zero extend the argument
4400 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4401 // Perform the larger operation, then subtract if needed.
4402 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4403 switch(Node->getOpcode()) {
4404 case ISD::CTPOP:
4405 Result = Tmp1;
4406 break;
4407 case ISD::CTTZ:
4408 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004409 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4411 ISD::SETEQ);
4412 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4413 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4414 break;
4415 case ISD::CTLZ:
4416 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4417 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4418 DAG.getConstant(MVT::getSizeInBits(NVT) -
4419 MVT::getSizeInBits(VT), NVT));
4420 break;
4421 }
4422 break;
4423 case ISD::EXTRACT_SUBVECTOR:
4424 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4425 break;
4426 case ISD::EXTRACT_VECTOR_ELT:
4427 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4428 break;
4429 }
4430
4431 assert(Result.Val && "Didn't set a result!");
4432
4433 // Make sure the result is itself legal.
4434 Result = LegalizeOp(Result);
4435
4436 // Remember that we promoted this!
4437 AddPromotedOperand(Op, Result);
4438 return Result;
4439}
4440
4441/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4442/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4443/// based on the vector type. The return type of this matches the element type
4444/// of the vector, which may not be legal for the target.
4445SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4446 // We know that operand #0 is the Vec vector. If the index is a constant
4447 // or if the invec is a supported hardware type, we can use it. Otherwise,
4448 // lower to a store then an indexed load.
4449 SDOperand Vec = Op.getOperand(0);
4450 SDOperand Idx = Op.getOperand(1);
4451
Dan Gohmana0763d92007-09-24 15:54:53 +00004452 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453 unsigned NumElems = MVT::getVectorNumElements(TVT);
4454
4455 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4456 default: assert(0 && "This action is not supported yet!");
4457 case TargetLowering::Custom: {
4458 Vec = LegalizeOp(Vec);
4459 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4460 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4461 if (Tmp3.Val)
4462 return Tmp3;
4463 break;
4464 }
4465 case TargetLowering::Legal:
4466 if (isTypeLegal(TVT)) {
4467 Vec = LegalizeOp(Vec);
4468 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004469 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 }
4471 break;
4472 case TargetLowering::Expand:
4473 break;
4474 }
4475
4476 if (NumElems == 1) {
4477 // This must be an access of the only element. Return it.
4478 Op = ScalarizeVectorOp(Vec);
4479 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004480 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004481 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4482 SDOperand Lo, Hi;
4483 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004484 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 Vec = Lo;
4486 } else {
4487 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004488 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489 Idx.getValueType());
4490 }
4491
4492 // It's now an extract from the appropriate high or low part. Recurse.
4493 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4494 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4495 } else {
4496 // Store the value to a temporary stack slot, then LOAD the scalar
4497 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004498 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4500
4501 // Add the offset to the index.
4502 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4503 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4504 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004505
4506 if (MVT::getSizeInBits(Idx.getValueType()) >
4507 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004508 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004509 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004510 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4513
4514 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4515 }
4516 return Op;
4517}
4518
4519/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4520/// we assume the operation can be split if it is not already legal.
4521SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4522 // We know that operand #0 is the Vec vector. For now we assume the index
4523 // is a constant and that the extracted result is a supported hardware type.
4524 SDOperand Vec = Op.getOperand(0);
4525 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4526
4527 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4528
4529 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4530 // This must be an access of the desired vector length. Return it.
4531 return Vec;
4532 }
4533
4534 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4535 SDOperand Lo, Hi;
4536 SplitVectorOp(Vec, Lo, Hi);
4537 if (CIdx->getValue() < NumElems/2) {
4538 Vec = Lo;
4539 } else {
4540 Vec = Hi;
4541 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4542 }
4543
4544 // It's now an extract from the appropriate high or low part. Recurse.
4545 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4546 return ExpandEXTRACT_SUBVECTOR(Op);
4547}
4548
4549/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4550/// with condition CC on the current target. This usually involves legalizing
4551/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4552/// there may be no choice but to create a new SetCC node to represent the
4553/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4554/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4555void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4556 SDOperand &RHS,
4557 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004558 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559
4560 switch (getTypeAction(LHS.getValueType())) {
4561 case Legal:
4562 Tmp1 = LegalizeOp(LHS); // LHS
4563 Tmp2 = LegalizeOp(RHS); // RHS
4564 break;
4565 case Promote:
4566 Tmp1 = PromoteOp(LHS); // LHS
4567 Tmp2 = PromoteOp(RHS); // RHS
4568
4569 // If this is an FP compare, the operands have already been extended.
4570 if (MVT::isInteger(LHS.getValueType())) {
4571 MVT::ValueType VT = LHS.getValueType();
4572 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4573
4574 // Otherwise, we have to insert explicit sign or zero extends. Note
4575 // that we could insert sign extends for ALL conditions, but zero extend
4576 // is cheaper on many machines (an AND instead of two shifts), so prefer
4577 // it.
4578 switch (cast<CondCodeSDNode>(CC)->get()) {
4579 default: assert(0 && "Unknown integer comparison!");
4580 case ISD::SETEQ:
4581 case ISD::SETNE:
4582 case ISD::SETUGE:
4583 case ISD::SETUGT:
4584 case ISD::SETULE:
4585 case ISD::SETULT:
4586 // ALL of these operations will work if we either sign or zero extend
4587 // the operands (including the unsigned comparisons!). Zero extend is
4588 // usually a simpler/cheaper operation, so prefer it.
4589 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4590 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4591 break;
4592 case ISD::SETGE:
4593 case ISD::SETGT:
4594 case ISD::SETLT:
4595 case ISD::SETLE:
4596 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4597 DAG.getValueType(VT));
4598 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4599 DAG.getValueType(VT));
4600 break;
4601 }
4602 }
4603 break;
4604 case Expand: {
4605 MVT::ValueType VT = LHS.getValueType();
4606 if (VT == MVT::f32 || VT == MVT::f64) {
4607 // Expand into one or more soft-fp libcall(s).
4608 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4609 switch (cast<CondCodeSDNode>(CC)->get()) {
4610 case ISD::SETEQ:
4611 case ISD::SETOEQ:
4612 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4613 break;
4614 case ISD::SETNE:
4615 case ISD::SETUNE:
4616 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4617 break;
4618 case ISD::SETGE:
4619 case ISD::SETOGE:
4620 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4621 break;
4622 case ISD::SETLT:
4623 case ISD::SETOLT:
4624 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4625 break;
4626 case ISD::SETLE:
4627 case ISD::SETOLE:
4628 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4629 break;
4630 case ISD::SETGT:
4631 case ISD::SETOGT:
4632 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4633 break;
4634 case ISD::SETUO:
4635 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4636 break;
4637 case ISD::SETO:
4638 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4639 break;
4640 default:
4641 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4642 switch (cast<CondCodeSDNode>(CC)->get()) {
4643 case ISD::SETONE:
4644 // SETONE = SETOLT | SETOGT
4645 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4646 // Fallthrough
4647 case ISD::SETUGT:
4648 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4649 break;
4650 case ISD::SETUGE:
4651 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4652 break;
4653 case ISD::SETULT:
4654 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4655 break;
4656 case ISD::SETULE:
4657 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4658 break;
4659 case ISD::SETUEQ:
4660 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4661 break;
4662 default: assert(0 && "Unsupported FP setcc!");
4663 }
4664 }
4665
4666 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004667 Tmp1 = ExpandLibCall(LC1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4669 false /*sign irrelevant*/, Dummy);
4670 Tmp2 = DAG.getConstant(0, MVT::i32);
4671 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4672 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00004673 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004674 CC);
Duncan Sandsf1db7c82008-04-12 17:14:18 +00004675 LHS = ExpandLibCall(LC2,
4676 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00004678 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004679 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4680 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4681 Tmp2 = SDOperand();
4682 }
4683 LHS = Tmp1;
4684 RHS = Tmp2;
4685 return;
4686 }
4687
4688 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4689 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004690 ExpandOp(RHS, RHSLo, RHSHi);
4691 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4692
4693 if (VT==MVT::ppcf128) {
4694 // FIXME: This generated code sucks. We want to generate
4695 // FCMP crN, hi1, hi2
4696 // BNE crN, L:
4697 // FCMP crN, lo1, lo2
4698 // The following can be improved, but not that much.
Scott Michel502151f2008-03-10 15:42:14 +00004699 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4700 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004701 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00004702 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4703 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004704 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4705 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4706 Tmp2 = SDOperand();
4707 break;
4708 }
4709
4710 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 case ISD::SETEQ:
4712 case ISD::SETNE:
4713 if (RHSLo == RHSHi)
4714 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4715 if (RHSCST->isAllOnesValue()) {
4716 // Comparison to -1.
4717 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4718 Tmp2 = RHSLo;
4719 break;
4720 }
4721
4722 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4723 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4724 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4725 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4726 break;
4727 default:
4728 // If this is a comparison of the sign bit, just look at the top part.
4729 // X > -1, x < 0
4730 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4731 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004732 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004733 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4734 CST->isAllOnesValue())) { // X > -1
4735 Tmp1 = LHSHi;
4736 Tmp2 = RHSHi;
4737 break;
4738 }
4739
4740 // FIXME: This generated code sucks.
4741 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742 switch (CCCode) {
4743 default: assert(0 && "Unknown integer setcc!");
4744 case ISD::SETLT:
4745 case ISD::SETULT: LowCC = ISD::SETULT; break;
4746 case ISD::SETGT:
4747 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4748 case ISD::SETLE:
4749 case ISD::SETULE: LowCC = ISD::SETULE; break;
4750 case ISD::SETGE:
4751 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4752 }
4753
4754 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4755 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4756 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4757
4758 // NOTE: on targets without efficient SELECT of bools, we can always use
4759 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4760 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00004761 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004762 LowCC, false, DagCombineInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763 if (!Tmp1.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004764 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4765 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004766 CCCode, false, DagCombineInfo);
4767 if (!Tmp2.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004768 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004769 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770
4771 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4772 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohman9d24dc72008-03-13 22:13:53 +00004773 if ((Tmp1C && Tmp1C->isNullValue()) ||
4774 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004775 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4776 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00004777 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4779 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4780 // low part is known false, returns high part.
4781 // For LE / GE, if high part is known false, ignore the low part.
4782 // For LT / GT, if high part is known true, ignore the low part.
4783 Tmp1 = Tmp2;
4784 Tmp2 = SDOperand();
4785 } else {
Scott Michel502151f2008-03-10 15:42:14 +00004786 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 ISD::SETEQ, false, DagCombineInfo);
4788 if (!Result.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004789 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004790 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004791 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4792 Result, Tmp1, Tmp2));
4793 Tmp1 = Result;
4794 Tmp2 = SDOperand();
4795 }
4796 }
4797 }
4798 }
4799 LHS = Tmp1;
4800 RHS = Tmp2;
4801}
4802
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004803/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4804/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4805/// a load from the stack slot to DestVT, extending it if needed.
4806/// The resultant code need not be legal.
4807SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4808 MVT::ValueType SlotVT,
4809 MVT::ValueType DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004810 // Create the stack frame object.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004811 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4812
Dan Gohman20e37962008-02-11 18:58:42 +00004813 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004814 int SPFI = StackPtrFI->getIndex();
4815
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004816 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4817 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4818 unsigned DestSize = MVT::getSizeInBits(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004820 // Emit a store to the stack slot. Use a truncstore if the input value is
4821 // later than DestVT.
4822 SDOperand Store;
4823 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004824 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004825 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004826 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004827 else {
4828 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004829 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004830 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004831 SPFI, SlotVT);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004832 }
4833
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004835 if (SlotSize == DestSize)
4836 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4837
4838 assert(SlotSize < DestSize && "Unknown extension!");
4839 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840}
4841
4842SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4843 // Create a vector sized/aligned stack slot, store the value to element #0,
4844 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004845 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00004846
Dan Gohman20e37962008-02-11 18:58:42 +00004847 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004848 int SPFI = StackPtrFI->getIndex();
4849
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004851 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman12a9c082008-02-06 22:27:42 +00004852 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004853 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854}
4855
4856
4857/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4858/// support the operation, but do support the resultant vector type.
4859SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4860
4861 // If the only non-undef value is the low element, turn this into a
4862 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4863 unsigned NumElems = Node->getNumOperands();
4864 bool isOnlyLowElement = true;
4865 SDOperand SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00004866
4867 // FIXME: it would be far nicer to change this into map<SDOperand,uint64_t>
4868 // and use a bitmask instead of a list of elements.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004869 std::map<SDOperand, std::vector<unsigned> > Values;
4870 Values[SplatValue].push_back(0);
4871 bool isConstant = true;
4872 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4873 SplatValue.getOpcode() != ISD::UNDEF)
4874 isConstant = false;
4875
4876 for (unsigned i = 1; i < NumElems; ++i) {
4877 SDOperand V = Node->getOperand(i);
4878 Values[V].push_back(i);
4879 if (V.getOpcode() != ISD::UNDEF)
4880 isOnlyLowElement = false;
4881 if (SplatValue != V)
4882 SplatValue = SDOperand(0,0);
4883
4884 // If this isn't a constant element or an undef, we can't use a constant
4885 // pool load.
4886 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4887 V.getOpcode() != ISD::UNDEF)
4888 isConstant = false;
4889 }
4890
4891 if (isOnlyLowElement) {
4892 // If the low element is an undef too, then this whole things is an undef.
4893 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4894 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4895 // Otherwise, turn this into a scalar_to_vector node.
4896 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4897 Node->getOperand(0));
4898 }
4899
4900 // If all elements are constants, create a load from the constant pool.
4901 if (isConstant) {
4902 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 std::vector<Constant*> CV;
4904 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4905 if (ConstantFPSDNode *V =
4906 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004907 CV.push_back(ConstantFP::get(V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004908 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00004909 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4910 CV.push_back(ConstantInt::get(V->getAPIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004911 } else {
4912 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00004913 const Type *OpNTy =
4914 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004915 CV.push_back(UndefValue::get(OpNTy));
4916 }
4917 }
4918 Constant *CP = ConstantVector::get(CV);
4919 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004920 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004921 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 }
4923
4924 if (SplatValue.Val) { // Splat of one value?
4925 // Build the shuffle constant vector: <0, 0, 0, 0>
4926 MVT::ValueType MaskVT =
4927 MVT::getIntVectorWithNumElements(NumElems);
4928 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4929 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4930 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4931 &ZeroVec[0], ZeroVec.size());
4932
4933 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4934 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4935 // Get the splatted value into the low element of a vector register.
4936 SDOperand LowValVec =
4937 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4938
4939 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4940 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4941 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4942 SplatMask);
4943 }
4944 }
4945
4946 // If there are only two unique elements, we may be able to turn this into a
4947 // vector shuffle.
4948 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00004949 // Get the two values in deterministic order.
4950 SDOperand Val1 = Node->getOperand(1);
4951 SDOperand Val2;
4952 std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin();
4953 if (MI->first != Val1)
4954 Val2 = MI->first;
4955 else
4956 Val2 = (++MI)->first;
4957
4958 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4959 // vector shuffle has the undef vector on the RHS.
4960 if (Val1.getOpcode() == ISD::UNDEF)
4961 std::swap(Val1, Val2);
4962
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Chris Lattnerd8cee732008-03-09 00:29:42 +00004964 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4965 MVT::ValueType MaskEltVT = MVT::getVectorElementType(MaskVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966 std::vector<SDOperand> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00004967
4968 // Set elements of the shuffle mask for Val1.
4969 std::vector<unsigned> &Val1Elts = Values[Val1];
4970 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4971 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4972
4973 // Set elements of the shuffle mask for Val2.
4974 std::vector<unsigned> &Val2Elts = Values[Val2];
4975 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4976 if (Val2.getOpcode() != ISD::UNDEF)
4977 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4978 else
4979 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4980
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4982 &MaskVec[0], MaskVec.size());
4983
Chris Lattnerd8cee732008-03-09 00:29:42 +00004984 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4986 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00004987 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
4988 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
4989 SDOperand Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990
4991 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00004992 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004993 }
4994 }
4995
4996 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4997 // aligned object on the stack, store each element into it, then load
4998 // the result as a vector.
4999 MVT::ValueType VT = Node->getValueType(0);
5000 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00005001 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002
5003 // Emit a store of each element to the stack slot.
5004 SmallVector<SDOperand, 8> Stores;
5005 unsigned TypeByteSize =
5006 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
5007 // Store (in the right endianness) the elements to memory.
5008 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5009 // Ignore undef elements.
5010 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5011
5012 unsigned Offset = TypeByteSize*i;
5013
5014 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5015 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5016
5017 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5018 NULL, 0));
5019 }
5020
5021 SDOperand StoreChain;
5022 if (!Stores.empty()) // Not all undef elements?
5023 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5024 &Stores[0], Stores.size());
5025 else
5026 StoreChain = DAG.getEntryNode();
5027
5028 // Result is a load from the stack slot.
5029 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5030}
5031
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5033 SDOperand Op, SDOperand Amt,
5034 SDOperand &Lo, SDOperand &Hi) {
5035 // Expand the subcomponents.
5036 SDOperand LHSL, LHSH;
5037 ExpandOp(Op, LHSL, LHSH);
5038
5039 SDOperand Ops[] = { LHSL, LHSH, Amt };
5040 MVT::ValueType VT = LHSL.getValueType();
5041 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5042 Hi = Lo.getValue(1);
5043}
5044
5045
5046/// ExpandShift - Try to find a clever way to expand this shift operation out to
5047/// smaller elements. If we can't find a way that is more efficient than a
5048/// libcall on this target, return false. Otherwise, return true with the
5049/// low-parts expanded into Lo and Hi.
5050bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5051 SDOperand &Lo, SDOperand &Hi) {
5052 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5053 "This is not a shift!");
5054
5055 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
5056 SDOperand ShAmt = LegalizeOp(Amt);
5057 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohmanece0a882008-02-20 16:57:27 +00005058 unsigned ShBits = MVT::getSizeInBits(ShTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5060 unsigned NVTBits = MVT::getSizeInBits(NVT);
5061
Chris Lattner8c931452007-10-14 20:35:12 +00005062 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5064 unsigned Cst = CN->getValue();
5065 // Expand the incoming operand to be shifted, so that we have its parts
5066 SDOperand InL, InH;
5067 ExpandOp(Op, InL, InH);
5068 switch(Opc) {
5069 case ISD::SHL:
5070 if (Cst > VTBits) {
5071 Lo = DAG.getConstant(0, NVT);
5072 Hi = DAG.getConstant(0, NVT);
5073 } else if (Cst > NVTBits) {
5074 Lo = DAG.getConstant(0, NVT);
5075 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5076 } else if (Cst == NVTBits) {
5077 Lo = DAG.getConstant(0, NVT);
5078 Hi = InL;
5079 } else {
5080 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5081 Hi = DAG.getNode(ISD::OR, NVT,
5082 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5083 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5084 }
5085 return true;
5086 case ISD::SRL:
5087 if (Cst > VTBits) {
5088 Lo = DAG.getConstant(0, NVT);
5089 Hi = DAG.getConstant(0, NVT);
5090 } else if (Cst > NVTBits) {
5091 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5092 Hi = DAG.getConstant(0, NVT);
5093 } else if (Cst == NVTBits) {
5094 Lo = InH;
5095 Hi = DAG.getConstant(0, NVT);
5096 } else {
5097 Lo = DAG.getNode(ISD::OR, NVT,
5098 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5099 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5100 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5101 }
5102 return true;
5103 case ISD::SRA:
5104 if (Cst > VTBits) {
5105 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5106 DAG.getConstant(NVTBits-1, ShTy));
5107 } else if (Cst > NVTBits) {
5108 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5109 DAG.getConstant(Cst-NVTBits, ShTy));
5110 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5111 DAG.getConstant(NVTBits-1, ShTy));
5112 } else if (Cst == NVTBits) {
5113 Lo = InH;
5114 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5115 DAG.getConstant(NVTBits-1, ShTy));
5116 } else {
5117 Lo = DAG.getNode(ISD::OR, NVT,
5118 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5119 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5120 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5121 }
5122 return true;
5123 }
5124 }
5125
5126 // Okay, the shift amount isn't constant. However, if we can tell that it is
5127 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005128 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5129 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005130 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5131
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005132 // If we know that if any of the high bits of the shift amount are one, then
5133 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005134 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005135 // Mask out the high bit, which we know is set.
5136 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005137 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005138
5139 // Expand the incoming operand to be shifted, so that we have its parts
5140 SDOperand InL, InH;
5141 ExpandOp(Op, InL, InH);
5142 switch(Opc) {
5143 case ISD::SHL:
5144 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5145 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5146 return true;
5147 case ISD::SRL:
5148 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5149 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5150 return true;
5151 case ISD::SRA:
5152 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5153 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5154 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5155 return true;
5156 }
5157 }
5158
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005159 // If we know that the high bits of the shift amount are all zero, then we can
5160 // do this as a couple of simple shifts.
5161 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005162 // Compute 32-amt.
5163 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5164 DAG.getConstant(NVTBits, Amt.getValueType()),
5165 Amt);
5166
5167 // Expand the incoming operand to be shifted, so that we have its parts
5168 SDOperand InL, InH;
5169 ExpandOp(Op, InL, InH);
5170 switch(Opc) {
5171 case ISD::SHL:
5172 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5173 Hi = DAG.getNode(ISD::OR, NVT,
5174 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5175 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5176 return true;
5177 case ISD::SRL:
5178 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5179 Lo = DAG.getNode(ISD::OR, NVT,
5180 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5181 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5182 return true;
5183 case ISD::SRA:
5184 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5185 Lo = DAG.getNode(ISD::OR, NVT,
5186 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5187 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5188 return true;
5189 }
5190 }
5191
5192 return false;
5193}
5194
5195
5196// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5197// does not fit into a register, return the lo part and set the hi part to the
5198// by-reg argument. If it does fit into a single register, return the result
5199// and leave the Hi part unset.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005200SDOperand SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005201 bool isSigned, SDOperand &Hi) {
5202 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5203 // The input chain to this libcall is the entry node of the function.
5204 // Legalizing the call will automatically add the previous call to the
5205 // dependence.
5206 SDOperand InChain = DAG.getEntryNode();
5207
5208 TargetLowering::ArgListTy Args;
5209 TargetLowering::ArgListEntry Entry;
5210 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5211 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5212 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
5213 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5214 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005215 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005216 Args.push_back(Entry);
5217 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005218 SDOperand Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
5219 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005220
5221 // Splice the libcall in wherever FindInputOutputChains tells us to.
5222 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
5223 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005224 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5225 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005226
5227 // Legalize the call sequence, starting with the chain. This will advance
5228 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5229 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5230 LegalizeOp(CallInfo.second);
5231 SDOperand Result;
5232 switch (getTypeAction(CallInfo.first.getValueType())) {
5233 default: assert(0 && "Unknown thing");
5234 case Legal:
5235 Result = CallInfo.first;
5236 break;
5237 case Expand:
5238 ExpandOp(CallInfo.first, Result, Hi);
5239 break;
5240 }
5241 return Result;
5242}
5243
5244
5245/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5246///
5247SDOperand SelectionDAGLegalize::
5248ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmanc98645c2008-03-05 01:08:17 +00005249 MVT::ValueType SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005250 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005251
Evan Chengf99a7752008-04-01 02:18:22 +00005252 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5253 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005254 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255 // incoming integer is set. To handle this, we dynamically test to see if
5256 // it is set, and, if so, add a fudge factor.
Dan Gohman8b232ff2008-03-11 01:59:03 +00005257 SDOperand Hi;
5258 if (ExpandSource) {
5259 SDOperand Lo;
5260 ExpandOp(Source, Lo, Hi);
5261 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5262 } else {
5263 // The comparison for the sign bit will use the entire operand.
5264 Hi = Source;
5265 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005266
5267 // If this is unsigned, and not supported, first perform the conversion to
5268 // signed, then adjust the result if the sign bit is set.
Dan Gohman8b232ff2008-03-11 01:59:03 +00005269 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005270
Scott Michel502151f2008-03-10 15:42:14 +00005271 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005272 DAG.getConstant(0, Hi.getValueType()),
5273 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005274 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005275 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5276 SignSet, Four, Zero);
5277 uint64_t FF = 0x5f800000ULL;
5278 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005279 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280
5281 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5282 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5283 SDOperand FudgeInReg;
5284 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005285 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005286 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005287 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005288 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005289 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005290 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005291 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005292 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005293 else
5294 assert(0 && "Unexpected conversion");
5295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 MVT::ValueType SCVT = SignedConv.getValueType();
5297 if (SCVT != DestTy) {
5298 // Destination type needs to be expanded as well. The FADD now we are
5299 // constructing will be expanded into a libcall.
5300 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmanc98645c2008-03-05 01:08:17 +00005301 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5302 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 SignedConv, SignedConv.getValue(1));
5304 }
5305 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5306 }
5307 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5308 }
5309
5310 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005311 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 default: assert(0 && "This action not implemented for this operation!");
5313 case TargetLowering::Legal:
5314 case TargetLowering::Expand:
5315 break; // This case is handled below.
5316 case TargetLowering::Custom: {
5317 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5318 Source), DAG);
5319 if (NV.Val)
5320 return LegalizeOp(NV);
5321 break; // The target decided this was legal after all
5322 }
5323 }
5324
5325 // Expand the source, then glue it back together for the call. We must expand
5326 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005327 if (ExpandSource) {
5328 SDOperand SrcLo, SrcHi;
5329 ExpandOp(Source, SrcLo, SrcHi);
5330 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5331 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332
5333 RTLIB::Libcall LC;
Evan Chenga8740032008-04-01 01:50:16 +00005334 if (SourceVT == MVT::i32) {
5335 if (DestTy == MVT::f32)
Evan Chengcadb43c2008-04-01 02:00:09 +00005336 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Chenga8740032008-04-01 01:50:16 +00005337 else {
5338 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5339 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
5340 }
5341 } else if (SourceVT == MVT::i64) {
Dan Gohmanc98645c2008-03-05 01:08:17 +00005342 if (DestTy == MVT::f32)
5343 LC = RTLIB::SINTTOFP_I64_F32;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005344 else if (DestTy == MVT::f64)
Dan Gohmanc98645c2008-03-05 01:08:17 +00005345 LC = RTLIB::SINTTOFP_I64_F64;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005346 else if (DestTy == MVT::f80)
5347 LC = RTLIB::SINTTOFP_I64_F80;
5348 else {
5349 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5350 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dan Gohmanc98645c2008-03-05 01:08:17 +00005351 }
5352 } else if (SourceVT == MVT::i128) {
5353 if (DestTy == MVT::f32)
5354 LC = RTLIB::SINTTOFP_I128_F32;
5355 else if (DestTy == MVT::f64)
5356 LC = RTLIB::SINTTOFP_I128_F64;
5357 else if (DestTy == MVT::f80)
5358 LC = RTLIB::SINTTOFP_I128_F80;
5359 else {
5360 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5361 LC = RTLIB::SINTTOFP_I128_PPCF128;
5362 }
5363 } else {
5364 assert(0 && "Unknown int value type");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 }
5366
5367 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
5368 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohmanec51f642008-03-10 23:03:31 +00005369 SDOperand HiPart;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005370 SDOperand Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart);
Evan Chenga8740032008-04-01 01:50:16 +00005371 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmanec51f642008-03-10 23:03:31 +00005372 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5373 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005374}
5375
5376/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5377/// INT_TO_FP operation of the specified operand when the target requests that
5378/// we expand it. At this point, we know that the result and operand types are
5379/// legal for the target.
5380SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5381 SDOperand Op0,
5382 MVT::ValueType DestVT) {
5383 if (Op0.getValueType() == MVT::i32) {
5384 // simple 32-bit [signed|unsigned] integer to float/double expansion
5385
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005386 // Get the stack frame index of a 8 byte buffer.
5387 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5388
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 // word offset constant for Hi/Lo address computation
5390 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5391 // set up Hi and Lo (into buffer) address based on endian
5392 SDOperand Hi = StackSlot;
5393 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5394 if (TLI.isLittleEndian())
5395 std::swap(Hi, Lo);
5396
5397 // if signed map to unsigned space
5398 SDOperand Op0Mapped;
5399 if (isSigned) {
5400 // constant used to invert sign bit (signed to unsigned mapping)
5401 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5402 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5403 } else {
5404 Op0Mapped = Op0;
5405 }
5406 // store the lo of the constructed double - based on integer input
5407 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5408 Op0Mapped, Lo, NULL, 0);
5409 // initial hi portion of constructed double
5410 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5411 // store the hi of the constructed double - biased exponent
5412 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5413 // load the constructed double
5414 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5415 // FP constant to bias correct the final result
5416 SDOperand Bias = DAG.getConstantFP(isSigned ?
5417 BitsToDouble(0x4330000080000000ULL)
5418 : BitsToDouble(0x4330000000000000ULL),
5419 MVT::f64);
5420 // subtract the bias
5421 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5422 // final result
5423 SDOperand Result;
5424 // handle final rounding
5425 if (DestVT == MVT::f64) {
5426 // do nothing
5427 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005428 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005429 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5430 DAG.getIntPtrConstant(0));
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005431 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5432 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433 }
5434 return Result;
5435 }
5436 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5437 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5438
Scott Michel502151f2008-03-10 15:42:14 +00005439 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005440 DAG.getConstant(0, Op0.getValueType()),
5441 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005442 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5444 SignSet, Four, Zero);
5445
5446 // If the sign bit of the integer is set, the large number will be treated
5447 // as a negative number. To counteract this, the dynamic code adds an
5448 // offset depending on the data type.
5449 uint64_t FF;
5450 switch (Op0.getValueType()) {
5451 default: assert(0 && "Unsupported integer type!");
5452 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5453 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5454 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5455 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5456 }
5457 if (TLI.isLittleEndian()) FF <<= 32;
5458 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5459
5460 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5461 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5462 SDOperand FudgeInReg;
5463 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005464 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005465 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005466 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005467 FudgeInReg =
5468 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5469 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005470 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005471 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005472 }
5473
5474 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5475}
5476
5477/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5478/// *INT_TO_FP operation of the specified operand when the target requests that
5479/// we promote it. At this point, we know that the result and operand types are
5480/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5481/// operation that takes a larger input.
5482SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5483 MVT::ValueType DestVT,
5484 bool isSigned) {
5485 // First step, figure out the appropriate *INT_TO_FP operation to use.
5486 MVT::ValueType NewInTy = LegalOp.getValueType();
5487
5488 unsigned OpToUse = 0;
5489
5490 // Scan for the appropriate larger type to use.
5491 while (1) {
5492 NewInTy = (MVT::ValueType)(NewInTy+1);
5493 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5494
5495 // If the target supports SINT_TO_FP of this type, use it.
5496 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5497 default: break;
5498 case TargetLowering::Legal:
5499 if (!TLI.isTypeLegal(NewInTy))
5500 break; // Can't use this datatype.
5501 // FALL THROUGH.
5502 case TargetLowering::Custom:
5503 OpToUse = ISD::SINT_TO_FP;
5504 break;
5505 }
5506 if (OpToUse) break;
5507 if (isSigned) continue;
5508
5509 // If the target supports UINT_TO_FP of this type, use it.
5510 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5511 default: break;
5512 case TargetLowering::Legal:
5513 if (!TLI.isTypeLegal(NewInTy))
5514 break; // Can't use this datatype.
5515 // FALL THROUGH.
5516 case TargetLowering::Custom:
5517 OpToUse = ISD::UINT_TO_FP;
5518 break;
5519 }
5520 if (OpToUse) break;
5521
5522 // Otherwise, try a larger type.
5523 }
5524
5525 // Okay, we found the operation and type to use. Zero extend our input to the
5526 // desired type then run the operation on it.
5527 return DAG.getNode(OpToUse, DestVT,
5528 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5529 NewInTy, LegalOp));
5530}
5531
5532/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5533/// FP_TO_*INT operation of the specified operand when the target requests that
5534/// we promote it. At this point, we know that the result and operand types are
5535/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5536/// operation that returns a larger result.
5537SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5538 MVT::ValueType DestVT,
5539 bool isSigned) {
5540 // First step, figure out the appropriate FP_TO*INT operation to use.
5541 MVT::ValueType NewOutTy = DestVT;
5542
5543 unsigned OpToUse = 0;
5544
5545 // Scan for the appropriate larger type to use.
5546 while (1) {
5547 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5548 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5549
5550 // If the target supports FP_TO_SINT returning this type, use it.
5551 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5552 default: break;
5553 case TargetLowering::Legal:
5554 if (!TLI.isTypeLegal(NewOutTy))
5555 break; // Can't use this datatype.
5556 // FALL THROUGH.
5557 case TargetLowering::Custom:
5558 OpToUse = ISD::FP_TO_SINT;
5559 break;
5560 }
5561 if (OpToUse) break;
5562
5563 // If the target supports FP_TO_UINT of this type, use it.
5564 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5565 default: break;
5566 case TargetLowering::Legal:
5567 if (!TLI.isTypeLegal(NewOutTy))
5568 break; // Can't use this datatype.
5569 // FALL THROUGH.
5570 case TargetLowering::Custom:
5571 OpToUse = ISD::FP_TO_UINT;
5572 break;
5573 }
5574 if (OpToUse) break;
5575
5576 // Otherwise, try a larger type.
5577 }
5578
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005579
5580 // Okay, we found the operation and type to use.
5581 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5582
5583 // If the operation produces an invalid type, it must be custom lowered. Use
5584 // the target lowering hooks to expand it. Just keep the low part of the
5585 // expanded operation, we know that we're truncating anyway.
5586 if (getTypeAction(NewOutTy) == Expand) {
5587 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5588 assert(Operation.Val && "Didn't return anything");
5589 }
5590
5591 // Truncate the result of the extended FP_TO_*INT operation to the desired
5592 // size.
5593 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594}
5595
5596/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5597///
5598SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5599 MVT::ValueType VT = Op.getValueType();
5600 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5601 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5602 switch (VT) {
5603 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5604 case MVT::i16:
5605 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5606 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5607 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5608 case MVT::i32:
5609 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5610 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5611 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5612 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5613 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5614 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5615 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5616 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5617 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5618 case MVT::i64:
5619 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5620 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5621 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5622 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5623 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5624 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5625 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5626 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5627 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5628 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5629 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5630 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5631 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5632 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5633 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5634 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5635 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5636 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5637 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5638 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5639 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5640 }
5641}
5642
5643/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5644///
5645SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5646 switch (Opc) {
5647 default: assert(0 && "Cannot expand this yet!");
5648 case ISD::CTPOP: {
5649 static const uint64_t mask[6] = {
5650 0x5555555555555555ULL, 0x3333333333333333ULL,
5651 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5652 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5653 };
5654 MVT::ValueType VT = Op.getValueType();
5655 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5656 unsigned len = MVT::getSizeInBits(VT);
5657 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5658 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5659 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5660 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5661 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5662 DAG.getNode(ISD::AND, VT,
5663 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5664 }
5665 return Op;
5666 }
5667 case ISD::CTLZ: {
5668 // for now, we do this:
5669 // x = x | (x >> 1);
5670 // x = x | (x >> 2);
5671 // ...
5672 // x = x | (x >>16);
5673 // x = x | (x >>32); // for 64-bit input
5674 // return popcount(~x);
5675 //
5676 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5677 MVT::ValueType VT = Op.getValueType();
5678 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5679 unsigned len = MVT::getSizeInBits(VT);
5680 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5681 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5682 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5683 }
5684 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5685 return DAG.getNode(ISD::CTPOP, VT, Op);
5686 }
5687 case ISD::CTTZ: {
5688 // for now, we use: { return popcount(~x & (x - 1)); }
5689 // unless the target has ctlz but not ctpop, in which case we use:
5690 // { return 32 - nlz(~x & (x-1)); }
5691 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5692 MVT::ValueType VT = Op.getValueType();
5693 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5694 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5695 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5696 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5697 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5698 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5699 TLI.isOperationLegal(ISD::CTLZ, VT))
5700 return DAG.getNode(ISD::SUB, VT,
5701 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5702 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5703 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5704 }
5705 }
5706}
5707
5708/// ExpandOp - Expand the specified SDOperand into its two component pieces
5709/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5710/// LegalizeNodes map is filled in for any results that are not expanded, the
5711/// ExpandedNodes map is filled in for any results that are expanded, and the
5712/// Lo/Hi values are returned.
5713void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5714 MVT::ValueType VT = Op.getValueType();
5715 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5716 SDNode *Node = Op.Val;
5717 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5718 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5719 MVT::isVector(VT)) &&
5720 "Cannot expand to FP value or to larger int value!");
5721
5722 // See if we already expanded it.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00005723 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005724 = ExpandedNodes.find(Op);
5725 if (I != ExpandedNodes.end()) {
5726 Lo = I->second.first;
5727 Hi = I->second.second;
5728 return;
5729 }
5730
5731 switch (Node->getOpcode()) {
5732 case ISD::CopyFromReg:
5733 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005734 case ISD::FP_ROUND_INREG:
5735 if (VT == MVT::ppcf128 &&
5736 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5737 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005738 SDOperand SrcLo, SrcHi, Src;
5739 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5740 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5741 SDOperand Result = TLI.LowerOperation(
5742 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005743 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5744 Lo = Result.Val->getOperand(0);
5745 Hi = Result.Val->getOperand(1);
5746 break;
5747 }
5748 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005749 default:
5750#ifndef NDEBUG
5751 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5752#endif
5753 assert(0 && "Do not know how to expand this operator!");
5754 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00005755 case ISD::EXTRACT_ELEMENT:
5756 ExpandOp(Node->getOperand(0), Lo, Hi);
5757 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5758 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00005759 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005760 case ISD::EXTRACT_VECTOR_ELT:
5761 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5762 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5763 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5764 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005765 case ISD::UNDEF:
5766 NVT = TLI.getTypeToExpandTo(VT);
5767 Lo = DAG.getNode(ISD::UNDEF, NVT);
5768 Hi = DAG.getNode(ISD::UNDEF, NVT);
5769 break;
5770 case ISD::Constant: {
Dan Gohman97f1f8e2008-03-03 22:20:46 +00005771 unsigned NVTBits = MVT::getSizeInBits(NVT);
5772 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5773 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5774 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 break;
5776 }
5777 case ISD::ConstantFP: {
5778 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005779 if (CFP->getValueType(0) == MVT::ppcf128) {
5780 APInt api = CFP->getValueAPF().convertToAPInt();
5781 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5782 MVT::f64);
5783 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5784 MVT::f64);
5785 break;
5786 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5788 if (getTypeAction(Lo.getValueType()) == Expand)
5789 ExpandOp(Lo, Lo, Hi);
5790 break;
5791 }
5792 case ISD::BUILD_PAIR:
5793 // Return the operands.
5794 Lo = Node->getOperand(0);
5795 Hi = Node->getOperand(1);
5796 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005797
5798 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005799 if (Node->getNumValues() == 1) {
5800 ExpandOp(Op.getOperand(0), Lo, Hi);
5801 break;
5802 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005803 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5804 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5805 Op.getValue(1).getValueType() == MVT::Other &&
5806 "unhandled MERGE_VALUES");
5807 ExpandOp(Op.getOperand(0), Lo, Hi);
5808 // Remember that we legalized the chain.
5809 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5810 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811
5812 case ISD::SIGN_EXTEND_INREG:
5813 ExpandOp(Node->getOperand(0), Lo, Hi);
5814 // sext_inreg the low part if needed.
5815 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5816
5817 // The high part gets the sign extension from the lo-part. This handles
5818 // things like sextinreg V:i64 from i8.
5819 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5820 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5821 TLI.getShiftAmountTy()));
5822 break;
5823
5824 case ISD::BSWAP: {
5825 ExpandOp(Node->getOperand(0), Lo, Hi);
5826 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5827 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5828 Lo = TempLo;
5829 break;
5830 }
5831
5832 case ISD::CTPOP:
5833 ExpandOp(Node->getOperand(0), Lo, Hi);
5834 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5835 DAG.getNode(ISD::CTPOP, NVT, Lo),
5836 DAG.getNode(ISD::CTPOP, NVT, Hi));
5837 Hi = DAG.getConstant(0, NVT);
5838 break;
5839
5840 case ISD::CTLZ: {
5841 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5842 ExpandOp(Node->getOperand(0), Lo, Hi);
5843 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5844 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Scott Michel502151f2008-03-10 15:42:14 +00005845 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 ISD::SETNE);
5847 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5848 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5849
5850 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5851 Hi = DAG.getConstant(0, NVT);
5852 break;
5853 }
5854
5855 case ISD::CTTZ: {
5856 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5857 ExpandOp(Node->getOperand(0), Lo, Hi);
5858 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5859 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Scott Michel502151f2008-03-10 15:42:14 +00005860 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005861 ISD::SETNE);
5862 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5863 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5864
5865 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5866 Hi = DAG.getConstant(0, NVT);
5867 break;
5868 }
5869
5870 case ISD::VAARG: {
5871 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5872 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5873 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5874 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5875
5876 // Remember that we legalized the chain.
5877 Hi = LegalizeOp(Hi);
5878 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005879 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005880 std::swap(Lo, Hi);
5881 break;
5882 }
5883
5884 case ISD::LOAD: {
5885 LoadSDNode *LD = cast<LoadSDNode>(Node);
5886 SDOperand Ch = LD->getChain(); // Legalize the chain.
5887 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5888 ISD::LoadExtType ExtType = LD->getExtensionType();
5889 int SVOffset = LD->getSrcValueOffset();
5890 unsigned Alignment = LD->getAlignment();
5891 bool isVolatile = LD->isVolatile();
5892
5893 if (ExtType == ISD::NON_EXTLOAD) {
5894 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5895 isVolatile, Alignment);
5896 if (VT == MVT::f32 || VT == MVT::f64) {
5897 // f32->i32 or f64->i64 one to one expansion.
5898 // Remember that we legalized the chain.
5899 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5900 // Recursively expand the new load.
5901 if (getTypeAction(NVT) == Expand)
5902 ExpandOp(Lo, Lo, Hi);
5903 break;
5904 }
5905
5906 // Increment the pointer to the other half.
5907 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5908 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005909 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005911 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005912 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5913 isVolatile, Alignment);
5914
5915 // Build a factor node to remember that this load is independent of the
5916 // other one.
5917 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5918 Hi.getValue(1));
5919
5920 // Remember that we legalized the chain.
5921 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005922 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923 std::swap(Lo, Hi);
5924 } else {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005925 MVT::ValueType EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005926
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005927 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5928 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005929 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5930 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5931 SVOffset, isVolatile, Alignment);
5932 // Remember that we legalized the chain.
5933 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5934 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5935 break;
5936 }
5937
5938 if (EVT == NVT)
5939 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5940 SVOffset, isVolatile, Alignment);
5941 else
5942 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5943 SVOffset, EVT, isVolatile,
5944 Alignment);
5945
5946 // Remember that we legalized the chain.
5947 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5948
5949 if (ExtType == ISD::SEXTLOAD) {
5950 // The high part is obtained by SRA'ing all but one of the bits of the
5951 // lo part.
5952 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5953 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5954 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5955 } else if (ExtType == ISD::ZEXTLOAD) {
5956 // The high part is just a zero.
5957 Hi = DAG.getConstant(0, NVT);
5958 } else /* if (ExtType == ISD::EXTLOAD) */ {
5959 // The high part is undefined.
5960 Hi = DAG.getNode(ISD::UNDEF, NVT);
5961 }
5962 }
5963 break;
5964 }
5965 case ISD::AND:
5966 case ISD::OR:
5967 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5968 SDOperand LL, LH, RL, RH;
5969 ExpandOp(Node->getOperand(0), LL, LH);
5970 ExpandOp(Node->getOperand(1), RL, RH);
5971 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5972 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5973 break;
5974 }
5975 case ISD::SELECT: {
5976 SDOperand LL, LH, RL, RH;
5977 ExpandOp(Node->getOperand(1), LL, LH);
5978 ExpandOp(Node->getOperand(2), RL, RH);
5979 if (getTypeAction(NVT) == Expand)
5980 NVT = TLI.getTypeToExpandTo(NVT);
5981 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5982 if (VT != MVT::f32)
5983 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5984 break;
5985 }
5986 case ISD::SELECT_CC: {
5987 SDOperand TL, TH, FL, FH;
5988 ExpandOp(Node->getOperand(2), TL, TH);
5989 ExpandOp(Node->getOperand(3), FL, FH);
5990 if (getTypeAction(NVT) == Expand)
5991 NVT = TLI.getTypeToExpandTo(NVT);
5992 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5993 Node->getOperand(1), TL, FL, Node->getOperand(4));
5994 if (VT != MVT::f32)
5995 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5996 Node->getOperand(1), TH, FH, Node->getOperand(4));
5997 break;
5998 }
5999 case ISD::ANY_EXTEND:
6000 // The low part is any extension of the input (which degenerates to a copy).
6001 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6002 // The high part is undefined.
6003 Hi = DAG.getNode(ISD::UNDEF, NVT);
6004 break;
6005 case ISD::SIGN_EXTEND: {
6006 // The low part is just a sign extension of the input (which degenerates to
6007 // a copy).
6008 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6009
6010 // The high part is obtained by SRA'ing all but one of the bits of the lo
6011 // part.
6012 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
6013 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6014 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6015 break;
6016 }
6017 case ISD::ZERO_EXTEND:
6018 // The low part is just a zero extension of the input (which degenerates to
6019 // a copy).
6020 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6021
6022 // The high part is just a zero.
6023 Hi = DAG.getConstant(0, NVT);
6024 break;
6025
6026 case ISD::TRUNCATE: {
6027 // The input value must be larger than this value. Expand *it*.
6028 SDOperand NewLo;
6029 ExpandOp(Node->getOperand(0), NewLo, Hi);
6030
6031 // The low part is now either the right size, or it is closer. If not the
6032 // right size, make an illegal truncate so we recursively expand it.
6033 if (NewLo.getValueType() != Node->getValueType(0))
6034 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6035 ExpandOp(NewLo, Lo, Hi);
6036 break;
6037 }
6038
6039 case ISD::BIT_CONVERT: {
6040 SDOperand Tmp;
6041 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6042 // If the target wants to, allow it to lower this itself.
6043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6044 case Expand: assert(0 && "cannot expand FP!");
6045 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6046 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6047 }
6048 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6049 }
6050
6051 // f32 / f64 must be expanded to i32 / i64.
6052 if (VT == MVT::f32 || VT == MVT::f64) {
6053 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6054 if (getTypeAction(NVT) == Expand)
6055 ExpandOp(Lo, Lo, Hi);
6056 break;
6057 }
6058
6059 // If source operand will be expanded to the same type as VT, i.e.
6060 // i64 <- f64, i32 <- f32, expand the source operand instead.
6061 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6062 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6063 ExpandOp(Node->getOperand(0), Lo, Hi);
6064 break;
6065 }
6066
6067 // Turn this into a load/store pair by default.
6068 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006069 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006070
6071 ExpandOp(Tmp, Lo, Hi);
6072 break;
6073 }
6074
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006075 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006076 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6077 TargetLowering::Custom &&
6078 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006079 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6080 assert(Tmp.Val && "Node must be custom expanded!");
6081 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006082 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006083 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006084 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006085 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006086
Andrew Lenharth81580822008-03-05 01:15:49 +00006087 case ISD::ATOMIC_LCS: {
6088 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6089 assert(Tmp.Val && "Node must be custom expanded!");
6090 ExpandOp(Tmp.getValue(0), Lo, Hi);
6091 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6092 LegalizeOp(Tmp.getValue(1)));
6093 break;
6094 }
6095
6096
6097
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006098 // These operators cannot be expanded directly, emit them as calls to
6099 // library functions.
6100 case ISD::FP_TO_SINT: {
6101 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6102 SDOperand Op;
6103 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6104 case Expand: assert(0 && "cannot expand FP!");
6105 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6106 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6107 }
6108
6109 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6110
6111 // Now that the custom expander is done, expand the result, which is still
6112 // VT.
6113 if (Op.Val) {
6114 ExpandOp(Op, Lo, Hi);
6115 break;
6116 }
6117 }
6118
Dale Johannesenac77b272007-10-05 20:04:43 +00006119 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanec51f642008-03-10 23:03:31 +00006120 if (VT == MVT::i64) {
6121 if (Node->getOperand(0).getValueType() == MVT::f32)
6122 LC = RTLIB::FPTOSINT_F32_I64;
6123 else if (Node->getOperand(0).getValueType() == MVT::f64)
6124 LC = RTLIB::FPTOSINT_F64_I64;
6125 else if (Node->getOperand(0).getValueType() == MVT::f80)
6126 LC = RTLIB::FPTOSINT_F80_I64;
6127 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6128 LC = RTLIB::FPTOSINT_PPCF128_I64;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006129 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanec51f642008-03-10 23:03:31 +00006130 } else if (VT == MVT::i128) {
6131 if (Node->getOperand(0).getValueType() == MVT::f32)
6132 LC = RTLIB::FPTOSINT_F32_I128;
6133 else if (Node->getOperand(0).getValueType() == MVT::f64)
6134 LC = RTLIB::FPTOSINT_F64_I128;
6135 else if (Node->getOperand(0).getValueType() == MVT::f80)
6136 LC = RTLIB::FPTOSINT_F80_I128;
6137 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6138 LC = RTLIB::FPTOSINT_PPCF128_I128;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006139 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanec51f642008-03-10 23:03:31 +00006140 } else {
6141 assert(0 && "Unexpected uint-to-fp conversion!");
6142 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006143 break;
6144 }
6145
6146 case ISD::FP_TO_UINT: {
6147 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6148 SDOperand Op;
6149 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6150 case Expand: assert(0 && "cannot expand FP!");
6151 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6152 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6153 }
6154
6155 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6156
6157 // Now that the custom expander is done, expand the result.
6158 if (Op.Val) {
6159 ExpandOp(Op, Lo, Hi);
6160 break;
6161 }
6162 }
6163
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00006164 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanec51f642008-03-10 23:03:31 +00006165 if (VT == MVT::i64) {
6166 if (Node->getOperand(0).getValueType() == MVT::f32)
6167 LC = RTLIB::FPTOUINT_F32_I64;
6168 else if (Node->getOperand(0).getValueType() == MVT::f64)
6169 LC = RTLIB::FPTOUINT_F64_I64;
6170 else if (Node->getOperand(0).getValueType() == MVT::f80)
6171 LC = RTLIB::FPTOUINT_F80_I64;
6172 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6173 LC = RTLIB::FPTOUINT_PPCF128_I64;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006174 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanec51f642008-03-10 23:03:31 +00006175 } else if (VT == MVT::i128) {
6176 if (Node->getOperand(0).getValueType() == MVT::f32)
6177 LC = RTLIB::FPTOUINT_F32_I128;
6178 else if (Node->getOperand(0).getValueType() == MVT::f64)
6179 LC = RTLIB::FPTOUINT_F64_I128;
6180 else if (Node->getOperand(0).getValueType() == MVT::f80)
6181 LC = RTLIB::FPTOUINT_F80_I128;
6182 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6183 LC = RTLIB::FPTOUINT_PPCF128_I128;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006184 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanec51f642008-03-10 23:03:31 +00006185 } else {
6186 assert(0 && "Unexpected uint-to-fp conversion!");
6187 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006188 break;
6189 }
6190
6191 case ISD::SHL: {
6192 // If the target wants custom lowering, do so.
6193 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6194 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6195 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6196 Op = TLI.LowerOperation(Op, DAG);
6197 if (Op.Val) {
6198 // Now that the custom expander is done, expand the result, which is
6199 // still VT.
6200 ExpandOp(Op, Lo, Hi);
6201 break;
6202 }
6203 }
6204
6205 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6206 // this X << 1 as X+X.
6207 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006208 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006209 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6210 SDOperand LoOps[2], HiOps[3];
6211 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6212 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6213 LoOps[1] = LoOps[0];
6214 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6215
6216 HiOps[1] = HiOps[0];
6217 HiOps[2] = Lo.getValue(1);
6218 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6219 break;
6220 }
6221 }
6222
6223 // If we can emit an efficient shift operation, do so now.
6224 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6225 break;
6226
6227 // If this target supports SHL_PARTS, use it.
6228 TargetLowering::LegalizeAction Action =
6229 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6230 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6231 Action == TargetLowering::Custom) {
6232 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6233 break;
6234 }
6235
6236 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006237 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006238 break;
6239 }
6240
6241 case ISD::SRA: {
6242 // If the target wants custom lowering, do so.
6243 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6244 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6245 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6246 Op = TLI.LowerOperation(Op, DAG);
6247 if (Op.Val) {
6248 // Now that the custom expander is done, expand the result, which is
6249 // still VT.
6250 ExpandOp(Op, Lo, Hi);
6251 break;
6252 }
6253 }
6254
6255 // If we can emit an efficient shift operation, do so now.
6256 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6257 break;
6258
6259 // If this target supports SRA_PARTS, use it.
6260 TargetLowering::LegalizeAction Action =
6261 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6262 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6263 Action == TargetLowering::Custom) {
6264 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6265 break;
6266 }
6267
6268 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006269 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006270 break;
6271 }
6272
6273 case ISD::SRL: {
6274 // If the target wants custom lowering, do so.
6275 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6276 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6277 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6278 Op = TLI.LowerOperation(Op, DAG);
6279 if (Op.Val) {
6280 // Now that the custom expander is done, expand the result, which is
6281 // still VT.
6282 ExpandOp(Op, Lo, Hi);
6283 break;
6284 }
6285 }
6286
6287 // If we can emit an efficient shift operation, do so now.
6288 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6289 break;
6290
6291 // If this target supports SRL_PARTS, use it.
6292 TargetLowering::LegalizeAction Action =
6293 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6294 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6295 Action == TargetLowering::Custom) {
6296 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6297 break;
6298 }
6299
6300 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006301 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302 break;
6303 }
6304
6305 case ISD::ADD:
6306 case ISD::SUB: {
6307 // If the target wants to custom expand this, let them.
6308 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6309 TargetLowering::Custom) {
6310 Op = TLI.LowerOperation(Op, DAG);
6311 if (Op.Val) {
6312 ExpandOp(Op, Lo, Hi);
6313 break;
6314 }
6315 }
6316
6317 // Expand the subcomponents.
6318 SDOperand LHSL, LHSH, RHSL, RHSH;
6319 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6320 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6321 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6322 SDOperand LoOps[2], HiOps[3];
6323 LoOps[0] = LHSL;
6324 LoOps[1] = RHSL;
6325 HiOps[0] = LHSH;
6326 HiOps[1] = RHSH;
6327 if (Node->getOpcode() == ISD::ADD) {
6328 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6329 HiOps[2] = Lo.getValue(1);
6330 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6331 } else {
6332 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6333 HiOps[2] = Lo.getValue(1);
6334 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6335 }
6336 break;
6337 }
6338
6339 case ISD::ADDC:
6340 case ISD::SUBC: {
6341 // Expand the subcomponents.
6342 SDOperand LHSL, LHSH, RHSL, RHSH;
6343 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6344 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6345 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6346 SDOperand LoOps[2] = { LHSL, RHSL };
6347 SDOperand HiOps[3] = { LHSH, RHSH };
6348
6349 if (Node->getOpcode() == ISD::ADDC) {
6350 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6351 HiOps[2] = Lo.getValue(1);
6352 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6353 } else {
6354 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6355 HiOps[2] = Lo.getValue(1);
6356 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6357 }
6358 // Remember that we legalized the flag.
6359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6360 break;
6361 }
6362 case ISD::ADDE:
6363 case ISD::SUBE: {
6364 // Expand the subcomponents.
6365 SDOperand LHSL, LHSH, RHSL, RHSH;
6366 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6367 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6368 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6369 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6370 SDOperand HiOps[3] = { LHSH, RHSH };
6371
6372 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6373 HiOps[2] = Lo.getValue(1);
6374 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6375
6376 // Remember that we legalized the flag.
6377 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6378 break;
6379 }
6380 case ISD::MUL: {
6381 // If the target wants to custom expand this, let them.
6382 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6383 SDOperand New = TLI.LowerOperation(Op, DAG);
6384 if (New.Val) {
6385 ExpandOp(New, Lo, Hi);
6386 break;
6387 }
6388 }
6389
6390 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6391 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006392 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6393 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6394 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006395 SDOperand LL, LH, RL, RH;
6396 ExpandOp(Node->getOperand(0), LL, LH);
6397 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006398 unsigned OuterBitSize = Op.getValueSizeInBits();
6399 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006400 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6401 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006402 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6403 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6404 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006405 // The inputs are both zero-extended.
6406 if (HasUMUL_LOHI) {
6407 // We can emit a umul_lohi.
6408 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6409 Hi = SDOperand(Lo.Val, 1);
6410 break;
6411 }
6412 if (HasMULHU) {
6413 // We can emit a mulhu+mul.
6414 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6415 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6416 break;
6417 }
Dan Gohman5a199552007-10-08 18:33:35 +00006418 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006419 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006420 // The input values are both sign-extended.
6421 if (HasSMUL_LOHI) {
6422 // We can emit a smul_lohi.
6423 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6424 Hi = SDOperand(Lo.Val, 1);
6425 break;
6426 }
6427 if (HasMULHS) {
6428 // We can emit a mulhs+mul.
6429 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6430 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6431 break;
6432 }
6433 }
6434 if (HasUMUL_LOHI) {
6435 // Lo,Hi = umul LHS, RHS.
6436 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6437 DAG.getVTList(NVT, NVT), LL, RL);
6438 Lo = UMulLOHI;
6439 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006440 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6441 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6442 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6443 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6444 break;
6445 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006446 if (HasMULHU) {
6447 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6448 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6449 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6450 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6451 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6452 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6453 break;
6454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006455 }
6456
Dan Gohman5a199552007-10-08 18:33:35 +00006457 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006458 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459 break;
6460 }
6461 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006462 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006463 break;
6464 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006465 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006466 break;
6467 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006468 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006469 break;
6470 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006471 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 break;
6473
6474 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006475 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6476 RTLIB::ADD_F64,
6477 RTLIB::ADD_F80,
6478 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006479 Node, false, Hi);
6480 break;
6481 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006482 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6483 RTLIB::SUB_F64,
6484 RTLIB::SUB_F80,
6485 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006486 Node, false, Hi);
6487 break;
6488 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006489 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6490 RTLIB::MUL_F64,
6491 RTLIB::MUL_F80,
6492 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006493 Node, false, Hi);
6494 break;
6495 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006496 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6497 RTLIB::DIV_F64,
6498 RTLIB::DIV_F80,
6499 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006500 Node, false, Hi);
6501 break;
6502 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006503 if (VT == MVT::ppcf128) {
6504 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6505 Node->getOperand(0).getValueType()==MVT::f64);
6506 const uint64_t zero = 0;
6507 if (Node->getOperand(0).getValueType()==MVT::f32)
6508 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6509 else
6510 Hi = Node->getOperand(0);
6511 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6512 break;
6513 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006514 Lo = ExpandLibCall(RTLIB::FPEXT_F32_F64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006515 break;
6516 case ISD::FP_ROUND:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006517 Lo = ExpandLibCall(RTLIB::FPROUND_F64_F32, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006519 case ISD::FPOWI:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006520 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32,
6521 RTLIB::POWI_F64,
6522 RTLIB::POWI_F80,
6523 RTLIB::POWI_PPCF128),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006524 Node, false, Hi);
6525 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006526 case ISD::FSQRT:
6527 case ISD::FSIN:
6528 case ISD::FCOS: {
6529 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6530 switch(Node->getOpcode()) {
6531 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006532 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6533 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006534 break;
6535 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006536 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6537 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006538 break;
6539 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006540 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6541 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006542 break;
6543 default: assert(0 && "Unreachable!");
6544 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006545 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006546 break;
6547 }
6548 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006549 if (VT == MVT::ppcf128) {
6550 SDOperand Tmp;
6551 ExpandOp(Node->getOperand(0), Lo, Tmp);
6552 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6553 // lo = hi==fabs(hi) ? lo : -lo;
6554 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6555 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6556 DAG.getCondCode(ISD::SETEQ));
6557 break;
6558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006559 SDOperand Mask = (VT == MVT::f64)
6560 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6561 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6562 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6563 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6564 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6565 if (getTypeAction(NVT) == Expand)
6566 ExpandOp(Lo, Lo, Hi);
6567 break;
6568 }
6569 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006570 if (VT == MVT::ppcf128) {
6571 ExpandOp(Node->getOperand(0), Lo, Hi);
6572 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6573 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6574 break;
6575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006576 SDOperand Mask = (VT == MVT::f64)
6577 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6578 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6579 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6580 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6581 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6582 if (getTypeAction(NVT) == Expand)
6583 ExpandOp(Lo, Lo, Hi);
6584 break;
6585 }
6586 case ISD::FCOPYSIGN: {
6587 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6588 if (getTypeAction(NVT) == Expand)
6589 ExpandOp(Lo, Lo, Hi);
6590 break;
6591 }
6592 case ISD::SINT_TO_FP:
6593 case ISD::UINT_TO_FP: {
6594 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6595 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00006596
6597 // Promote the operand if needed. Do this before checking for
6598 // ppcf128 so conversions of i16 and i8 work.
6599 if (getTypeAction(SrcVT) == Promote) {
6600 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6601 Tmp = isSigned
6602 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6603 DAG.getValueType(SrcVT))
6604 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6605 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6606 SrcVT = Node->getOperand(0).getValueType();
6607 }
6608
Dan Gohmanec51f642008-03-10 23:03:31 +00006609 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00006610 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00006611 if (isSigned) {
6612 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6613 Node->getOperand(0)));
6614 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6615 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00006616 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00006617 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6618 Node->getOperand(0)));
6619 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6620 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006621 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006622 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6623 DAG.getConstant(0, MVT::i32),
6624 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6625 DAG.getConstantFP(
6626 APFloat(APInt(128, 2, TwoE32)),
6627 MVT::ppcf128)),
6628 Hi,
6629 DAG.getCondCode(ISD::SETLT)),
6630 Lo, Hi);
6631 }
6632 break;
6633 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006634 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6635 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00006636 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006637 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6638 Lo, Hi);
6639 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6640 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6641 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6642 DAG.getConstant(0, MVT::i64),
6643 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6644 DAG.getConstantFP(
6645 APFloat(APInt(128, 2, TwoE64)),
6646 MVT::ppcf128)),
6647 Hi,
6648 DAG.getCondCode(ISD::SETLT)),
6649 Lo, Hi);
6650 break;
6651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006652
Dan Gohmanec51f642008-03-10 23:03:31 +00006653 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6654 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00006655 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00006656 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00006657 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006658 break;
6659 }
6660 }
6661
6662 // Make sure the resultant values have been legalized themselves, unless this
6663 // is a type that requires multi-step expansion.
6664 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6665 Lo = LegalizeOp(Lo);
6666 if (Hi.Val)
6667 // Don't legalize the high part if it is expanded to a single node.
6668 Hi = LegalizeOp(Hi);
6669 }
6670
6671 // Remember in a map if the values will be reused later.
6672 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6673 assert(isNew && "Value already expanded?!?");
6674}
6675
6676/// SplitVectorOp - Given an operand of vector type, break it down into
6677/// two smaller values, still of vector type.
6678void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6679 SDOperand &Hi) {
6680 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6681 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006682 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006684
Dan Gohmana0763d92007-09-24 15:54:53 +00006685 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006686
6687 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6688 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6689
6690 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6691 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006693 // See if we already split it.
6694 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6695 = SplitNodes.find(Op);
6696 if (I != SplitNodes.end()) {
6697 Lo = I->second.first;
6698 Hi = I->second.second;
6699 return;
6700 }
6701
6702 switch (Node->getOpcode()) {
6703 default:
6704#ifndef NDEBUG
6705 Node->dump(&DAG);
6706#endif
6707 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006708 case ISD::UNDEF:
6709 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6710 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6711 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006712 case ISD::BUILD_PAIR:
6713 Lo = Node->getOperand(0);
6714 Hi = Node->getOperand(1);
6715 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006716 case ISD::INSERT_VECTOR_ELT: {
6717 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6718 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6719 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006720 if (Index < NewNumElts_Lo)
6721 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006722 DAG.getIntPtrConstant(Index));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006723 else
Nate Begeman4a365ad2007-11-15 21:15:26 +00006724 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006725 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006726 break;
6727 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006728 case ISD::VECTOR_SHUFFLE: {
6729 // Build the low part.
6730 SDOperand Mask = Node->getOperand(2);
6731 SmallVector<SDOperand, 8> Ops;
6732 MVT::ValueType PtrVT = TLI.getPointerTy();
6733
6734 // Insert all of the elements from the input that are needed. We use
6735 // buildvector of extractelement here because the input vectors will have
6736 // to be legalized, so this makes the code simpler.
6737 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006738 SDOperand IdxNode = Mask.getOperand(i);
6739 if (IdxNode.getOpcode() == ISD::UNDEF) {
6740 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6741 continue;
6742 }
6743 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner587c46d2007-11-19 21:16:54 +00006744 SDOperand InVec = Node->getOperand(0);
6745 if (Idx >= NumElements) {
6746 InVec = Node->getOperand(1);
6747 Idx -= NumElements;
6748 }
6749 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6750 DAG.getConstant(Idx, PtrVT)));
6751 }
6752 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6753 Ops.clear();
6754
6755 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006756 SDOperand IdxNode = Mask.getOperand(i);
6757 if (IdxNode.getOpcode() == ISD::UNDEF) {
6758 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6759 continue;
6760 }
6761 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner587c46d2007-11-19 21:16:54 +00006762 SDOperand InVec = Node->getOperand(0);
6763 if (Idx >= NumElements) {
6764 InVec = Node->getOperand(1);
6765 Idx -= NumElements;
6766 }
6767 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6768 DAG.getConstant(Idx, PtrVT)));
6769 }
6770 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6771 break;
6772 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006773 case ISD::BUILD_VECTOR: {
6774 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006775 Node->op_begin()+NewNumElts_Lo);
6776 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006777
Nate Begeman4a365ad2007-11-15 21:15:26 +00006778 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006779 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006780 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006781 break;
6782 }
6783 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006784 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006785 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6786 if (NewNumSubvectors == 1) {
6787 Lo = Node->getOperand(0);
6788 Hi = Node->getOperand(1);
6789 } else {
6790 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6791 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006792 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006793
6794 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6795 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006796 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006797 }
6798 break;
6799 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006800 case ISD::SELECT: {
6801 SDOperand Cond = Node->getOperand(0);
6802
6803 SDOperand LL, LH, RL, RH;
6804 SplitVectorOp(Node->getOperand(1), LL, LH);
6805 SplitVectorOp(Node->getOperand(2), RL, RH);
6806
6807 if (MVT::isVector(Cond.getValueType())) {
6808 // Handle a vector merge.
6809 SDOperand CL, CH;
6810 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006811 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6812 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006813 } else {
6814 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006815 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6816 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006817 }
6818 break;
6819 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006820 case ISD::ADD:
6821 case ISD::SUB:
6822 case ISD::MUL:
6823 case ISD::FADD:
6824 case ISD::FSUB:
6825 case ISD::FMUL:
6826 case ISD::SDIV:
6827 case ISD::UDIV:
6828 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006829 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830 case ISD::AND:
6831 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006832 case ISD::XOR:
6833 case ISD::UREM:
6834 case ISD::SREM:
6835 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006836 SDOperand LL, LH, RL, RH;
6837 SplitVectorOp(Node->getOperand(0), LL, LH);
6838 SplitVectorOp(Node->getOperand(1), RL, RH);
6839
Nate Begeman4a365ad2007-11-15 21:15:26 +00006840 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6841 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842 break;
6843 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006844 case ISD::FPOWI: {
6845 SDOperand L, H;
6846 SplitVectorOp(Node->getOperand(0), L, H);
6847
Nate Begeman4a365ad2007-11-15 21:15:26 +00006848 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6849 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006850 break;
6851 }
6852 case ISD::CTTZ:
6853 case ISD::CTLZ:
6854 case ISD::CTPOP:
6855 case ISD::FNEG:
6856 case ISD::FABS:
6857 case ISD::FSQRT:
6858 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006859 case ISD::FCOS:
6860 case ISD::FP_TO_SINT:
6861 case ISD::FP_TO_UINT:
6862 case ISD::SINT_TO_FP:
6863 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006864 SDOperand L, H;
6865 SplitVectorOp(Node->getOperand(0), L, H);
6866
Nate Begeman4a365ad2007-11-15 21:15:26 +00006867 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6868 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006869 break;
6870 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006871 case ISD::LOAD: {
6872 LoadSDNode *LD = cast<LoadSDNode>(Node);
6873 SDOperand Ch = LD->getChain();
6874 SDOperand Ptr = LD->getBasePtr();
6875 const Value *SV = LD->getSrcValue();
6876 int SVOffset = LD->getSrcValueOffset();
6877 unsigned Alignment = LD->getAlignment();
6878 bool isVolatile = LD->isVolatile();
6879
Nate Begeman4a365ad2007-11-15 21:15:26 +00006880 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6881 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006882 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006883 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006884 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006885 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006886 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006887
6888 // Build a factor node to remember that this load is independent of the
6889 // other one.
6890 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6891 Hi.getValue(1));
6892
6893 // Remember that we legalized the chain.
6894 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6895 break;
6896 }
6897 case ISD::BIT_CONVERT: {
6898 // We know the result is a vector. The input may be either a vector or a
6899 // scalar value.
6900 SDOperand InOp = Node->getOperand(0);
6901 if (!MVT::isVector(InOp.getValueType()) ||
6902 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6903 // The input is a scalar or single-element vector.
6904 // Lower to a store/load so that it can be split.
6905 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006906 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman20e37962008-02-11 18:58:42 +00006907 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006908
6909 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006910 InOp, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006911 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006912 FI->getIndex());
6913 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00006914 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006915 FI->getIndex());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006916 }
6917 // Split the vector and convert each of the pieces now.
6918 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006919 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6920 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006921 break;
6922 }
6923 }
6924
6925 // Remember in a map if the values will be reused later.
6926 bool isNew =
6927 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6928 assert(isNew && "Value already split?!?");
6929}
6930
6931
6932/// ScalarizeVectorOp - Given an operand of single-element vector type
6933/// (e.g. v1f32), convert it into the equivalent operation that returns a
6934/// scalar (e.g. f32) value.
6935SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6936 assert(MVT::isVector(Op.getValueType()) &&
6937 "Bad ScalarizeVectorOp invocation!");
6938 SDNode *Node = Op.Val;
6939 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6940 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6941
6942 // See if we already scalarized it.
6943 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6944 if (I != ScalarizedNodes.end()) return I->second;
6945
6946 SDOperand Result;
6947 switch (Node->getOpcode()) {
6948 default:
6949#ifndef NDEBUG
6950 Node->dump(&DAG); cerr << "\n";
6951#endif
6952 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6953 case ISD::ADD:
6954 case ISD::FADD:
6955 case ISD::SUB:
6956 case ISD::FSUB:
6957 case ISD::MUL:
6958 case ISD::FMUL:
6959 case ISD::SDIV:
6960 case ISD::UDIV:
6961 case ISD::FDIV:
6962 case ISD::SREM:
6963 case ISD::UREM:
6964 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006965 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006966 case ISD::AND:
6967 case ISD::OR:
6968 case ISD::XOR:
6969 Result = DAG.getNode(Node->getOpcode(),
6970 NewVT,
6971 ScalarizeVectorOp(Node->getOperand(0)),
6972 ScalarizeVectorOp(Node->getOperand(1)));
6973 break;
6974 case ISD::FNEG:
6975 case ISD::FABS:
6976 case ISD::FSQRT:
6977 case ISD::FSIN:
6978 case ISD::FCOS:
6979 Result = DAG.getNode(Node->getOpcode(),
6980 NewVT,
6981 ScalarizeVectorOp(Node->getOperand(0)));
6982 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006983 case ISD::FPOWI:
6984 Result = DAG.getNode(Node->getOpcode(),
6985 NewVT,
6986 ScalarizeVectorOp(Node->getOperand(0)),
6987 Node->getOperand(1));
6988 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006989 case ISD::LOAD: {
6990 LoadSDNode *LD = cast<LoadSDNode>(Node);
6991 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6992 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6993
6994 const Value *SV = LD->getSrcValue();
6995 int SVOffset = LD->getSrcValueOffset();
6996 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6997 LD->isVolatile(), LD->getAlignment());
6998
6999 // Remember that we legalized the chain.
7000 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7001 break;
7002 }
7003 case ISD::BUILD_VECTOR:
7004 Result = Node->getOperand(0);
7005 break;
7006 case ISD::INSERT_VECTOR_ELT:
7007 // Returning the inserted scalar element.
7008 Result = Node->getOperand(1);
7009 break;
7010 case ISD::CONCAT_VECTORS:
7011 assert(Node->getOperand(0).getValueType() == NewVT &&
7012 "Concat of non-legal vectors not yet supported!");
7013 Result = Node->getOperand(0);
7014 break;
7015 case ISD::VECTOR_SHUFFLE: {
7016 // Figure out if the scalar is the LHS or RHS and return it.
7017 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7018 if (cast<ConstantSDNode>(EltNum)->getValue())
7019 Result = ScalarizeVectorOp(Node->getOperand(1));
7020 else
7021 Result = ScalarizeVectorOp(Node->getOperand(0));
7022 break;
7023 }
7024 case ISD::EXTRACT_SUBVECTOR:
7025 Result = Node->getOperand(0);
7026 assert(Result.getValueType() == NewVT);
7027 break;
7028 case ISD::BIT_CONVERT:
7029 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
7030 break;
7031 case ISD::SELECT:
7032 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7033 ScalarizeVectorOp(Op.getOperand(1)),
7034 ScalarizeVectorOp(Op.getOperand(2)));
7035 break;
7036 }
7037
7038 if (TLI.isTypeLegal(NewVT))
7039 Result = LegalizeOp(Result);
7040 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7041 assert(isNew && "Value already scalarized?");
7042 return Result;
7043}
7044
7045
7046// SelectionDAG::Legalize - This is the entry point for the file.
7047//
7048void SelectionDAG::Legalize() {
7049 if (ViewLegalizeDAGs) viewGraph();
7050
7051 /// run - This is the main entry point to this class.
7052 ///
7053 SelectionDAGLegalize(*this).LegalizeDAG();
7054}
7055