blob: fcfe0fef83e7200dec8dedf679226e784a334e9b [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
51class VISIBILITY_HIDDEN SelectionDAGLegalize {
52 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
55 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
60 SDOperand LastCALLSEQ_END;
61
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
67 enum LegalizeAction {
68 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
70 Expand // Try to expand this to other ops, otherwise use a libcall.
71 };
72
73 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
76 TargetLowering::ValueTypeActionImpl ValueTypeActions;
77
78 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +000081 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +000086 DenseMap<SDOperand, SDOperand> PromotedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 /// ExpandedNodes - For nodes that need to be expanded this map indicates
89 /// which which operands are the expanded version of the input. This allows
90 /// us to avoid expanding the same node more than once.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +000091 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 /// SplitNodes - For vector nodes that need to be split, this map indicates
94 /// which which operands are the split version of the input. This allows us
95 /// to avoid splitting the same node more than once.
96 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
97
98 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
100 /// processed to the result.
101 std::map<SDOperand, SDOperand> ScalarizedNodes;
102
103 void AddLegalizedOperand(SDOperand From, SDOperand To) {
104 LegalizedNodes.insert(std::make_pair(From, To));
105 // If someone requests legalization of the new node, return itself.
106 if (From != To)
107 LegalizedNodes.insert(std::make_pair(To, To));
108 }
109 void AddPromotedOperand(SDOperand From, SDOperand To) {
Dan Gohman55d19662008-07-07 17:46:23 +0000110 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 assert(isNew && "Got into the map somehow?");
112 // If someone requests legalization of the new node, return itself.
113 LegalizedNodes.insert(std::make_pair(To, To));
114 }
115
116public:
Dan Gohmane887fdf2008-07-07 18:00:37 +0000117 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118
119 /// getTypeAction - Return how we should legalize values of this type, either
120 /// it is already legal or we need to expand it into multiple registers of
121 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands92c43912008-06-06 12:08:01 +0000122 LegalizeAction getTypeAction(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
124 }
125
126 /// isTypeLegal - Return true if this type is legal on this target.
127 ///
Duncan Sands92c43912008-06-06 12:08:01 +0000128 bool isTypeLegal(MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 return getTypeAction(VT) == Legal;
130 }
131
132 void LegalizeDAG();
133
134private:
135 /// HandleOp - Legalize, Promote, or Expand the specified operand as
136 /// appropriate for its type.
137 void HandleOp(SDOperand Op);
138
139 /// LegalizeOp - We know that the specified value has a legal type.
140 /// Recursively ensure that the operands have legal types, then return the
141 /// result.
142 SDOperand LegalizeOp(SDOperand O);
143
Dan Gohman6d05cac2007-10-11 23:57:53 +0000144 /// UnrollVectorOp - We know that the given vector has a legal type, however
145 /// the operation it performs is not legal and is an operation that we have
146 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
147 /// operating on each element individually.
148 SDOperand UnrollVectorOp(SDOperand O);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000149
150 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
151 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
152 /// is necessary to spill the vector being inserted into to memory, perform
153 /// the insert there, and then read the result back.
154 SDOperand PerformInsertVectorEltInMemory(SDOperand Vec, SDOperand Val,
155 SDOperand Idx);
Dan Gohman6d05cac2007-10-11 23:57:53 +0000156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
162 SDOperand PromoteOp(SDOperand O);
163
164 /// ExpandOp - Expand the specified SDOperand into its two component pieces
165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
170 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
171
172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
174 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
179 SDOperand ScalarizeVectorOp(SDOperand O);
180
Duncan Sandsd3ace282008-07-21 10:20:31 +0000181 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Duncan Sands92c43912008-06-06 12:08:01 +0000190 SDNode *isShuffleLegal(MVT VT, SDOperand Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
194
195 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
196
Duncan Sandsf1db7c82008-04-12 17:14:18 +0000197 SDOperand ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 SDOperand &Hi);
Duncan Sands92c43912008-06-06 12:08:01 +0000199 SDOperand ExpandIntToFP(bool isSigned, MVT DestTy, SDOperand Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
Duncan Sands92c43912008-06-06 12:08:01 +0000201 SDOperand EmitStackConvert(SDOperand SrcOp, MVT SlotVT, MVT DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
203 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Duncan Sands92c43912008-06-06 12:08:01 +0000204 SDOperand ExpandLegalINT_TO_FP(bool isSigned, SDOperand LegalOp, MVT DestVT);
205 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT DestVT, bool isSigned);
206 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
214
215 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217};
218}
219
220/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
221/// specified mask and type. Targets can specify exactly which masks they
222/// support and the code generator is tasked with not creating illegal masks.
223///
224/// Note that this will also return true for shuffles that are promoted to a
225/// different type.
Duncan Sands92c43912008-06-06 12:08:01 +0000226SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDOperand Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
228 default: return 0;
229 case TargetLowering::Legal:
230 case TargetLowering::Custom:
231 break;
232 case TargetLowering::Promote: {
233 // If this is promoted to a different type, convert the shuffle mask and
234 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000235 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000236 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
238 // If we changed # elements, change the shuffle mask.
239 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000240 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
242 if (NumEltsGrowth > 1) {
243 // Renumber the elements.
244 SmallVector<SDOperand, 8> Ops;
245 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
246 SDOperand InOp = Mask.getOperand(i);
247 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
248 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000249 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 else {
251 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000252 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 }
254 }
255 }
256 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
257 }
258 VT = NVT;
259 break;
260 }
261 }
262 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
263}
264
265SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
266 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
267 ValueTypeActions(TLI.getValueTypeActions()) {
268 assert(MVT::LAST_VALUETYPE <= 32 &&
269 "Too many value types for ValueTypeActions to hold!");
270}
271
272/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
273/// contains all of a nodes operands before it contains the node.
274static void ComputeTopDownOrdering(SelectionDAG &DAG,
275 SmallVector<SDNode*, 64> &Order) {
276
277 DenseMap<SDNode*, unsigned> Visited;
278 std::vector<SDNode*> Worklist;
279 Worklist.reserve(128);
280
281 // Compute ordering from all of the leaves in the graphs, those (like the
282 // entry node) that have no operands.
283 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
284 E = DAG.allnodes_end(); I != E; ++I) {
285 if (I->getNumOperands() == 0) {
286 Visited[I] = 0 - 1U;
287 Worklist.push_back(I);
288 }
289 }
290
291 while (!Worklist.empty()) {
292 SDNode *N = Worklist.back();
293 Worklist.pop_back();
294
295 if (++Visited[N] != N->getNumOperands())
296 continue; // Haven't visited all operands yet
297
298 Order.push_back(N);
299
300 // Now that we have N in, add anything that uses it if all of their operands
301 // are now done.
302 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
303 UI != E; ++UI)
Roman Levenstein05650fd2008-04-07 10:06:32 +0000304 Worklist.push_back(UI->getUser());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 }
306
307 assert(Order.size() == Visited.size() &&
Dan Gohman17495de2008-06-20 17:15:19 +0000308 Order.size() == DAG.allnodes_size() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 "Error: DAG is cyclic!");
310}
311
312
313void SelectionDAGLegalize::LegalizeDAG() {
314 LastCALLSEQ_END = DAG.getEntryNode();
315 IsLegalizingCall = false;
316
317 // The legalize process is inherently a bottom-up recursive process (users
318 // legalize their uses before themselves). Given infinite stack space, we
319 // could just start legalizing on the root and traverse the whole graph. In
320 // practice however, this causes us to run out of stack space on large basic
321 // blocks. To avoid this problem, compute an ordering of the nodes where each
322 // node is only legalized after all of its operands are legalized.
323 SmallVector<SDNode*, 64> Order;
324 ComputeTopDownOrdering(DAG, Order);
325
326 for (unsigned i = 0, e = Order.size(); i != e; ++i)
327 HandleOp(SDOperand(Order[i], 0));
328
329 // Finally, it's possible the root changed. Get the new root.
330 SDOperand OldRoot = DAG.getRoot();
331 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
332 DAG.setRoot(LegalizedNodes[OldRoot]);
333
334 ExpandedNodes.clear();
335 LegalizedNodes.clear();
336 PromotedNodes.clear();
337 SplitNodes.clear();
338 ScalarizedNodes.clear();
339
340 // Remove dead nodes now.
341 DAG.RemoveDeadNodes();
342}
343
344
345/// FindCallEndFromCallStart - Given a chained node that is part of a call
346/// sequence, find the CALLSEQ_END node that terminates the call sequence.
347static SDNode *FindCallEndFromCallStart(SDNode *Node) {
348 if (Node->getOpcode() == ISD::CALLSEQ_END)
349 return Node;
350 if (Node->use_empty())
351 return 0; // No CallSeqEnd
352
353 // The chain is usually at the end.
354 SDOperand TheChain(Node, Node->getNumValues()-1);
355 if (TheChain.getValueType() != MVT::Other) {
356 // Sometimes it's at the beginning.
357 TheChain = SDOperand(Node, 0);
358 if (TheChain.getValueType() != MVT::Other) {
359 // Otherwise, hunt for it.
360 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
361 if (Node->getValueType(i) == MVT::Other) {
362 TheChain = SDOperand(Node, i);
363 break;
364 }
365
366 // Otherwise, we walked into a node without a chain.
367 if (TheChain.getValueType() != MVT::Other)
368 return 0;
369 }
370 }
371
372 for (SDNode::use_iterator UI = Node->use_begin(),
373 E = Node->use_end(); UI != E; ++UI) {
374
375 // Make sure to only follow users of our token chain.
Roman Levenstein05650fd2008-04-07 10:06:32 +0000376 SDNode *User = UI->getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
378 if (User->getOperand(i) == TheChain)
379 if (SDNode *Result = FindCallEndFromCallStart(User))
380 return Result;
381 }
382 return 0;
383}
384
385/// FindCallStartFromCallEnd - Given a chained node that is part of a call
386/// sequence, find the CALLSEQ_START node that initiates the call sequence.
387static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
388 assert(Node && "Didn't find callseq_start for a call??");
389 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
390
391 assert(Node->getOperand(0).getValueType() == MVT::Other &&
392 "Node doesn't have a token chain argument!");
393 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
394}
395
396/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
397/// see if any uses can reach Dest. If no dest operands can get to dest,
398/// legalize them, legalize ourself, and return false, otherwise, return true.
399///
400/// Keep track of the nodes we fine that actually do lead to Dest in
401/// NodesLeadingTo. This avoids retraversing them exponential number of times.
402///
403bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
404 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
405 if (N == Dest) return true; // N certainly leads to Dest :)
406
407 // If we've already processed this node and it does lead to Dest, there is no
408 // need to reprocess it.
409 if (NodesLeadingTo.count(N)) return true;
410
411 // If the first result of this node has been already legalized, then it cannot
412 // reach N.
413 switch (getTypeAction(N->getValueType(0))) {
414 case Legal:
415 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
416 break;
417 case Promote:
418 if (PromotedNodes.count(SDOperand(N, 0))) return false;
419 break;
420 case Expand:
421 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
422 break;
423 }
424
425 // Okay, this node has not already been legalized. Check and legalize all
426 // operands. If none lead to Dest, then we can legalize this node.
427 bool OperandsLeadToDest = false;
428 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
429 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
430 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
431
432 if (OperandsLeadToDest) {
433 NodesLeadingTo.insert(N);
434 return true;
435 }
436
437 // Okay, this node looks safe, legalize it and return false.
438 HandleOp(SDOperand(N, 0));
439 return false;
440}
441
442/// HandleOp - Legalize, Promote, or Expand the specified operand as
443/// appropriate for its type.
444void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000445 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 switch (getTypeAction(VT)) {
447 default: assert(0 && "Bad type action!");
448 case Legal: (void)LegalizeOp(Op); break;
449 case Promote: (void)PromoteOp(Op); break;
450 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000451 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 // If this is an illegal scalar, expand it into its two component
453 // pieces.
454 SDOperand X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000455 if (Op.getOpcode() == ISD::TargetConstant)
456 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000458 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 // If this is an illegal single element vector, convert it to a
460 // scalar operation.
461 (void)ScalarizeVectorOp(Op);
462 } else {
463 // Otherwise, this is an illegal multiple element vector.
464 // Split it in half and legalize both parts.
465 SDOperand X, Y;
466 SplitVectorOp(Op, X, Y);
467 }
468 break;
469 }
470}
471
472/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
473/// a load from the constant pool.
474static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
475 SelectionDAG &DAG, TargetLowering &TLI) {
476 bool Extend = false;
477
478 // If a FP immediate is precise when represented as a float and if the
479 // target can do an extending load from float to double, we put it into
480 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000481 // double. This shrinks FP constants and canonicalizes them for targets where
482 // an FP extending load is the same cost as a normal load (such as on the x87
483 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000484 MVT VT = CFP->getValueType(0);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000485 ConstantFP *LLVMC = ConstantFP::get(CFP->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000487 if (VT!=MVT::f64 && VT!=MVT::f32)
488 assert(0 && "Invalid type expansion");
Dan Gohman39509762008-03-11 00:11:06 +0000489 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000490 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 }
492
Duncan Sands92c43912008-06-06 12:08:01 +0000493 MVT OrigVT = VT;
494 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000495 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000496 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000497 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
498 // Only do this if the target has a native EXTLOAD instruction from
499 // smaller type.
Evan Cheng35190fd2008-03-05 01:30:59 +0000500 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000501 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000502 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000503 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
504 VT = SVT;
505 Extend = true;
506 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 }
508
509 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng354be062008-03-04 08:05:30 +0000510 if (Extend)
511 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000512 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Cheng354be062008-03-04 08:05:30 +0000513 0, VT);
514 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
515 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516}
517
518
519/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
520/// operations.
521static
Duncan Sands92c43912008-06-06 12:08:01 +0000522SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000524 MVT VT = Node->getValueType(0);
525 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
527 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000528 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529
530 // First get the sign bit of second operand.
531 SDOperand Mask1 = (SrcVT == MVT::f64)
532 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
533 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
534 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
535 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
536 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
537 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000538 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 if (SizeDiff > 0) {
540 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
541 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
542 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000543 } else if (SizeDiff < 0) {
544 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
545 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
546 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
547 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000569 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000572 if (ST->getMemoryVT().isFloatingPoint() ||
573 ST->getMemoryVT().isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000574 // Expand to a bitconvert of the value to the integer type of the
575 // same size, then a (misaligned) int store.
Duncan Sands92c43912008-06-06 12:08:01 +0000576 MVT intVT;
577 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000578 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000579 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000580 intVT = MVT::i64;
581 else if (VT==MVT::f32)
582 intVT = MVT::i32;
583 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000584 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000585
586 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
587 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
588 SVOffset, ST->isVolatile(), Alignment);
589 }
Duncan Sands92c43912008-06-06 12:08:01 +0000590 assert(ST->getMemoryVT().isInteger() &&
591 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000592 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000593 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000594 MVT NewStoredVT =
595 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
596 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000597 int IncrementSize = NumBits / 8;
598
599 // Divide the stored value in two parts.
600 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
601 SDOperand Lo = Val;
602 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
603
604 // Store the two parts
605 SDOperand Store1, Store2;
606 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
607 ST->getSrcValue(), SVOffset, NewStoredVT,
608 ST->isVolatile(), Alignment);
609 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
610 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000611 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000612 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
613 ST->getSrcValue(), SVOffset + IncrementSize,
614 NewStoredVT, ST->isVolatile(), Alignment);
615
616 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
617}
618
619/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
620static
621SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
622 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000623 int SVOffset = LD->getSrcValueOffset();
624 SDOperand Chain = LD->getChain();
625 SDOperand Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000626 MVT VT = LD->getValueType(0);
627 MVT LoadedVT = LD->getMemoryVT();
628 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000629 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000630 // then bitconvert to floating point or vector.
Duncan Sands92c43912008-06-06 12:08:01 +0000631 MVT intVT;
632 if (LoadedVT.is128BitVector() ||
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000633 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000634 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000635 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000636 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000637 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000638 intVT = MVT::i32;
639 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000640 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000641
642 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
643 SVOffset, LD->isVolatile(),
644 LD->getAlignment());
645 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands92c43912008-06-06 12:08:01 +0000646 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000647 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
648
649 SDOperand Ops[] = { Result, Chain };
Duncan Sands698842f2008-07-02 17:40:58 +0000650 return DAG.getMergeValues(Ops, 2);
Dale Johannesen08275382007-09-08 19:29:23 +0000651 }
Duncan Sands92c43912008-06-06 12:08:01 +0000652 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000653 "Unaligned load of unsupported type.");
654
Dale Johannesendc0ee192008-02-27 22:36:00 +0000655 // Compute the new VT that is half the size of the old one. This is an
656 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000657 unsigned NumBits = LoadedVT.getSizeInBits();
658 MVT NewLoadedVT;
659 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000660 NumBits >>= 1;
661
662 unsigned Alignment = LD->getAlignment();
663 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000664 ISD::LoadExtType HiExtType = LD->getExtensionType();
665
666 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
667 if (HiExtType == ISD::NON_EXTLOAD)
668 HiExtType = ISD::ZEXTLOAD;
669
670 // Load the value in two parts
671 SDOperand Lo, Hi;
672 if (TLI.isLittleEndian()) {
673 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
674 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
675 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
676 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
677 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
678 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000679 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000680 } else {
681 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
682 NewLoadedVT,LD->isVolatile(), Alignment);
683 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
684 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
685 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
686 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000687 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000688 }
689
690 // aggregate the two parts
691 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
692 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
693 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
694
695 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
696 Hi.getValue(1));
697
698 SDOperand Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000699 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000700}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701
Dan Gohman6d05cac2007-10-11 23:57:53 +0000702/// UnrollVectorOp - We know that the given vector has a legal type, however
703/// the operation it performs is not legal and is an operation that we have
704/// no way of lowering. "Unroll" the vector, splitting out the scalars and
705/// operating on each element individually.
706SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000707 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000708 assert(isTypeLegal(VT) &&
709 "Caller should expand or promote operands that are not legal!");
710 assert(Op.Val->getNumValues() == 1 &&
711 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000712 unsigned NE = VT.getVectorNumElements();
713 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000714
715 SmallVector<SDOperand, 8> Scalars;
716 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
717 for (unsigned i = 0; i != NE; ++i) {
718 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
719 SDOperand Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000720 MVT OperandVT = Operand.getValueType();
721 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000722 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000723 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000724 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
725 OperandEltVT,
726 Operand,
727 DAG.getConstant(i, MVT::i32));
728 } else {
729 // A scalar operand; just use it as is.
730 Operands[j] = Operand;
731 }
732 }
733 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
734 &Operands[0], Operands.size()));
735 }
736
737 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
738}
739
Duncan Sands37a3f472008-01-10 10:28:30 +0000740/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000741static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000742 RTLIB::Libcall Call_F32,
743 RTLIB::Libcall Call_F64,
744 RTLIB::Libcall Call_F80,
745 RTLIB::Libcall Call_PPCF128) {
746 return
747 VT == MVT::f32 ? Call_F32 :
748 VT == MVT::f64 ? Call_F64 :
749 VT == MVT::f80 ? Call_F80 :
750 VT == MVT::ppcf128 ? Call_PPCF128 :
751 RTLIB::UNKNOWN_LIBCALL;
752}
753
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000754/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
755/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
756/// is necessary to spill the vector being inserted into to memory, perform
757/// the insert there, and then read the result back.
758SDOperand SelectionDAGLegalize::
759PerformInsertVectorEltInMemory(SDOperand Vec, SDOperand Val, SDOperand Idx) {
760 SDOperand Tmp1 = Vec;
761 SDOperand Tmp2 = Val;
762 SDOperand Tmp3 = Idx;
763
764 // If the target doesn't support this, we have to spill the input vector
765 // to a temporary stack slot, update the element, then reload it. This is
766 // badness. We could also load the value into a vector register (either
767 // with a "move to register" or "extload into register" instruction, then
768 // permute it into place, if the idx is a constant and if the idx is
769 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000770 MVT VT = Tmp1.getValueType();
771 MVT EltVT = VT.getVectorElementType();
772 MVT IdxVT = Tmp3.getValueType();
773 MVT PtrVT = TLI.getPointerTy();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000774 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
775
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000776 int SPFI = cast<FrameIndexSDNode>(StackPtr.Val)->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000777
778 // Store the vector.
779 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000780 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000781
782 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000783 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000784 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
785 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000786 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000787 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
788 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
789 // Store the scalar value.
790 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000791 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000792 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000793 return DAG.getLoad(VT, Ch, StackPtr,
794 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000795}
796
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797/// LegalizeOp - We know that the specified value has a legal type, and
798/// that its operands are legal. Now ensure that the operation itself
799/// is legal, recursively ensuring that the operands' operations remain
800/// legal.
801SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000802 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
803 return Op;
804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 assert(isTypeLegal(Op.getValueType()) &&
806 "Caller should expand or promote operands that are not legal!");
807 SDNode *Node = Op.Val;
808
809 // If this operation defines any values that cannot be represented in a
810 // register on this target, make sure to expand or promote them.
811 if (Node->getNumValues() > 1) {
812 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
813 if (getTypeAction(Node->getValueType(i)) != Legal) {
814 HandleOp(Op.getValue(i));
815 assert(LegalizedNodes.count(Op) &&
816 "Handling didn't add legal operands!");
817 return LegalizedNodes[Op];
818 }
819 }
820
821 // Note that LegalizeOp may be reentered even from single-use nodes, which
822 // means that we always must cache transformed nodes.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000823 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 if (I != LegalizedNodes.end()) return I->second;
825
826 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
827 SDOperand Result = Op;
828 bool isCustom = false;
829
830 switch (Node->getOpcode()) {
831 case ISD::FrameIndex:
832 case ISD::EntryToken:
833 case ISD::Register:
834 case ISD::BasicBlock:
835 case ISD::TargetFrameIndex:
836 case ISD::TargetJumpTable:
837 case ISD::TargetConstant:
838 case ISD::TargetConstantFP:
839 case ISD::TargetConstantPool:
840 case ISD::TargetGlobalAddress:
841 case ISD::TargetGlobalTLSAddress:
842 case ISD::TargetExternalSymbol:
843 case ISD::VALUETYPE:
844 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000845 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +0000847 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000849 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 "This must be legal!");
851 break;
852 default:
853 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
854 // If this is a target node, legalize it by legalizing the operands then
855 // passing it through.
856 SmallVector<SDOperand, 8> Ops;
857 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
858 Ops.push_back(LegalizeOp(Node->getOperand(i)));
859
860 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
861
862 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
863 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
864 return Result.getValue(Op.ResNo);
865 }
866 // Otherwise this is an unhandled builtin node. splat.
867#ifndef NDEBUG
868 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
869#endif
870 assert(0 && "Do not know how to legalize this operator!");
871 abort();
872 case ISD::GLOBAL_OFFSET_TABLE:
873 case ISD::GlobalAddress:
874 case ISD::GlobalTLSAddress:
875 case ISD::ExternalSymbol:
876 case ISD::ConstantPool:
877 case ISD::JumpTable: // Nothing to do.
878 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
879 default: assert(0 && "This action is not supported yet!");
880 case TargetLowering::Custom:
881 Tmp1 = TLI.LowerOperation(Op, DAG);
882 if (Tmp1.Val) Result = Tmp1;
883 // FALLTHROUGH if the target doesn't want to lower this op after all.
884 case TargetLowering::Legal:
885 break;
886 }
887 break;
888 case ISD::FRAMEADDR:
889 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 // The only option for these nodes is to custom lower them. If the target
891 // does not custom lower them, then return zero.
892 Tmp1 = TLI.LowerOperation(Op, DAG);
893 if (Tmp1.Val)
894 Result = Tmp1;
895 else
896 Result = DAG.getConstant(0, TLI.getPointerTy());
897 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000898 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +0000899 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000900 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
901 default: assert(0 && "This action is not supported yet!");
902 case TargetLowering::Custom:
903 Result = TLI.LowerOperation(Op, DAG);
904 if (Result.Val) break;
905 // Fall Thru
906 case TargetLowering::Legal:
907 Result = DAG.getConstant(0, VT);
908 break;
909 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000910 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000911 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 case ISD::EXCEPTIONADDR: {
913 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +0000914 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
916 default: assert(0 && "This action is not supported yet!");
917 case TargetLowering::Expand: {
918 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000919 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 }
921 break;
922 case TargetLowering::Custom:
923 Result = TLI.LowerOperation(Op, DAG);
924 if (Result.Val) break;
925 // Fall Thru
926 case TargetLowering::Legal: {
927 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +0000928 Result = DAG.getMergeValues(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::EHSELECTION: {
946 Tmp1 = LegalizeOp(Node->getOperand(0));
947 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +0000948 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
950 default: assert(0 && "This action is not supported yet!");
951 case TargetLowering::Expand: {
952 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000953 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 }
955 break;
956 case TargetLowering::Custom:
957 Result = TLI.LowerOperation(Op, DAG);
958 if (Result.Val) break;
959 // Fall Thru
960 case TargetLowering::Legal: {
961 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +0000962 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 break;
964 }
965 }
966 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000967 if (Result.Val->getNumValues() == 1) break;
968
969 assert(Result.Val->getNumValues() == 2 &&
970 "Cannot return more than two values!");
971
972 // Since we produced two values, make sure to remember that we
973 // legalized both of them.
974 Tmp1 = LegalizeOp(Result);
975 Tmp2 = LegalizeOp(Result.getValue(1));
976 AddLegalizedOperand(Op.getValue(0), Tmp1);
977 AddLegalizedOperand(Op.getValue(1), Tmp2);
978 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +0000980 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 // The only "good" option for this node is to custom lower it.
982 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
983 default: assert(0 && "This action is not supported at all!");
984 case TargetLowering::Custom:
985 Result = TLI.LowerOperation(Op, DAG);
986 if (Result.Val) break;
987 // Fall Thru
988 case TargetLowering::Legal:
989 // Target does not know, how to lower this, lower to noop
990 Result = LegalizeOp(Node->getOperand(0));
991 break;
992 }
993 }
994 break;
995 case ISD::AssertSext:
996 case ISD::AssertZext:
997 Tmp1 = LegalizeOp(Node->getOperand(0));
998 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
999 break;
1000 case ISD::MERGE_VALUES:
1001 // Legalize eliminates MERGE_VALUES nodes.
1002 Result = Node->getOperand(Op.ResNo);
1003 break;
1004 case ISD::CopyFromReg:
1005 Tmp1 = LegalizeOp(Node->getOperand(0));
1006 Result = Op.getValue(0);
1007 if (Node->getNumValues() == 2) {
1008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1009 } else {
1010 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1011 if (Node->getNumOperands() == 3) {
1012 Tmp2 = LegalizeOp(Node->getOperand(2));
1013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1014 } else {
1015 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1016 }
1017 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1018 }
1019 // Since CopyFromReg produces two values, make sure to remember that we
1020 // legalized both of them.
1021 AddLegalizedOperand(Op.getValue(0), Result);
1022 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1023 return Result.getValue(Op.ResNo);
1024 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001025 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1027 default: assert(0 && "This action is not supported yet!");
1028 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001029 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001031 else if (VT.isFloatingPoint())
1032 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001033 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 else
1035 assert(0 && "Unknown value type!");
1036 break;
1037 case TargetLowering::Legal:
1038 break;
1039 }
1040 break;
1041 }
1042
1043 case ISD::INTRINSIC_W_CHAIN:
1044 case ISD::INTRINSIC_WO_CHAIN:
1045 case ISD::INTRINSIC_VOID: {
1046 SmallVector<SDOperand, 8> Ops;
1047 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1048 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1049 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1050
1051 // Allow the target to custom lower its intrinsics if it wants to.
1052 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1053 TargetLowering::Custom) {
1054 Tmp3 = TLI.LowerOperation(Result, DAG);
1055 if (Tmp3.Val) Result = Tmp3;
1056 }
1057
1058 if (Result.Val->getNumValues() == 1) break;
1059
1060 // Must have return value and chain result.
1061 assert(Result.Val->getNumValues() == 2 &&
1062 "Cannot return more than two values!");
1063
1064 // Since loads produce two values, make sure to remember that we
1065 // legalized both of them.
1066 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1067 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1068 return Result.getValue(Op.ResNo);
1069 }
1070
Dan Gohman472d12c2008-06-30 20:59:49 +00001071 case ISD::DBG_STOPPOINT:
1072 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1074
Dan Gohman472d12c2008-06-30 20:59:49 +00001075 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 case TargetLowering::Promote:
1077 default: assert(0 && "This action is not supported yet!");
1078 case TargetLowering::Expand: {
1079 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1080 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001081 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082
Dan Gohman472d12c2008-06-30 20:59:49 +00001083 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001085 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1086 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087
Dan Gohman472d12c2008-06-30 20:59:49 +00001088 unsigned Line = DSP->getLine();
1089 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090
1091 if (useDEBUG_LOC) {
Evan Chengd6f57682008-07-08 20:06:39 +00001092 SDOperand Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
1093 DAG.getConstant(Col, MVT::i32),
1094 DAG.getConstant(SrcFile, MVT::i32) };
1095 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001097 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001098 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 }
1100 } else {
1101 Result = Tmp1; // chain
1102 }
1103 break;
1104 }
Evan Chengd6f57682008-07-08 20:06:39 +00001105 case TargetLowering::Legal: {
1106 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1107 if (Action == Legal && Tmp1 == Node->getOperand(0))
1108 break;
1109
1110 SmallVector<SDOperand, 8> Ops;
1111 Ops.push_back(Tmp1);
1112 if (Action == Legal) {
1113 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1114 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1115 } else {
1116 // Otherwise promote them.
1117 Ops.push_back(PromoteOp(Node->getOperand(1)));
1118 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 }
Evan Chengd6f57682008-07-08 20:06:39 +00001120 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1121 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1122 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 break;
1124 }
Evan Chengd6f57682008-07-08 20:06:39 +00001125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001127
1128 case ISD::DECLARE:
1129 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1130 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1131 default: assert(0 && "This action is not supported yet!");
1132 case TargetLowering::Legal:
1133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1134 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1135 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1136 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1137 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001138 case TargetLowering::Expand:
1139 Result = LegalizeOp(Node->getOperand(0));
1140 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001141 }
1142 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143
1144 case ISD::DEBUG_LOC:
1145 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1146 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1147 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001148 case TargetLowering::Legal: {
1149 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001151 if (Action == Legal && Tmp1 == Node->getOperand(0))
1152 break;
1153 if (Action == Legal) {
1154 Tmp2 = Node->getOperand(1);
1155 Tmp3 = Node->getOperand(2);
1156 Tmp4 = Node->getOperand(3);
1157 } else {
1158 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1159 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1160 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1161 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1163 break;
1164 }
Evan Chengd6f57682008-07-08 20:06:39 +00001165 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 break;
1167
Dan Gohmanfa607c92008-07-01 00:05:16 +00001168 case ISD::DBG_LABEL:
1169 case ISD::EH_LABEL:
1170 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1171 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 default: assert(0 && "This action is not supported yet!");
1173 case TargetLowering::Legal:
1174 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001175 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 break;
1177 case TargetLowering::Expand:
1178 Result = LegalizeOp(Node->getOperand(0));
1179 break;
1180 }
1181 break;
1182
Evan Chengd1d68072008-03-08 00:58:38 +00001183 case ISD::PREFETCH:
1184 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1185 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1186 default: assert(0 && "This action is not supported yet!");
1187 case TargetLowering::Legal:
1188 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1189 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1190 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1191 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1193 break;
1194 case TargetLowering::Expand:
1195 // It's a noop.
1196 Result = LegalizeOp(Node->getOperand(0));
1197 break;
1198 }
1199 break;
1200
Andrew Lenharth785610d2008-02-16 01:24:58 +00001201 case ISD::MEMBARRIER: {
1202 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001203 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1204 default: assert(0 && "This action is not supported yet!");
1205 case TargetLowering::Legal: {
1206 SDOperand Ops[6];
1207 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001208 for (int x = 1; x < 6; ++x) {
1209 Ops[x] = Node->getOperand(x);
1210 if (!isTypeLegal(Ops[x].getValueType()))
1211 Ops[x] = PromoteOp(Ops[x]);
1212 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001213 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1214 break;
1215 }
1216 case TargetLowering::Expand:
1217 //There is no libgcc call for this op
1218 Result = Node->getOperand(0); // Noop
1219 break;
1220 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001221 break;
1222 }
1223
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001224 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001225 unsigned int num_operands = 4;
1226 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001227 SDOperand Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001228 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001229 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001230 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1231
1232 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1233 default: assert(0 && "This action is not supported yet!");
1234 case TargetLowering::Custom:
1235 Result = TLI.LowerOperation(Result, DAG);
1236 break;
1237 case TargetLowering::Legal:
1238 break;
1239 }
1240 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1241 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1242 return Result.getValue(Op.ResNo);
Duncan Sandsac496a12008-07-04 11:47:58 +00001243 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001244 case ISD::ATOMIC_LOAD_ADD:
1245 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang078a62d2008-05-05 19:05:59 +00001246 case ISD::ATOMIC_LOAD_AND:
1247 case ISD::ATOMIC_LOAD_OR:
1248 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharthaf02d592008-06-14 05:48:15 +00001249 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang078a62d2008-05-05 19:05:59 +00001250 case ISD::ATOMIC_LOAD_MIN:
1251 case ISD::ATOMIC_LOAD_MAX:
1252 case ISD::ATOMIC_LOAD_UMIN:
1253 case ISD::ATOMIC_LOAD_UMAX:
1254 case ISD::ATOMIC_SWAP: {
1255 unsigned int num_operands = 3;
1256 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
1257 SDOperand Ops[3];
1258 for (unsigned int x = 0; x < num_operands; ++x)
1259 Ops[x] = LegalizeOp(Node->getOperand(x));
1260 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001261
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001262 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001263 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001264 case TargetLowering::Custom:
1265 Result = TLI.LowerOperation(Result, DAG);
1266 break;
Mon P Wang078a62d2008-05-05 19:05:59 +00001267 case TargetLowering::Expand:
Duncan Sandsac496a12008-07-04 11:47:58 +00001268 Result = SDOperand(TLI.ReplaceNodeResults(Op.Val, DAG),0);
Mon P Wang078a62d2008-05-05 19:05:59 +00001269 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001270 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001271 break;
1272 }
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001273 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1274 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1275 return Result.getValue(Op.ResNo);
Duncan Sandsac496a12008-07-04 11:47:58 +00001276 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001277 case ISD::Constant: {
1278 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1279 unsigned opAction =
1280 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 // We know we don't need to expand constants here, constants only have one
1283 // value and we check that it is fine above.
1284
Scott Michelf2e2b702007-08-08 23:23:31 +00001285 if (opAction == TargetLowering::Custom) {
1286 Tmp1 = TLI.LowerOperation(Result, DAG);
1287 if (Tmp1.Val)
1288 Result = Tmp1;
1289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 case ISD::ConstantFP: {
1293 // Spill FP immediates to the constant pool if the target cannot directly
1294 // codegen them. Targets often have some immediate values that can be
1295 // efficiently generated into an FP register without a load. We explicitly
1296 // leave these constants as ConstantFP nodes for the target to deal with.
1297 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1300 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001301 case TargetLowering::Legal:
1302 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303 case TargetLowering::Custom:
1304 Tmp3 = TLI.LowerOperation(Result, DAG);
1305 if (Tmp3.Val) {
1306 Result = Tmp3;
1307 break;
1308 }
1309 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001310 case TargetLowering::Expand: {
1311 // Check to see if this FP immediate is already legal.
1312 bool isLegal = false;
1313 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1314 E = TLI.legal_fpimm_end(); I != E; ++I) {
1315 if (CFP->isExactlyValue(*I)) {
1316 isLegal = true;
1317 break;
1318 }
1319 }
1320 // If this is a legal constant, turn it into a TargetConstantFP node.
1321 if (isLegal)
1322 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1324 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001325 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 break;
1327 }
1328 case ISD::TokenFactor:
1329 if (Node->getNumOperands() == 2) {
1330 Tmp1 = LegalizeOp(Node->getOperand(0));
1331 Tmp2 = LegalizeOp(Node->getOperand(1));
1332 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1333 } else if (Node->getNumOperands() == 3) {
1334 Tmp1 = LegalizeOp(Node->getOperand(0));
1335 Tmp2 = LegalizeOp(Node->getOperand(1));
1336 Tmp3 = LegalizeOp(Node->getOperand(2));
1337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1338 } else {
1339 SmallVector<SDOperand, 8> Ops;
1340 // Legalize the operands.
1341 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1342 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1343 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1344 }
1345 break;
1346
1347 case ISD::FORMAL_ARGUMENTS:
1348 case ISD::CALL:
1349 // The only option for this is to custom lower it.
1350 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1351 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001352 // A call within a calling sequence must be legalized to something
1353 // other than the normal CALLSEQ_END. Violating this gets Legalize
1354 // into an infinite loop.
1355 assert ((!IsLegalizingCall ||
1356 Node->getOpcode() != ISD::CALL ||
1357 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1358 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001359
1360 // The number of incoming and outgoing values should match; unless the final
1361 // outgoing value is a flag.
1362 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1363 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1364 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1365 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 "Lowering call/formal_arguments produced unexpected # results!");
1367
1368 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1369 // remember that we legalized all of them, so it doesn't get relegalized.
1370 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001371 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1372 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1374 if (Op.ResNo == i)
1375 Tmp2 = Tmp1;
1376 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1377 }
1378 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001379 case ISD::EXTRACT_SUBREG: {
1380 Tmp1 = LegalizeOp(Node->getOperand(0));
1381 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1382 assert(idx && "Operand must be a constant");
1383 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1384 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1385 }
1386 break;
1387 case ISD::INSERT_SUBREG: {
1388 Tmp1 = LegalizeOp(Node->getOperand(0));
1389 Tmp2 = LegalizeOp(Node->getOperand(1));
1390 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1391 assert(idx && "Operand must be a constant");
1392 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1393 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1394 }
1395 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396 case ISD::BUILD_VECTOR:
1397 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1398 default: assert(0 && "This action is not supported yet!");
1399 case TargetLowering::Custom:
1400 Tmp3 = TLI.LowerOperation(Result, DAG);
1401 if (Tmp3.Val) {
1402 Result = Tmp3;
1403 break;
1404 }
1405 // FALLTHROUGH
1406 case TargetLowering::Expand:
1407 Result = ExpandBUILD_VECTOR(Result.Val);
1408 break;
1409 }
1410 break;
1411 case ISD::INSERT_VECTOR_ELT:
1412 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001414
1415 // The type of the value to insert may not be legal, even though the vector
1416 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1417 // here.
1418 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1419 default: assert(0 && "Cannot expand insert element operand");
1420 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1421 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1422 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1424
1425 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1426 Node->getValueType(0))) {
1427 default: assert(0 && "This action is not supported yet!");
1428 case TargetLowering::Legal:
1429 break;
1430 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001431 Tmp4 = TLI.LowerOperation(Result, DAG);
1432 if (Tmp4.Val) {
1433 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 break;
1435 }
1436 // FALLTHROUGH
1437 case TargetLowering::Expand: {
1438 // If the insert index is a constant, codegen this as a scalar_to_vector,
1439 // then a shuffle that inserts it into the right position in the vector.
1440 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001441 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1442 // match the element type of the vector being created.
1443 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001444 Op.getValueType().getVectorElementType()) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001445 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1446 Tmp1.getValueType(), Tmp2);
1447
Duncan Sands92c43912008-06-06 12:08:01 +00001448 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1449 MVT ShufMaskVT =
1450 MVT::getIntVectorWithNumElements(NumElts);
1451 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001452
1453 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1454 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1455 // elt 0 of the RHS.
1456 SmallVector<SDOperand, 8> ShufOps;
1457 for (unsigned i = 0; i != NumElts; ++i) {
1458 if (i != InsertPos->getValue())
1459 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1460 else
1461 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1462 }
1463 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1464 &ShufOps[0], ShufOps.size());
1465
1466 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1467 Tmp1, ScVec, ShufMask);
1468 Result = LegalizeOp(Result);
1469 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001472 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001473 break;
1474 }
1475 }
1476 break;
1477 case ISD::SCALAR_TO_VECTOR:
1478 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1479 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1480 break;
1481 }
1482
1483 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1484 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1485 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1486 Node->getValueType(0))) {
1487 default: assert(0 && "This action is not supported yet!");
1488 case TargetLowering::Legal:
1489 break;
1490 case TargetLowering::Custom:
1491 Tmp3 = TLI.LowerOperation(Result, DAG);
1492 if (Tmp3.Val) {
1493 Result = Tmp3;
1494 break;
1495 }
1496 // FALLTHROUGH
1497 case TargetLowering::Expand:
1498 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1499 break;
1500 }
1501 break;
1502 case ISD::VECTOR_SHUFFLE:
1503 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1504 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1505 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1506
1507 // Allow targets to custom lower the SHUFFLEs they support.
1508 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1509 default: assert(0 && "Unknown operation action!");
1510 case TargetLowering::Legal:
1511 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1512 "vector shuffle should not be created if not legal!");
1513 break;
1514 case TargetLowering::Custom:
1515 Tmp3 = TLI.LowerOperation(Result, DAG);
1516 if (Tmp3.Val) {
1517 Result = Tmp3;
1518 break;
1519 }
1520 // FALLTHROUGH
1521 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001522 MVT VT = Node->getValueType(0);
1523 MVT EltVT = VT.getVectorElementType();
1524 MVT PtrVT = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 SDOperand Mask = Node->getOperand(2);
1526 unsigned NumElems = Mask.getNumOperands();
1527 SmallVector<SDOperand,8> Ops;
1528 for (unsigned i = 0; i != NumElems; ++i) {
1529 SDOperand Arg = Mask.getOperand(i);
1530 if (Arg.getOpcode() == ISD::UNDEF) {
1531 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1532 } else {
1533 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1534 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1535 if (Idx < NumElems)
1536 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1537 DAG.getConstant(Idx, PtrVT)));
1538 else
1539 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1540 DAG.getConstant(Idx - NumElems, PtrVT)));
1541 }
1542 }
1543 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1544 break;
1545 }
1546 case TargetLowering::Promote: {
1547 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001548 MVT OVT = Node->getValueType(0);
1549 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550
1551 // Cast the two input vectors.
1552 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1553 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1554
1555 // Convert the shuffle mask to the right # elements.
1556 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1557 assert(Tmp3.Val && "Shuffle not legal?");
1558 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1559 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1560 break;
1561 }
1562 }
1563 break;
1564
1565 case ISD::EXTRACT_VECTOR_ELT:
1566 Tmp1 = Node->getOperand(0);
1567 Tmp2 = LegalizeOp(Node->getOperand(1));
1568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1569 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1570 break;
1571
1572 case ISD::EXTRACT_SUBVECTOR:
1573 Tmp1 = Node->getOperand(0);
1574 Tmp2 = LegalizeOp(Node->getOperand(1));
1575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1576 Result = ExpandEXTRACT_SUBVECTOR(Result);
1577 break;
1578
1579 case ISD::CALLSEQ_START: {
1580 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1581
1582 // Recursively Legalize all of the inputs of the call end that do not lead
1583 // to this call start. This ensures that any libcalls that need be inserted
1584 // are inserted *before* the CALLSEQ_START.
1585 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1586 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1587 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1588 NodesLeadingTo);
1589 }
1590
1591 // Now that we legalized all of the inputs (which may have inserted
1592 // libcalls) create the new CALLSEQ_START node.
1593 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1594
1595 // Merge in the last call, to ensure that this call start after the last
1596 // call ended.
1597 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1598 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1599 Tmp1 = LegalizeOp(Tmp1);
1600 }
1601
1602 // Do not try to legalize the target-specific arguments (#1+).
1603 if (Tmp1 != Node->getOperand(0)) {
1604 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1605 Ops[0] = Tmp1;
1606 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1607 }
1608
1609 // Remember that the CALLSEQ_START is legalized.
1610 AddLegalizedOperand(Op.getValue(0), Result);
1611 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1612 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1613
1614 // Now that the callseq_start and all of the non-call nodes above this call
1615 // sequence have been legalized, legalize the call itself. During this
1616 // process, no libcalls can/will be inserted, guaranteeing that no calls
1617 // can overlap.
1618 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001619 // Note that we are selecting this call!
1620 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1621 IsLegalizingCall = true;
1622
1623 // Legalize the call, starting from the CALLSEQ_END.
1624 LegalizeOp(LastCALLSEQ_END);
1625 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1626 return Result;
1627 }
1628 case ISD::CALLSEQ_END:
1629 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1630 // will cause this node to be legalized as well as handling libcalls right.
1631 if (LastCALLSEQ_END.Val != Node) {
1632 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001633 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 assert(I != LegalizedNodes.end() &&
1635 "Legalizing the call start should have legalized this node!");
1636 return I->second;
1637 }
1638
1639 // Otherwise, the call start has been legalized and everything is going
1640 // according to plan. Just legalize ourselves normally here.
1641 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1642 // Do not try to legalize the target-specific arguments (#1+), except for
1643 // an optional flag input.
1644 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1645 if (Tmp1 != Node->getOperand(0)) {
1646 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1647 Ops[0] = Tmp1;
1648 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1649 }
1650 } else {
1651 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1652 if (Tmp1 != Node->getOperand(0) ||
1653 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1654 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1655 Ops[0] = Tmp1;
1656 Ops.back() = Tmp2;
1657 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1658 }
1659 }
1660 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1661 // This finishes up call legalization.
1662 IsLegalizingCall = false;
1663
1664 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1665 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1666 if (Node->getNumValues() == 2)
1667 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1668 return Result.getValue(Op.ResNo);
1669 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001670 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1672 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1673 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1675
1676 Tmp1 = Result.getValue(0);
1677 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001678 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001679 default: assert(0 && "This action is not supported yet!");
1680 case TargetLowering::Expand: {
1681 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1682 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1683 " not tell us which reg is the stack pointer!");
1684 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001685
1686 // Chain the dynamic stack allocation so that it doesn't modify the stack
1687 // pointer when other instructions are using the stack.
1688 Chain = DAG.getCALLSEQ_START(Chain,
1689 DAG.getConstant(0, TLI.getPointerTy()));
1690
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001692 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1693 Chain = SP.getValue(1);
1694 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1695 unsigned StackAlign =
1696 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1697 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001698 SP = DAG.getNode(ISD::AND, VT, SP,
1699 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001700 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001701 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1702
1703 Tmp2 =
1704 DAG.getCALLSEQ_END(Chain,
1705 DAG.getConstant(0, TLI.getPointerTy()),
1706 DAG.getConstant(0, TLI.getPointerTy()),
1707 SDOperand());
1708
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 Tmp1 = LegalizeOp(Tmp1);
1710 Tmp2 = LegalizeOp(Tmp2);
1711 break;
1712 }
1713 case TargetLowering::Custom:
1714 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1715 if (Tmp3.Val) {
1716 Tmp1 = LegalizeOp(Tmp3);
1717 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1718 }
1719 break;
1720 case TargetLowering::Legal:
1721 break;
1722 }
1723 // Since this op produce two values, make sure to remember that we
1724 // legalized both of them.
1725 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1726 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1727 return Op.ResNo ? Tmp2 : Tmp1;
1728 }
1729 case ISD::INLINEASM: {
1730 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1731 bool Changed = false;
1732 // Legalize all of the operands of the inline asm, in case they are nodes
1733 // that need to be expanded or something. Note we skip the asm string and
1734 // all of the TargetConstant flags.
1735 SDOperand Op = LegalizeOp(Ops[0]);
1736 Changed = Op != Ops[0];
1737 Ops[0] = Op;
1738
1739 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1740 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1741 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1742 for (++i; NumVals; ++i, --NumVals) {
1743 SDOperand Op = LegalizeOp(Ops[i]);
1744 if (Op != Ops[i]) {
1745 Changed = true;
1746 Ops[i] = Op;
1747 }
1748 }
1749 }
1750
1751 if (HasInFlag) {
1752 Op = LegalizeOp(Ops.back());
1753 Changed |= Op != Ops.back();
1754 Ops.back() = Op;
1755 }
1756
1757 if (Changed)
1758 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1759
1760 // INLINE asm returns a chain and flag, make sure to add both to the map.
1761 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1762 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1763 return Result.getValue(Op.ResNo);
1764 }
1765 case ISD::BR:
1766 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1767 // Ensure that libcalls are emitted before a branch.
1768 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1769 Tmp1 = LegalizeOp(Tmp1);
1770 LastCALLSEQ_END = DAG.getEntryNode();
1771
1772 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1773 break;
1774 case ISD::BRIND:
1775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1776 // Ensure that libcalls are emitted before a branch.
1777 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1778 Tmp1 = LegalizeOp(Tmp1);
1779 LastCALLSEQ_END = DAG.getEntryNode();
1780
1781 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1782 default: assert(0 && "Indirect target must be legal type (pointer)!");
1783 case Legal:
1784 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1785 break;
1786 }
1787 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1788 break;
1789 case ISD::BR_JT:
1790 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1791 // Ensure that libcalls are emitted before a branch.
1792 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1793 Tmp1 = LegalizeOp(Tmp1);
1794 LastCALLSEQ_END = DAG.getEntryNode();
1795
1796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1797 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1798
1799 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1800 default: assert(0 && "This action is not supported yet!");
1801 case TargetLowering::Legal: break;
1802 case TargetLowering::Custom:
1803 Tmp1 = TLI.LowerOperation(Result, DAG);
1804 if (Tmp1.Val) Result = Tmp1;
1805 break;
1806 case TargetLowering::Expand: {
1807 SDOperand Chain = Result.getOperand(0);
1808 SDOperand Table = Result.getOperand(1);
1809 SDOperand Index = Result.getOperand(2);
1810
Duncan Sands92c43912008-06-06 12:08:01 +00001811 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001812 MachineFunction &MF = DAG.getMachineFunction();
1813 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1814 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1815 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1816
1817 SDOperand LD;
1818 switch (EntrySize) {
1819 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001820 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001821 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001822 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001823 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001824 }
1825
Evan Cheng6fb06762007-11-09 01:32:10 +00001826 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001827 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1828 // For PIC, the sequence is:
1829 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001830 // RelocBase can be JumpTable, GOT or some sort of global base.
1831 if (PTy != MVT::i32)
1832 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1833 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1834 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001836 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001837 }
1838 }
1839 break;
1840 case ISD::BRCOND:
1841 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1842 // Ensure that libcalls are emitted before a return.
1843 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1844 Tmp1 = LegalizeOp(Tmp1);
1845 LastCALLSEQ_END = DAG.getEntryNode();
1846
1847 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1848 case Expand: assert(0 && "It's impossible to expand bools");
1849 case Legal:
1850 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1851 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001852 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001853 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1854
1855 // The top bits of the promoted condition are not necessarily zero, ensure
1856 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001857 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001858 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001859 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001860 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1861 break;
1862 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001863 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864
1865 // Basic block destination (Op#2) is always legal.
1866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1867
1868 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1869 default: assert(0 && "This action is not supported yet!");
1870 case TargetLowering::Legal: break;
1871 case TargetLowering::Custom:
1872 Tmp1 = TLI.LowerOperation(Result, DAG);
1873 if (Tmp1.Val) Result = Tmp1;
1874 break;
1875 case TargetLowering::Expand:
1876 // Expand brcond's setcc into its constituent parts and create a BR_CC
1877 // Node.
1878 if (Tmp2.getOpcode() == ISD::SETCC) {
1879 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1880 Tmp2.getOperand(0), Tmp2.getOperand(1),
1881 Node->getOperand(2));
1882 } else {
1883 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1884 DAG.getCondCode(ISD::SETNE), Tmp2,
1885 DAG.getConstant(0, Tmp2.getValueType()),
1886 Node->getOperand(2));
1887 }
1888 break;
1889 }
1890 break;
1891 case ISD::BR_CC:
1892 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1893 // Ensure that libcalls are emitted before a branch.
1894 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1895 Tmp1 = LegalizeOp(Tmp1);
1896 Tmp2 = Node->getOperand(2); // LHS
1897 Tmp3 = Node->getOperand(3); // RHS
1898 Tmp4 = Node->getOperand(1); // CC
1899
1900 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1901 LastCALLSEQ_END = DAG.getEntryNode();
1902
1903 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1904 // the LHS is a legal SETCC itself. In this case, we need to compare
1905 // the result against zero to select between true and false values.
1906 if (Tmp3.Val == 0) {
1907 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1908 Tmp4 = DAG.getCondCode(ISD::SETNE);
1909 }
1910
1911 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1912 Node->getOperand(4));
1913
1914 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1915 default: assert(0 && "Unexpected action for BR_CC!");
1916 case TargetLowering::Legal: break;
1917 case TargetLowering::Custom:
1918 Tmp4 = TLI.LowerOperation(Result, DAG);
1919 if (Tmp4.Val) Result = Tmp4;
1920 break;
1921 }
1922 break;
1923 case ISD::LOAD: {
1924 LoadSDNode *LD = cast<LoadSDNode>(Node);
1925 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1926 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1927
1928 ISD::LoadExtType ExtType = LD->getExtensionType();
1929 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00001930 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1932 Tmp3 = Result.getValue(0);
1933 Tmp4 = Result.getValue(1);
1934
1935 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1936 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001937 case TargetLowering::Legal:
1938 // If this is an unaligned load and the target doesn't support it,
1939 // expand it.
1940 if (!TLI.allowsUnalignedMemoryAccesses()) {
1941 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00001942 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001943 if (LD->getAlignment() < ABIAlignment){
1944 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1945 TLI);
1946 Tmp3 = Result.getOperand(0);
1947 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001948 Tmp3 = LegalizeOp(Tmp3);
1949 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001950 }
1951 }
1952 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001953 case TargetLowering::Custom:
1954 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1955 if (Tmp1.Val) {
1956 Tmp3 = LegalizeOp(Tmp1);
1957 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1958 }
1959 break;
1960 case TargetLowering::Promote: {
1961 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00001962 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001964 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965
1966 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1967 LD->getSrcValueOffset(),
1968 LD->isVolatile(), LD->getAlignment());
1969 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1970 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1971 break;
1972 }
1973 }
1974 // Since loads produce two values, make sure to remember that we
1975 // legalized both of them.
1976 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1977 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1978 return Op.ResNo ? Tmp4 : Tmp3;
1979 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00001980 MVT SrcVT = LD->getMemoryVT();
1981 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00001982 int SVOffset = LD->getSrcValueOffset();
1983 unsigned Alignment = LD->getAlignment();
1984 bool isVolatile = LD->isVolatile();
1985
Duncan Sands92c43912008-06-06 12:08:01 +00001986 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00001987 // Some targets pretend to have an i1 loading operation, and actually
1988 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1989 // bits are guaranteed to be zero; it helps the optimizers understand
1990 // that these bits are zero. It is also useful for EXTLOAD, since it
1991 // tells the optimizers that those bits are undefined. It would be
1992 // nice to have an effective generic way of getting these benefits...
1993 // Until such a way is found, don't insist on promoting i1 here.
1994 (SrcVT != MVT::i1 ||
1995 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1996 // Promote to a byte-sized load if not loading an integral number of
1997 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00001998 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1999 MVT NVT = MVT::getIntegerVT(NewWidth);
Duncan Sands082524c2008-01-23 20:39:46 +00002000 SDOperand Ch;
2001
2002 // The extra bits are guaranteed to be zero, since we stored them that
2003 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2004
2005 ISD::LoadExtType NewExtType =
2006 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2007
2008 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2009 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2010 NVT, isVolatile, Alignment);
2011
2012 Ch = Result.getValue(1); // The chain.
2013
2014 if (ExtType == ISD::SEXTLOAD)
2015 // Having the top bits zero doesn't help when sign extending.
2016 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2017 Result, DAG.getValueType(SrcVT));
2018 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2019 // All the top bits are guaranteed to be zero - inform the optimizers.
2020 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2021 DAG.getValueType(SrcVT));
2022
2023 Tmp1 = LegalizeOp(Result);
2024 Tmp2 = LegalizeOp(Ch);
2025 } else if (SrcWidth & (SrcWidth - 1)) {
2026 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002027 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002028 "Unsupported extload!");
2029 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2030 assert(RoundWidth < SrcWidth);
2031 unsigned ExtraWidth = SrcWidth - RoundWidth;
2032 assert(ExtraWidth < RoundWidth);
2033 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2034 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002035 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2036 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Duncan Sands082524c2008-01-23 20:39:46 +00002037 SDOperand Lo, Hi, Ch;
2038 unsigned IncrementSize;
2039
2040 if (TLI.isLittleEndian()) {
2041 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2042 // Load the bottom RoundWidth bits.
2043 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2044 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2045 Alignment);
2046
2047 // Load the remaining ExtraWidth bits.
2048 IncrementSize = RoundWidth / 8;
2049 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2050 DAG.getIntPtrConstant(IncrementSize));
2051 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2052 LD->getSrcValue(), SVOffset + IncrementSize,
2053 ExtraVT, isVolatile,
2054 MinAlign(Alignment, IncrementSize));
2055
2056 // Build a factor node to remember that this load is independent of the
2057 // other one.
2058 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2059 Hi.getValue(1));
2060
2061 // Move the top bits to the right place.
2062 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2063 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2064
2065 // Join the hi and lo parts.
2066 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002067 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002068 // Big endian - avoid unaligned loads.
2069 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2070 // Load the top RoundWidth bits.
2071 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2072 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2073 Alignment);
2074
2075 // Load the remaining ExtraWidth bits.
2076 IncrementSize = RoundWidth / 8;
2077 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2078 DAG.getIntPtrConstant(IncrementSize));
2079 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2080 LD->getSrcValue(), SVOffset + IncrementSize,
2081 ExtraVT, isVolatile,
2082 MinAlign(Alignment, IncrementSize));
2083
2084 // Build a factor node to remember that this load is independent of the
2085 // other one.
2086 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2087 Hi.getValue(1));
2088
2089 // Move the top bits to the right place.
2090 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2091 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2092
2093 // Join the hi and lo parts.
2094 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2095 }
2096
2097 Tmp1 = LegalizeOp(Result);
2098 Tmp2 = LegalizeOp(Ch);
2099 } else {
2100 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2101 default: assert(0 && "This action is not supported yet!");
2102 case TargetLowering::Custom:
2103 isCustom = true;
2104 // FALLTHROUGH
2105 case TargetLowering::Legal:
2106 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2107 Tmp1 = Result.getValue(0);
2108 Tmp2 = Result.getValue(1);
2109
2110 if (isCustom) {
2111 Tmp3 = TLI.LowerOperation(Result, DAG);
2112 if (Tmp3.Val) {
2113 Tmp1 = LegalizeOp(Tmp3);
2114 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2115 }
2116 } else {
2117 // If this is an unaligned load and the target doesn't support it,
2118 // expand it.
2119 if (!TLI.allowsUnalignedMemoryAccesses()) {
2120 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002121 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002122 if (LD->getAlignment() < ABIAlignment){
2123 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2124 TLI);
2125 Tmp1 = Result.getOperand(0);
2126 Tmp2 = Result.getOperand(1);
2127 Tmp1 = LegalizeOp(Tmp1);
2128 Tmp2 = LegalizeOp(Tmp2);
2129 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002130 }
2131 }
Duncan Sands082524c2008-01-23 20:39:46 +00002132 break;
2133 case TargetLowering::Expand:
2134 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2135 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2136 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2137 LD->getSrcValueOffset(),
2138 LD->isVolatile(), LD->getAlignment());
2139 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2140 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2141 Tmp2 = LegalizeOp(Load.getValue(1));
2142 break;
2143 }
2144 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2145 // Turn the unsupported load into an EXTLOAD followed by an explicit
2146 // zero/sign extend inreg.
2147 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2148 Tmp1, Tmp2, LD->getSrcValue(),
2149 LD->getSrcValueOffset(), SrcVT,
2150 LD->isVolatile(), LD->getAlignment());
2151 SDOperand ValRes;
2152 if (ExtType == ISD::SEXTLOAD)
2153 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2154 Result, DAG.getValueType(SrcVT));
2155 else
2156 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2157 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2158 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 break;
2160 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 }
Duncan Sands082524c2008-01-23 20:39:46 +00002162
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 // Since loads produce two values, make sure to remember that we legalized
2164 // both of them.
2165 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2166 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2167 return Op.ResNo ? Tmp2 : Tmp1;
2168 }
2169 }
2170 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002171 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002172 switch (getTypeAction(OpTy)) {
2173 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2174 case Legal:
2175 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2176 // 1 -> Hi
2177 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002178 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002179 TLI.getShiftAmountTy()));
2180 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2181 } else {
2182 // 0 -> Lo
2183 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2184 Node->getOperand(0));
2185 }
2186 break;
2187 case Expand:
2188 // Get both the low and high parts.
2189 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2190 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2191 Result = Tmp2; // 1 -> Hi
2192 else
2193 Result = Tmp1; // 0 -> Lo
2194 break;
2195 }
2196 break;
2197 }
2198
2199 case ISD::CopyToReg:
2200 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2201
2202 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2203 "Register type must be legal!");
2204 // Legalize the incoming value (must be a legal type).
2205 Tmp2 = LegalizeOp(Node->getOperand(2));
2206 if (Node->getNumValues() == 1) {
2207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2208 } else {
2209 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2210 if (Node->getNumOperands() == 4) {
2211 Tmp3 = LegalizeOp(Node->getOperand(3));
2212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2213 Tmp3);
2214 } else {
2215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2216 }
2217
2218 // Since this produces two values, make sure to remember that we legalized
2219 // both of them.
2220 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2221 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2222 return Result;
2223 }
2224 break;
2225
2226 case ISD::RET:
2227 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2228
2229 // Ensure that libcalls are emitted before a return.
2230 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2231 Tmp1 = LegalizeOp(Tmp1);
2232 LastCALLSEQ_END = DAG.getEntryNode();
2233
2234 switch (Node->getNumOperands()) {
2235 case 3: // ret val
2236 Tmp2 = Node->getOperand(1);
2237 Tmp3 = Node->getOperand(2); // Signness
2238 switch (getTypeAction(Tmp2.getValueType())) {
2239 case Legal:
2240 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2241 break;
2242 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002243 if (!Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 SDOperand Lo, Hi;
2245 ExpandOp(Tmp2, Lo, Hi);
2246
2247 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002248 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002249 std::swap(Lo, Hi);
2250
2251 if (Hi.Val)
2252 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2253 else
2254 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2255 Result = LegalizeOp(Result);
2256 } else {
2257 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002258 int InIx = Tmp2.ResNo;
Duncan Sands92c43912008-06-06 12:08:01 +00002259 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2260 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261
2262 // Figure out if there is a simple type corresponding to this Vector
2263 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002264 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002265 if (TLI.isTypeLegal(TVT)) {
2266 // Turn this into a return of the vector type.
2267 Tmp2 = LegalizeOp(Tmp2);
2268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2269 } else if (NumElems == 1) {
2270 // Turn this into a return of the scalar type.
2271 Tmp2 = ScalarizeVectorOp(Tmp2);
2272 Tmp2 = LegalizeOp(Tmp2);
2273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2274
2275 // FIXME: Returns of gcc generic vectors smaller than a legal type
2276 // should be returned in integer registers!
2277
2278 // The scalarized value type may not be legal, e.g. it might require
2279 // promotion or expansion. Relegalize the return.
2280 Result = LegalizeOp(Result);
2281 } else {
2282 // FIXME: Returns of gcc generic vectors larger than a legal vector
2283 // type should be returned by reference!
2284 SDOperand Lo, Hi;
2285 SplitVectorOp(Tmp2, Lo, Hi);
2286 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2287 Result = LegalizeOp(Result);
2288 }
2289 }
2290 break;
2291 case Promote:
2292 Tmp2 = PromoteOp(Node->getOperand(1));
2293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2294 Result = LegalizeOp(Result);
2295 break;
2296 }
2297 break;
2298 case 1: // ret void
2299 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2300 break;
2301 default: { // ret <values>
2302 SmallVector<SDOperand, 8> NewValues;
2303 NewValues.push_back(Tmp1);
2304 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2305 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2306 case Legal:
2307 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2308 NewValues.push_back(Node->getOperand(i+1));
2309 break;
2310 case Expand: {
2311 SDOperand Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002312 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002313 "FIXME: TODO: implement returning non-legal vector types!");
2314 ExpandOp(Node->getOperand(i), Lo, Hi);
2315 NewValues.push_back(Lo);
2316 NewValues.push_back(Node->getOperand(i+1));
2317 if (Hi.Val) {
2318 NewValues.push_back(Hi);
2319 NewValues.push_back(Node->getOperand(i+1));
2320 }
2321 break;
2322 }
2323 case Promote:
2324 assert(0 && "Can't promote multiple return value yet!");
2325 }
2326
2327 if (NewValues.size() == Node->getNumOperands())
2328 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2329 else
2330 Result = DAG.getNode(ISD::RET, MVT::Other,
2331 &NewValues[0], NewValues.size());
2332 break;
2333 }
2334 }
2335
2336 if (Result.getOpcode() == ISD::RET) {
2337 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2338 default: assert(0 && "This action is not supported yet!");
2339 case TargetLowering::Legal: break;
2340 case TargetLowering::Custom:
2341 Tmp1 = TLI.LowerOperation(Result, DAG);
2342 if (Tmp1.Val) Result = Tmp1;
2343 break;
2344 }
2345 }
2346 break;
2347 case ISD::STORE: {
2348 StoreSDNode *ST = cast<StoreSDNode>(Node);
2349 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2350 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2351 int SVOffset = ST->getSrcValueOffset();
2352 unsigned Alignment = ST->getAlignment();
2353 bool isVolatile = ST->isVolatile();
2354
2355 if (!ST->isTruncatingStore()) {
2356 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2357 // FIXME: We shouldn't do this for TargetConstantFP's.
2358 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2359 // to phase ordering between legalized code and the dag combiner. This
2360 // probably means that we need to integrate dag combiner and legalizer
2361 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002362 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002363 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002364 if (CFP->getValueType(0) == MVT::f32 &&
2365 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002366 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2367 convertToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002368 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002369 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2370 SVOffset, isVolatile, Alignment);
2371 break;
2372 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002373 // If this target supports 64-bit registers, do a single 64-bit store.
2374 if (getTypeAction(MVT::i64) == Legal) {
2375 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002376 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002377 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2378 SVOffset, isVolatile, Alignment);
2379 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002380 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002381 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2382 // stores. If the target supports neither 32- nor 64-bits, this
2383 // xform is certainly not worth it.
Dan Gohman39509762008-03-11 00:11:06 +00002384 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
2385 SDOperand Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2386 SDOperand Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002387 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002388
2389 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2390 SVOffset, isVolatile, Alignment);
2391 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002392 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002393 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002394 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002395
2396 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2397 break;
2398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400 }
2401
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002402 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 case Legal: {
2404 Tmp3 = LegalizeOp(ST->getValue());
2405 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2406 ST->getOffset());
2407
Duncan Sands92c43912008-06-06 12:08:01 +00002408 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002409 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2410 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002411 case TargetLowering::Legal:
2412 // If this is an unaligned store and the target doesn't support it,
2413 // expand it.
2414 if (!TLI.allowsUnalignedMemoryAccesses()) {
2415 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002416 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002417 if (ST->getAlignment() < ABIAlignment)
2418 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2419 TLI);
2420 }
2421 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 case TargetLowering::Custom:
2423 Tmp1 = TLI.LowerOperation(Result, DAG);
2424 if (Tmp1.Val) Result = Tmp1;
2425 break;
2426 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002427 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2429 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2430 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2431 ST->getSrcValue(), SVOffset, isVolatile,
2432 Alignment);
2433 break;
2434 }
2435 break;
2436 }
2437 case Promote:
2438 // Truncate the value and store the result.
2439 Tmp3 = PromoteOp(ST->getValue());
2440 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002441 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 isVolatile, Alignment);
2443 break;
2444
2445 case Expand:
2446 unsigned IncrementSize = 0;
2447 SDOperand Lo, Hi;
2448
2449 // If this is a vector type, then we have to calculate the increment as
2450 // the product of the element size in bytes, and the number of elements
2451 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002452 if (ST->getValue().getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002454 int InIx = ST->getValue().ResNo;
Duncan Sands92c43912008-06-06 12:08:01 +00002455 MVT InVT = InVal->getValueType(InIx);
2456 unsigned NumElems = InVT.getVectorNumElements();
2457 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002458
2459 // Figure out if there is a simple type corresponding to this Vector
2460 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002461 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462 if (TLI.isTypeLegal(TVT)) {
2463 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002464 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2466 SVOffset, isVolatile, Alignment);
2467 Result = LegalizeOp(Result);
2468 break;
2469 } else if (NumElems == 1) {
2470 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002471 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2473 SVOffset, isVolatile, Alignment);
2474 // The scalarized value type may not be legal, e.g. it might require
2475 // promotion or expansion. Relegalize the scalar store.
2476 Result = LegalizeOp(Result);
2477 break;
2478 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002479 SplitVectorOp(ST->getValue(), Lo, Hi);
Duncan Sands92c43912008-06-06 12:08:01 +00002480 IncrementSize = Lo.Val->getValueType(0).getVectorNumElements() *
2481 EVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 }
2483 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002484 ExpandOp(ST->getValue(), Lo, Hi);
Duncan Sands92c43912008-06-06 12:08:01 +00002485 IncrementSize = Hi.Val ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002487 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 std::swap(Lo, Hi);
2489 }
2490
2491 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2492 SVOffset, isVolatile, Alignment);
2493
2494 if (Hi.Val == NULL) {
2495 // Must be int <-> float one-to-one expansion.
2496 Result = Lo;
2497 break;
2498 }
2499
2500 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002501 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002502 assert(isTypeLegal(Tmp2.getValueType()) &&
2503 "Pointers must be legal!");
2504 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002505 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002506 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2507 SVOffset, isVolatile, Alignment);
2508 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2509 break;
2510 }
2511 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002512 switch (getTypeAction(ST->getValue().getValueType())) {
2513 case Legal:
2514 Tmp3 = LegalizeOp(ST->getValue());
2515 break;
2516 case Promote:
2517 // We can promote the value, the truncstore will still take care of it.
2518 Tmp3 = PromoteOp(ST->getValue());
2519 break;
2520 case Expand:
2521 // Just store the low part. This may become a non-trunc store, so make
2522 // sure to use getTruncStore, not UpdateNodeOperands below.
2523 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2524 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2525 SVOffset, MVT::i8, isVolatile, Alignment);
2526 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527
Duncan Sands92c43912008-06-06 12:08:01 +00002528 MVT StVT = ST->getMemoryVT();
2529 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002530
Duncan Sands92c43912008-06-06 12:08:01 +00002531 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002532 // Promote to a byte-sized store with upper bits zero if not
2533 // storing an integral number of bytes. For example, promote
2534 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002535 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002536 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2537 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2538 SVOffset, NVT, isVolatile, Alignment);
2539 } else if (StWidth & (StWidth - 1)) {
2540 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002541 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002542 "Unsupported truncstore!");
2543 unsigned RoundWidth = 1 << Log2_32(StWidth);
2544 assert(RoundWidth < StWidth);
2545 unsigned ExtraWidth = StWidth - RoundWidth;
2546 assert(ExtraWidth < RoundWidth);
2547 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2548 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002549 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2550 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Duncan Sands40676662008-01-22 07:17:34 +00002551 SDOperand Lo, Hi;
2552 unsigned IncrementSize;
2553
2554 if (TLI.isLittleEndian()) {
2555 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2556 // Store the bottom RoundWidth bits.
2557 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2558 SVOffset, RoundVT,
2559 isVolatile, Alignment);
2560
2561 // Store the remaining ExtraWidth bits.
2562 IncrementSize = RoundWidth / 8;
2563 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2564 DAG.getIntPtrConstant(IncrementSize));
2565 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2566 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2567 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2568 SVOffset + IncrementSize, ExtraVT, isVolatile,
2569 MinAlign(Alignment, IncrementSize));
2570 } else {
2571 // Big endian - avoid unaligned stores.
2572 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2573 // Store the top RoundWidth bits.
2574 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2575 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2576 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2577 RoundVT, isVolatile, Alignment);
2578
2579 // Store the remaining ExtraWidth bits.
2580 IncrementSize = RoundWidth / 8;
2581 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2582 DAG.getIntPtrConstant(IncrementSize));
2583 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2584 SVOffset + IncrementSize, ExtraVT, isVolatile,
2585 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002586 }
Duncan Sands40676662008-01-22 07:17:34 +00002587
2588 // The order of the stores doesn't matter.
2589 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2590 } else {
2591 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2592 Tmp2 != ST->getBasePtr())
2593 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2594 ST->getOffset());
2595
2596 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2597 default: assert(0 && "This action is not supported yet!");
2598 case TargetLowering::Legal:
2599 // If this is an unaligned store and the target doesn't support it,
2600 // expand it.
2601 if (!TLI.allowsUnalignedMemoryAccesses()) {
2602 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002603 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002604 if (ST->getAlignment() < ABIAlignment)
2605 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2606 TLI);
2607 }
2608 break;
2609 case TargetLowering::Custom:
2610 Result = TLI.LowerOperation(Result, DAG);
2611 break;
2612 case Expand:
2613 // TRUNCSTORE:i16 i32 -> STORE i16
2614 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2615 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2616 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2617 isVolatile, Alignment);
2618 break;
2619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 }
2621 }
2622 break;
2623 }
2624 case ISD::PCMARKER:
2625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2626 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2627 break;
2628 case ISD::STACKSAVE:
2629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2630 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2631 Tmp1 = Result.getValue(0);
2632 Tmp2 = Result.getValue(1);
2633
2634 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2635 default: assert(0 && "This action is not supported yet!");
2636 case TargetLowering::Legal: break;
2637 case TargetLowering::Custom:
2638 Tmp3 = TLI.LowerOperation(Result, DAG);
2639 if (Tmp3.Val) {
2640 Tmp1 = LegalizeOp(Tmp3);
2641 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2642 }
2643 break;
2644 case TargetLowering::Expand:
2645 // Expand to CopyFromReg if the target set
2646 // StackPointerRegisterToSaveRestore.
2647 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2648 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2649 Node->getValueType(0));
2650 Tmp2 = Tmp1.getValue(1);
2651 } else {
2652 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2653 Tmp2 = Node->getOperand(0);
2654 }
2655 break;
2656 }
2657
2658 // Since stacksave produce two values, make sure to remember that we
2659 // legalized both of them.
2660 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2661 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2662 return Op.ResNo ? Tmp2 : Tmp1;
2663
2664 case ISD::STACKRESTORE:
2665 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2666 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2667 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2668
2669 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2670 default: assert(0 && "This action is not supported yet!");
2671 case TargetLowering::Legal: break;
2672 case TargetLowering::Custom:
2673 Tmp1 = TLI.LowerOperation(Result, DAG);
2674 if (Tmp1.Val) Result = Tmp1;
2675 break;
2676 case TargetLowering::Expand:
2677 // Expand to CopyToReg if the target set
2678 // StackPointerRegisterToSaveRestore.
2679 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2680 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2681 } else {
2682 Result = Tmp1;
2683 }
2684 break;
2685 }
2686 break;
2687
2688 case ISD::READCYCLECOUNTER:
2689 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2690 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2691 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2692 Node->getValueType(0))) {
2693 default: assert(0 && "This action is not supported yet!");
2694 case TargetLowering::Legal:
2695 Tmp1 = Result.getValue(0);
2696 Tmp2 = Result.getValue(1);
2697 break;
2698 case TargetLowering::Custom:
2699 Result = TLI.LowerOperation(Result, DAG);
2700 Tmp1 = LegalizeOp(Result.getValue(0));
2701 Tmp2 = LegalizeOp(Result.getValue(1));
2702 break;
2703 }
2704
2705 // Since rdcc produce two values, make sure to remember that we legalized
2706 // both of them.
2707 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2708 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2709 return Result;
2710
2711 case ISD::SELECT:
2712 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2713 case Expand: assert(0 && "It's impossible to expand bools");
2714 case Legal:
2715 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2716 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002717 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2719 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002720 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002722 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2724 break;
2725 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002726 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002727 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2728 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2729
2730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2731
2732 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2733 default: assert(0 && "This action is not supported yet!");
2734 case TargetLowering::Legal: break;
2735 case TargetLowering::Custom: {
2736 Tmp1 = TLI.LowerOperation(Result, DAG);
2737 if (Tmp1.Val) Result = Tmp1;
2738 break;
2739 }
2740 case TargetLowering::Expand:
2741 if (Tmp1.getOpcode() == ISD::SETCC) {
2742 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2743 Tmp2, Tmp3,
2744 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2745 } else {
2746 Result = DAG.getSelectCC(Tmp1,
2747 DAG.getConstant(0, Tmp1.getValueType()),
2748 Tmp2, Tmp3, ISD::SETNE);
2749 }
2750 break;
2751 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002752 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2754 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002755 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 ExtOp = ISD::BIT_CONVERT;
2757 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002758 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 ExtOp = ISD::ANY_EXTEND;
2760 TruncOp = ISD::TRUNCATE;
2761 } else {
2762 ExtOp = ISD::FP_EXTEND;
2763 TruncOp = ISD::FP_ROUND;
2764 }
2765 // Promote each of the values to the new type.
2766 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2767 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2768 // Perform the larger operation, then round down.
2769 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002770 if (TruncOp != ISD::FP_ROUND)
2771 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2772 else
2773 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2774 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775 break;
2776 }
2777 }
2778 break;
2779 case ISD::SELECT_CC: {
2780 Tmp1 = Node->getOperand(0); // LHS
2781 Tmp2 = Node->getOperand(1); // RHS
2782 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2783 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2784 SDOperand CC = Node->getOperand(4);
2785
2786 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2787
2788 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2789 // the LHS is a legal SETCC itself. In this case, we need to compare
2790 // the result against zero to select between true and false values.
2791 if (Tmp2.Val == 0) {
2792 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2793 CC = DAG.getCondCode(ISD::SETNE);
2794 }
2795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2796
2797 // Everything is legal, see if we should expand this op or something.
2798 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2799 default: assert(0 && "This action is not supported yet!");
2800 case TargetLowering::Legal: break;
2801 case TargetLowering::Custom:
2802 Tmp1 = TLI.LowerOperation(Result, DAG);
2803 if (Tmp1.Val) Result = Tmp1;
2804 break;
2805 }
2806 break;
2807 }
2808 case ISD::SETCC:
2809 Tmp1 = Node->getOperand(0);
2810 Tmp2 = Node->getOperand(1);
2811 Tmp3 = Node->getOperand(2);
2812 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2813
2814 // If we had to Expand the SetCC operands into a SELECT node, then it may
2815 // not always be possible to return a true LHS & RHS. In this case, just
2816 // return the value we legalized, returned in the LHS
2817 if (Tmp2.Val == 0) {
2818 Result = Tmp1;
2819 break;
2820 }
2821
2822 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2823 default: assert(0 && "Cannot handle this action for SETCC yet!");
2824 case TargetLowering::Custom:
2825 isCustom = true;
2826 // FALLTHROUGH.
2827 case TargetLowering::Legal:
2828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2829 if (isCustom) {
2830 Tmp4 = TLI.LowerOperation(Result, DAG);
2831 if (Tmp4.Val) Result = Tmp4;
2832 }
2833 break;
2834 case TargetLowering::Promote: {
2835 // First step, figure out the appropriate operation to use.
2836 // Allow SETCC to not be supported for all legal data types
2837 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00002838 MVT NewInTy = Node->getOperand(0).getValueType();
2839 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840
2841 // Scan for the appropriate larger type to use.
2842 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002843 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002844
Duncan Sands92c43912008-06-06 12:08:01 +00002845 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002846 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00002847 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848 "Fell off of the edge of the floating point world");
2849
2850 // If the target supports SETCC of this type, use it.
2851 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2852 break;
2853 }
Duncan Sands92c43912008-06-06 12:08:01 +00002854 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 assert(0 && "Cannot promote Legal Integer SETCC yet");
2856 else {
2857 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2858 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2859 }
2860 Tmp1 = LegalizeOp(Tmp1);
2861 Tmp2 = LegalizeOp(Tmp2);
2862 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2863 Result = LegalizeOp(Result);
2864 break;
2865 }
2866 case TargetLowering::Expand:
2867 // Expand a setcc node into a select_cc of the same condition, lhs, and
2868 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00002869 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2871 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2872 Tmp3);
2873 break;
2874 }
2875 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00002876 case ISD::VSETCC: {
2877 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2878 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2879 SDOperand CC = Node->getOperand(2);
2880
2881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
2882
2883 // Everything is legal, see if we should expand this op or something.
2884 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
2885 default: assert(0 && "This action is not supported yet!");
2886 case TargetLowering::Legal: break;
2887 case TargetLowering::Custom:
2888 Tmp1 = TLI.LowerOperation(Result, DAG);
2889 if (Tmp1.Val) Result = Tmp1;
2890 break;
2891 }
2892 break;
2893 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894
2895 case ISD::SHL_PARTS:
2896 case ISD::SRA_PARTS:
2897 case ISD::SRL_PARTS: {
2898 SmallVector<SDOperand, 8> Ops;
2899 bool Changed = false;
2900 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2901 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2902 Changed |= Ops.back() != Node->getOperand(i);
2903 }
2904 if (Changed)
2905 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2906
2907 switch (TLI.getOperationAction(Node->getOpcode(),
2908 Node->getValueType(0))) {
2909 default: assert(0 && "This action is not supported yet!");
2910 case TargetLowering::Legal: break;
2911 case TargetLowering::Custom:
2912 Tmp1 = TLI.LowerOperation(Result, DAG);
2913 if (Tmp1.Val) {
2914 SDOperand Tmp2, RetVal(0, 0);
2915 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2916 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2917 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2918 if (i == Op.ResNo)
2919 RetVal = Tmp2;
2920 }
2921 assert(RetVal.Val && "Illegal result number");
2922 return RetVal;
2923 }
2924 break;
2925 }
2926
2927 // Since these produce multiple values, make sure to remember that we
2928 // legalized all of them.
2929 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2930 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2931 return Result.getValue(Op.ResNo);
2932 }
2933
2934 // Binary operators
2935 case ISD::ADD:
2936 case ISD::SUB:
2937 case ISD::MUL:
2938 case ISD::MULHS:
2939 case ISD::MULHU:
2940 case ISD::UDIV:
2941 case ISD::SDIV:
2942 case ISD::AND:
2943 case ISD::OR:
2944 case ISD::XOR:
2945 case ISD::SHL:
2946 case ISD::SRL:
2947 case ISD::SRA:
2948 case ISD::FADD:
2949 case ISD::FSUB:
2950 case ISD::FMUL:
2951 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002952 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2954 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2955 case Expand: assert(0 && "Not possible");
2956 case Legal:
2957 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2958 break;
2959 case Promote:
2960 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2961 break;
2962 }
2963
2964 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2965
2966 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2967 default: assert(0 && "BinOp legalize operation not supported");
2968 case TargetLowering::Legal: break;
2969 case TargetLowering::Custom:
2970 Tmp1 = TLI.LowerOperation(Result, DAG);
2971 if (Tmp1.Val) Result = Tmp1;
2972 break;
2973 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00002974 MVT VT = Op.getValueType();
Dan Gohman5a199552007-10-08 18:33:35 +00002975
2976 // See if multiply or divide can be lowered using two-result operations.
2977 SDVTList VTs = DAG.getVTList(VT, VT);
2978 if (Node->getOpcode() == ISD::MUL) {
2979 // We just need the low half of the multiply; try both the signed
2980 // and unsigned forms. If the target supports both SMUL_LOHI and
2981 // UMUL_LOHI, form a preference by checking which forms of plain
2982 // MULH it supports.
2983 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2984 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2985 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2986 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2987 unsigned OpToUse = 0;
2988 if (HasSMUL_LOHI && !HasMULHS) {
2989 OpToUse = ISD::SMUL_LOHI;
2990 } else if (HasUMUL_LOHI && !HasMULHU) {
2991 OpToUse = ISD::UMUL_LOHI;
2992 } else if (HasSMUL_LOHI) {
2993 OpToUse = ISD::SMUL_LOHI;
2994 } else if (HasUMUL_LOHI) {
2995 OpToUse = ISD::UMUL_LOHI;
2996 }
2997 if (OpToUse) {
2998 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2999 break;
3000 }
3001 }
3002 if (Node->getOpcode() == ISD::MULHS &&
3003 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3004 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3005 break;
3006 }
3007 if (Node->getOpcode() == ISD::MULHU &&
3008 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3009 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3010 break;
3011 }
3012 if (Node->getOpcode() == ISD::SDIV &&
3013 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3014 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3015 break;
3016 }
3017 if (Node->getOpcode() == ISD::UDIV &&
3018 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3019 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3020 break;
3021 }
3022
Dan Gohman6d05cac2007-10-11 23:57:53 +00003023 // Check to see if we have a libcall for this operator.
3024 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3025 bool isSigned = false;
3026 switch (Node->getOpcode()) {
3027 case ISD::UDIV:
3028 case ISD::SDIV:
3029 if (VT == MVT::i32) {
3030 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003032 isSigned = Node->getOpcode() == ISD::SDIV;
3033 }
3034 break;
3035 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003036 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3037 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003038 break;
3039 default: break;
3040 }
3041 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3042 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003043 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 break;
3045 }
3046
Duncan Sands92c43912008-06-06 12:08:01 +00003047 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 "Cannot expand this binary operator!");
3049 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003050 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 break;
3052 }
3053 case TargetLowering::Promote: {
3054 switch (Node->getOpcode()) {
3055 default: assert(0 && "Do not know how to promote this BinOp!");
3056 case ISD::AND:
3057 case ISD::OR:
3058 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003059 MVT OVT = Node->getValueType(0);
3060 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3061 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 // Bit convert each of the values to the new type.
3063 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3064 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3065 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3066 // Bit convert the result back the original type.
3067 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3068 break;
3069 }
3070 }
3071 }
3072 }
3073 break;
3074
Dan Gohman475cd732007-10-05 14:17:22 +00003075 case ISD::SMUL_LOHI:
3076 case ISD::UMUL_LOHI:
3077 case ISD::SDIVREM:
3078 case ISD::UDIVREM:
3079 // These nodes will only be produced by target-specific lowering, so
3080 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003081 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003082 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003083
3084 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3085 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3086 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003087 break;
3088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3090 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3091 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3092 case Expand: assert(0 && "Not possible");
3093 case Legal:
3094 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3095 break;
3096 case Promote:
3097 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3098 break;
3099 }
3100
3101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3102
3103 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3104 default: assert(0 && "Operation not supported");
3105 case TargetLowering::Custom:
3106 Tmp1 = TLI.LowerOperation(Result, DAG);
3107 if (Tmp1.Val) Result = Tmp1;
3108 break;
3109 case TargetLowering::Legal: break;
3110 case TargetLowering::Expand: {
3111 // If this target supports fabs/fneg natively and select is cheap,
3112 // do this efficiently.
3113 if (!TLI.isSelectExpensive() &&
3114 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3115 TargetLowering::Legal &&
3116 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3117 TargetLowering::Legal) {
3118 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003119 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3121 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003122 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3124 // Get the absolute value of the result.
3125 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3126 // Select between the nabs and abs value based on the sign bit of
3127 // the input.
3128 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3129 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3130 AbsVal),
3131 AbsVal);
3132 Result = LegalizeOp(Result);
3133 break;
3134 }
3135
3136 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003137 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3139 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3140 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3141 Result = LegalizeOp(Result);
3142 break;
3143 }
3144 }
3145 break;
3146
3147 case ISD::ADDC:
3148 case ISD::SUBC:
3149 Tmp1 = LegalizeOp(Node->getOperand(0));
3150 Tmp2 = LegalizeOp(Node->getOperand(1));
3151 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3152 // Since this produces two values, make sure to remember that we legalized
3153 // both of them.
3154 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3155 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3156 return Result;
3157
3158 case ISD::ADDE:
3159 case ISD::SUBE:
3160 Tmp1 = LegalizeOp(Node->getOperand(0));
3161 Tmp2 = LegalizeOp(Node->getOperand(1));
3162 Tmp3 = LegalizeOp(Node->getOperand(2));
3163 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3164 // Since this produces two values, make sure to remember that we legalized
3165 // both of them.
3166 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3167 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3168 return Result;
3169
3170 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003171 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003172 // TODO: handle the case where the Lo and Hi operands are not of legal type
3173 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3174 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3175 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3176 case TargetLowering::Promote:
3177 case TargetLowering::Custom:
3178 assert(0 && "Cannot promote/custom this yet!");
3179 case TargetLowering::Legal:
3180 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3181 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3182 break;
3183 case TargetLowering::Expand:
3184 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3185 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3186 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003187 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188 TLI.getShiftAmountTy()));
3189 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3190 break;
3191 }
3192 break;
3193 }
3194
3195 case ISD::UREM:
3196 case ISD::SREM:
3197 case ISD::FREM:
3198 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3199 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3200
3201 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3202 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3203 case TargetLowering::Custom:
3204 isCustom = true;
3205 // FALLTHROUGH
3206 case TargetLowering::Legal:
3207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3208 if (isCustom) {
3209 Tmp1 = TLI.LowerOperation(Result, DAG);
3210 if (Tmp1.Val) Result = Tmp1;
3211 }
3212 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003213 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3215 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003216 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003217
3218 // See if remainder can be lowered using two-result operations.
3219 SDVTList VTs = DAG.getVTList(VT, VT);
3220 if (Node->getOpcode() == ISD::SREM &&
3221 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3222 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3223 break;
3224 }
3225 if (Node->getOpcode() == ISD::UREM &&
3226 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3227 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3228 break;
3229 }
3230
Duncan Sands92c43912008-06-06 12:08:01 +00003231 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003232 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233 TargetLowering::Legal) {
3234 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3236 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3237 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003238 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003239 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003240 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003241 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003242 "Cannot expand this binary operator!");
3243 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3244 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3245 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003246 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003248 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003249 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003250 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003251 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003252 Result = LegalizeOp(UnrollVectorOp(Op));
3253 } else {
3254 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003255 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3256 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003257 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003258 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003259 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 }
3261 break;
3262 }
Dan Gohman5a199552007-10-08 18:33:35 +00003263 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 break;
3265 case ISD::VAARG: {
3266 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3267 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3268
Duncan Sands92c43912008-06-06 12:08:01 +00003269 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3271 default: assert(0 && "This action is not supported yet!");
3272 case TargetLowering::Custom:
3273 isCustom = true;
3274 // FALLTHROUGH
3275 case TargetLowering::Legal:
3276 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3277 Result = Result.getValue(0);
3278 Tmp1 = Result.getValue(1);
3279
3280 if (isCustom) {
3281 Tmp2 = TLI.LowerOperation(Result, DAG);
3282 if (Tmp2.Val) {
3283 Result = LegalizeOp(Tmp2);
3284 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3285 }
3286 }
3287 break;
3288 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003289 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3290 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003291 // Increment the pointer, VAList, to the next vaarg
3292 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00003293 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 TLI.getPointerTy()));
3295 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003296 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 // Load the actual argument out of the pointer VAList
3298 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3299 Tmp1 = LegalizeOp(Result.getValue(1));
3300 Result = LegalizeOp(Result);
3301 break;
3302 }
3303 }
3304 // Since VAARG produces two values, make sure to remember that we
3305 // legalized both of them.
3306 AddLegalizedOperand(SDOperand(Node, 0), Result);
3307 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3308 return Op.ResNo ? Tmp1 : Result;
3309 }
3310
3311 case ISD::VACOPY:
3312 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3313 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3314 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3315
3316 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3317 default: assert(0 && "This action is not supported yet!");
3318 case TargetLowering::Custom:
3319 isCustom = true;
3320 // FALLTHROUGH
3321 case TargetLowering::Legal:
3322 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3323 Node->getOperand(3), Node->getOperand(4));
3324 if (isCustom) {
3325 Tmp1 = TLI.LowerOperation(Result, DAG);
3326 if (Tmp1.Val) Result = Tmp1;
3327 }
3328 break;
3329 case TargetLowering::Expand:
3330 // This defaults to loading a pointer from the input and storing it to the
3331 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003332 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3333 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003334 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3335 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 break;
3337 }
3338 break;
3339
3340 case ISD::VAEND:
3341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3342 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3343
3344 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3345 default: assert(0 && "This action is not supported yet!");
3346 case TargetLowering::Custom:
3347 isCustom = true;
3348 // FALLTHROUGH
3349 case TargetLowering::Legal:
3350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3351 if (isCustom) {
3352 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3353 if (Tmp1.Val) Result = Tmp1;
3354 }
3355 break;
3356 case TargetLowering::Expand:
3357 Result = Tmp1; // Default to a no-op, return the chain
3358 break;
3359 }
3360 break;
3361
3362 case ISD::VASTART:
3363 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3364 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3365
3366 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3367
3368 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3369 default: assert(0 && "This action is not supported yet!");
3370 case TargetLowering::Legal: break;
3371 case TargetLowering::Custom:
3372 Tmp1 = TLI.LowerOperation(Result, DAG);
3373 if (Tmp1.Val) Result = Tmp1;
3374 break;
3375 }
3376 break;
3377
3378 case ISD::ROTL:
3379 case ISD::ROTR:
3380 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3381 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3382 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3383 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3384 default:
3385 assert(0 && "ROTL/ROTR legalize operation not supported");
3386 break;
3387 case TargetLowering::Legal:
3388 break;
3389 case TargetLowering::Custom:
3390 Tmp1 = TLI.LowerOperation(Result, DAG);
3391 if (Tmp1.Val) Result = Tmp1;
3392 break;
3393 case TargetLowering::Promote:
3394 assert(0 && "Do not know how to promote ROTL/ROTR");
3395 break;
3396 case TargetLowering::Expand:
3397 assert(0 && "Do not know how to expand ROTL/ROTR");
3398 break;
3399 }
3400 break;
3401
3402 case ISD::BSWAP:
3403 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3404 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3405 case TargetLowering::Custom:
3406 assert(0 && "Cannot custom legalize this yet!");
3407 case TargetLowering::Legal:
3408 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3409 break;
3410 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003411 MVT OVT = Tmp1.getValueType();
3412 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3413 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414
3415 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3416 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3417 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3418 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3419 break;
3420 }
3421 case TargetLowering::Expand:
3422 Result = ExpandBSWAP(Tmp1);
3423 break;
3424 }
3425 break;
3426
3427 case ISD::CTPOP:
3428 case ISD::CTTZ:
3429 case ISD::CTLZ:
3430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3431 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003432 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433 case TargetLowering::Legal:
3434 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003435 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003436 TargetLowering::Custom) {
3437 Tmp1 = TLI.LowerOperation(Result, DAG);
3438 if (Tmp1.Val) {
3439 Result = Tmp1;
3440 }
Scott Michel48b63e62007-07-30 21:00:31 +00003441 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003442 break;
3443 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003444 MVT OVT = Tmp1.getValueType();
3445 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446
3447 // Zero extend the argument.
3448 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3449 // Perform the larger operation, then subtract if needed.
3450 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3451 switch (Node->getOpcode()) {
3452 case ISD::CTPOP:
3453 Result = Tmp1;
3454 break;
3455 case ISD::CTTZ:
3456 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003457 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003458 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 ISD::SETEQ);
3460 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003461 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 break;
3463 case ISD::CTLZ:
3464 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3465 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003466 DAG.getConstant(NVT.getSizeInBits() -
3467 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468 break;
3469 }
3470 break;
3471 }
3472 case TargetLowering::Expand:
3473 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3474 break;
3475 }
3476 break;
3477
3478 // Unary operators
3479 case ISD::FABS:
3480 case ISD::FNEG:
3481 case ISD::FSQRT:
3482 case ISD::FSIN:
3483 case ISD::FCOS:
3484 Tmp1 = LegalizeOp(Node->getOperand(0));
3485 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3486 case TargetLowering::Promote:
3487 case TargetLowering::Custom:
3488 isCustom = true;
3489 // FALLTHROUGH
3490 case TargetLowering::Legal:
3491 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3492 if (isCustom) {
3493 Tmp1 = TLI.LowerOperation(Result, DAG);
3494 if (Tmp1.Val) Result = Tmp1;
3495 }
3496 break;
3497 case TargetLowering::Expand:
3498 switch (Node->getOpcode()) {
3499 default: assert(0 && "Unreachable!");
3500 case ISD::FNEG:
3501 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3502 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3503 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3504 break;
3505 case ISD::FABS: {
3506 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003507 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003508 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003509 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003510 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003511 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3512 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3513 break;
3514 }
3515 case ISD::FSQRT:
3516 case ISD::FSIN:
3517 case ISD::FCOS: {
Duncan Sands92c43912008-06-06 12:08:01 +00003518 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003519
3520 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003521 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003522 Result = LegalizeOp(UnrollVectorOp(Op));
3523 break;
3524 }
3525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003526 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3527 switch(Node->getOpcode()) {
3528 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003529 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3530 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003531 break;
3532 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003533 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3534 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535 break;
3536 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003537 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3538 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539 break;
3540 default: assert(0 && "Unreachable!");
3541 }
3542 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003543 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 break;
3545 }
3546 }
3547 break;
3548 }
3549 break;
3550 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003551 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003552
3553 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003554 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003555 Result = LegalizeOp(UnrollVectorOp(Op));
3556 break;
3557 }
3558
3559 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003560 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3561 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003563 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003564 break;
3565 }
3566 case ISD::BIT_CONVERT:
3567 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003568 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3569 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003570 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003571 // The input has to be a vector type, we have to either scalarize it, pack
3572 // it, or convert it based on whether the input vector type is legal.
3573 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003574 int InIx = Node->getOperand(0).ResNo;
Duncan Sands92c43912008-06-06 12:08:01 +00003575 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3576 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577
3578 // Figure out if there is a simple type corresponding to this Vector
3579 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003580 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 if (TLI.isTypeLegal(TVT)) {
3582 // Turn this into a bit convert of the vector input.
3583 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3584 LegalizeOp(Node->getOperand(0)));
3585 break;
3586 } else if (NumElems == 1) {
3587 // Turn this into a bit convert of the scalar input.
3588 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3589 ScalarizeVectorOp(Node->getOperand(0)));
3590 break;
3591 } else {
3592 // FIXME: UNIMP! Store then reload
3593 assert(0 && "Cast from unsupported vector type not implemented yet!");
3594 }
3595 } else {
3596 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3597 Node->getOperand(0).getValueType())) {
3598 default: assert(0 && "Unknown operation action!");
3599 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003600 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3601 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003602 break;
3603 case TargetLowering::Legal:
3604 Tmp1 = LegalizeOp(Node->getOperand(0));
3605 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3606 break;
3607 }
3608 }
3609 break;
3610
3611 // Conversion operators. The source and destination have different types.
3612 case ISD::SINT_TO_FP:
3613 case ISD::UINT_TO_FP: {
3614 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3615 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3616 case Legal:
3617 switch (TLI.getOperationAction(Node->getOpcode(),
3618 Node->getOperand(0).getValueType())) {
3619 default: assert(0 && "Unknown operation action!");
3620 case TargetLowering::Custom:
3621 isCustom = true;
3622 // FALLTHROUGH
3623 case TargetLowering::Legal:
3624 Tmp1 = LegalizeOp(Node->getOperand(0));
3625 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3626 if (isCustom) {
3627 Tmp1 = TLI.LowerOperation(Result, DAG);
3628 if (Tmp1.Val) Result = Tmp1;
3629 }
3630 break;
3631 case TargetLowering::Expand:
3632 Result = ExpandLegalINT_TO_FP(isSigned,
3633 LegalizeOp(Node->getOperand(0)),
3634 Node->getValueType(0));
3635 break;
3636 case TargetLowering::Promote:
3637 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3638 Node->getValueType(0),
3639 isSigned);
3640 break;
3641 }
3642 break;
3643 case Expand:
3644 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3645 Node->getValueType(0), Node->getOperand(0));
3646 break;
3647 case Promote:
3648 Tmp1 = PromoteOp(Node->getOperand(0));
3649 if (isSigned) {
3650 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3651 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3652 } else {
3653 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3654 Node->getOperand(0).getValueType());
3655 }
3656 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3657 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3658 break;
3659 }
3660 break;
3661 }
3662 case ISD::TRUNCATE:
3663 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3664 case Legal:
3665 Tmp1 = LegalizeOp(Node->getOperand(0));
3666 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3667 break;
3668 case Expand:
3669 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3670
3671 // Since the result is legal, we should just be able to truncate the low
3672 // part of the source.
3673 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3674 break;
3675 case Promote:
3676 Result = PromoteOp(Node->getOperand(0));
3677 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3678 break;
3679 }
3680 break;
3681
3682 case ISD::FP_TO_SINT:
3683 case ISD::FP_TO_UINT:
3684 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3685 case Legal:
3686 Tmp1 = LegalizeOp(Node->getOperand(0));
3687
3688 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3689 default: assert(0 && "Unknown operation action!");
3690 case TargetLowering::Custom:
3691 isCustom = true;
3692 // FALLTHROUGH
3693 case TargetLowering::Legal:
3694 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3695 if (isCustom) {
3696 Tmp1 = TLI.LowerOperation(Result, DAG);
3697 if (Tmp1.Val) Result = Tmp1;
3698 }
3699 break;
3700 case TargetLowering::Promote:
3701 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3702 Node->getOpcode() == ISD::FP_TO_SINT);
3703 break;
3704 case TargetLowering::Expand:
3705 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3706 SDOperand True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00003707 MVT VT = Node->getOperand(0).getValueType();
3708 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003709 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00003710 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3711 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00003712 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003713 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003714 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003715 Node->getOperand(0), Tmp2, ISD::SETLT);
3716 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3717 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3718 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3719 Tmp2));
3720 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003721 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3723 break;
3724 } else {
3725 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3726 }
3727 break;
3728 }
3729 break;
3730 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003731 MVT VT = Op.getValueType();
3732 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003733 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003734 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003735 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3736 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3737 Node->getOperand(0), DAG.getValueType(MVT::f64));
3738 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3739 DAG.getIntPtrConstant(1));
3740 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3741 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003742 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3743 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3744 Tmp2 = DAG.getConstantFP(apf, OVT);
3745 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3746 // FIXME: generated code sucks.
3747 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3748 DAG.getNode(ISD::ADD, MVT::i32,
3749 DAG.getNode(ISD::FP_TO_SINT, VT,
3750 DAG.getNode(ISD::FSUB, OVT,
3751 Node->getOperand(0), Tmp2)),
3752 DAG.getConstant(0x80000000, MVT::i32)),
3753 DAG.getNode(ISD::FP_TO_SINT, VT,
3754 Node->getOperand(0)),
3755 DAG.getCondCode(ISD::SETGE));
3756 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003757 break;
3758 }
Dan Gohmanec51f642008-03-10 23:03:31 +00003759 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00003760 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3761 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3762 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763 SDOperand Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003764 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003765 break;
3766 }
3767 case Promote:
3768 Tmp1 = PromoteOp(Node->getOperand(0));
3769 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3770 Result = LegalizeOp(Result);
3771 break;
3772 }
3773 break;
3774
Chris Lattner56ecde32008-01-16 06:57:07 +00003775 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00003776 MVT DstVT = Op.getValueType();
3777 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00003778 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3779 // The only other way we can lower this is to turn it into a STORE,
3780 // LOAD pair, targetting a temporary location (a stack slot).
3781 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3782 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003783 }
3784 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3785 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3786 case Legal:
3787 Tmp1 = LegalizeOp(Node->getOperand(0));
3788 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3789 break;
3790 case Promote:
3791 Tmp1 = PromoteOp(Node->getOperand(0));
3792 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3793 break;
3794 }
3795 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003796 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003797 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00003798 MVT DstVT = Op.getValueType();
3799 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00003800 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3801 if (SrcVT == MVT::ppcf128) {
Dale Johannesena0d36082008-01-20 01:18:38 +00003802 SDOperand Lo;
3803 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003804 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003805 if (DstVT!=MVT::f64)
3806 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003807 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003808 }
Chris Lattner5872a362008-01-17 07:00:52 +00003809 // The only other way we can lower this is to turn it into a STORE,
3810 // LOAD pair, targetting a temporary location (a stack slot).
3811 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3812 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003814 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3815 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3816 case Legal:
3817 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003818 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003819 break;
3820 case Promote:
3821 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003822 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3823 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003824 break;
3825 }
3826 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003827 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003828 case ISD::ANY_EXTEND:
3829 case ISD::ZERO_EXTEND:
3830 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3832 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3833 case Legal:
3834 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00003835 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00003836 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3837 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00003838 Tmp1 = TLI.LowerOperation(Result, DAG);
3839 if (Tmp1.Val) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00003840 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 break;
3842 case Promote:
3843 switch (Node->getOpcode()) {
3844 case ISD::ANY_EXTEND:
3845 Tmp1 = PromoteOp(Node->getOperand(0));
3846 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3847 break;
3848 case ISD::ZERO_EXTEND:
3849 Result = PromoteOp(Node->getOperand(0));
3850 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3851 Result = DAG.getZeroExtendInReg(Result,
3852 Node->getOperand(0).getValueType());
3853 break;
3854 case ISD::SIGN_EXTEND:
3855 Result = PromoteOp(Node->getOperand(0));
3856 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3857 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3858 Result,
3859 DAG.getValueType(Node->getOperand(0).getValueType()));
3860 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 }
3862 }
3863 break;
3864 case ISD::FP_ROUND_INREG:
3865 case ISD::SIGN_EXTEND_INREG: {
3866 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003867 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868
3869 // If this operation is not supported, convert it to a shl/shr or load/store
3870 // pair.
3871 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3872 default: assert(0 && "This action not supported for this op yet!");
3873 case TargetLowering::Legal:
3874 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3875 break;
3876 case TargetLowering::Expand:
3877 // If this is an integer extend and shifts are supported, do that.
3878 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3879 // NOTE: we could fall back on load/store here too for targets without
3880 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00003881 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3882 ExtraVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3884 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3885 Node->getOperand(0), ShiftCst);
3886 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3887 Result, ShiftCst);
3888 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3889 // The only way we can lower this is to turn it into a TRUNCSTORE,
3890 // EXTLOAD pair, targetting a temporary location (a stack slot).
3891
3892 // NOTE: there is a choice here between constantly creating new stack
3893 // slots and always reusing the same one. We currently always create
3894 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003895 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3896 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 } else {
3898 assert(0 && "Unknown op");
3899 }
3900 break;
3901 }
3902 break;
3903 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003904 case ISD::TRAMPOLINE: {
3905 SDOperand Ops[6];
3906 for (unsigned i = 0; i != 6; ++i)
3907 Ops[i] = LegalizeOp(Node->getOperand(i));
3908 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3909 // The only option for this node is to custom lower it.
3910 Result = TLI.LowerOperation(Result, DAG);
3911 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003912
3913 // Since trampoline produces two values, make sure to remember that we
3914 // legalized both of them.
3915 Tmp1 = LegalizeOp(Result.getValue(1));
3916 Result = LegalizeOp(Result);
3917 AddLegalizedOperand(SDOperand(Node, 0), Result);
3918 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3919 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003920 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00003921 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00003922 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003923 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3924 default: assert(0 && "This action not supported for this op yet!");
3925 case TargetLowering::Custom:
3926 Result = TLI.LowerOperation(Op, DAG);
3927 if (Result.Val) break;
3928 // Fall Thru
3929 case TargetLowering::Legal:
3930 // If this operation is not supported, lower it to constant 1
3931 Result = DAG.getConstant(1, VT);
3932 break;
3933 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00003934 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003935 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003936 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00003937 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003938 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3939 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00003940 case TargetLowering::Legal:
3941 Tmp1 = LegalizeOp(Node->getOperand(0));
3942 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3943 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003944 case TargetLowering::Custom:
3945 Result = TLI.LowerOperation(Op, DAG);
3946 if (Result.Val) break;
3947 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00003948 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003949 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00003950 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003951 TargetLowering::ArgListTy Args;
3952 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00003953 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3954 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00003955 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3956 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003957 Result = CallResult.second;
3958 break;
3959 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003960 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003961 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 }
3963
3964 assert(Result.getValueType() == Op.getValueType() &&
3965 "Bad legalization!");
3966
3967 // Make sure that the generated code is itself legal.
3968 if (Result != Op)
3969 Result = LegalizeOp(Result);
3970
3971 // Note that LegalizeOp may be reentered even from single-use nodes, which
3972 // means that we always must cache transformed nodes.
3973 AddLegalizedOperand(Op, Result);
3974 return Result;
3975}
3976
3977/// PromoteOp - Given an operation that produces a value in an invalid type,
3978/// promote it to compute the value into a larger type. The produced value will
3979/// have the correct bits for the low portion of the register, but no guarantee
3980/// is made about the top bits: it may be zero, sign-extended, or garbage.
3981SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00003982 MVT VT = Op.getValueType();
3983 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 assert(getTypeAction(VT) == Promote &&
3985 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00003986 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 "Cannot promote to smaller type!");
3988
3989 SDOperand Tmp1, Tmp2, Tmp3;
3990 SDOperand Result;
3991 SDNode *Node = Op.Val;
3992
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003993 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 if (I != PromotedNodes.end()) return I->second;
3995
3996 switch (Node->getOpcode()) {
3997 case ISD::CopyFromReg:
3998 assert(0 && "CopyFromReg must be legal!");
3999 default:
4000#ifndef NDEBUG
4001 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
4002#endif
4003 assert(0 && "Do not know how to promote this operator!");
4004 abort();
4005 case ISD::UNDEF:
4006 Result = DAG.getNode(ISD::UNDEF, NVT);
4007 break;
4008 case ISD::Constant:
4009 if (VT != MVT::i1)
4010 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4011 else
4012 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
4013 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4014 break;
4015 case ISD::ConstantFP:
4016 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4017 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4018 break;
4019
4020 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00004021 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004022 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00004023 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004024 TLI.getSetCCResultType(Node->getOperand(0)),
4025 Node->getOperand(0), Node->getOperand(1),
4026 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027 break;
4028
4029 case ISD::TRUNCATE:
4030 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4031 case Legal:
4032 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004033 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004035 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4037 break;
4038 case Promote:
4039 // The truncation is not required, because we don't guarantee anything
4040 // about high bits anyway.
4041 Result = PromoteOp(Node->getOperand(0));
4042 break;
4043 case Expand:
4044 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4045 // Truncate the low part of the expanded value to the result type
4046 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4047 }
4048 break;
4049 case ISD::SIGN_EXTEND:
4050 case ISD::ZERO_EXTEND:
4051 case ISD::ANY_EXTEND:
4052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4053 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4054 case Legal:
4055 // Input is legal? Just do extend all the way to the larger type.
4056 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4057 break;
4058 case Promote:
4059 // Promote the reg if it's smaller.
4060 Result = PromoteOp(Node->getOperand(0));
4061 // The high bits are not guaranteed to be anything. Insert an extend.
4062 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4063 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4064 DAG.getValueType(Node->getOperand(0).getValueType()));
4065 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4066 Result = DAG.getZeroExtendInReg(Result,
4067 Node->getOperand(0).getValueType());
4068 break;
4069 }
4070 break;
4071 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004072 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4073 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 Result = PromoteOp(Result);
4075 break;
4076
4077 case ISD::FP_EXTEND:
4078 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4079 case ISD::FP_ROUND:
4080 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4081 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4082 case Promote: assert(0 && "Unreachable with 2 FP types!");
4083 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004084 if (Node->getConstantOperandVal(1) == 0) {
4085 // Input is legal? Do an FP_ROUND_INREG.
4086 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4087 DAG.getValueType(VT));
4088 } else {
4089 // Just remove the truncate, it isn't affecting the value.
4090 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4091 Node->getOperand(1));
4092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 break;
4094 }
4095 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096 case ISD::SINT_TO_FP:
4097 case ISD::UINT_TO_FP:
4098 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4099 case Legal:
4100 // No extra round required here.
4101 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4102 break;
4103
4104 case Promote:
4105 Result = PromoteOp(Node->getOperand(0));
4106 if (Node->getOpcode() == ISD::SINT_TO_FP)
4107 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4108 Result,
4109 DAG.getValueType(Node->getOperand(0).getValueType()));
4110 else
4111 Result = DAG.getZeroExtendInReg(Result,
4112 Node->getOperand(0).getValueType());
4113 // No extra round required here.
4114 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4115 break;
4116 case Expand:
4117 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4118 Node->getOperand(0));
4119 // Round if we cannot tolerate excess precision.
4120 if (NoExcessFPPrecision)
4121 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4122 DAG.getValueType(VT));
4123 break;
4124 }
4125 break;
4126
4127 case ISD::SIGN_EXTEND_INREG:
4128 Result = PromoteOp(Node->getOperand(0));
4129 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4130 Node->getOperand(1));
4131 break;
4132 case ISD::FP_TO_SINT:
4133 case ISD::FP_TO_UINT:
4134 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4135 case Legal:
4136 case Expand:
4137 Tmp1 = Node->getOperand(0);
4138 break;
4139 case Promote:
4140 // The input result is prerounded, so we don't have to do anything
4141 // special.
4142 Tmp1 = PromoteOp(Node->getOperand(0));
4143 break;
4144 }
4145 // If we're promoting a UINT to a larger size, check to see if the new node
4146 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4147 // we can use that instead. This allows us to generate better code for
4148 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4149 // legal, such as PowerPC.
4150 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4151 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4152 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4153 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4154 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4155 } else {
4156 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4157 }
4158 break;
4159
4160 case ISD::FABS:
4161 case ISD::FNEG:
4162 Tmp1 = PromoteOp(Node->getOperand(0));
4163 assert(Tmp1.getValueType() == NVT);
4164 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4165 // NOTE: we do not have to do any extra rounding here for
4166 // NoExcessFPPrecision, because we know the input will have the appropriate
4167 // precision, and these operations don't modify precision at all.
4168 break;
4169
4170 case ISD::FSQRT:
4171 case ISD::FSIN:
4172 case ISD::FCOS:
4173 Tmp1 = PromoteOp(Node->getOperand(0));
4174 assert(Tmp1.getValueType() == NVT);
4175 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4176 if (NoExcessFPPrecision)
4177 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4178 DAG.getValueType(VT));
4179 break;
4180
4181 case ISD::FPOWI: {
4182 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4183 // directly as well, which may be better.
4184 Tmp1 = PromoteOp(Node->getOperand(0));
4185 assert(Tmp1.getValueType() == NVT);
4186 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4187 if (NoExcessFPPrecision)
4188 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4189 DAG.getValueType(VT));
4190 break;
4191 }
4192
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004193 case ISD::ATOMIC_CMP_SWAP: {
4194 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004195 Tmp2 = PromoteOp(Node->getOperand(2));
4196 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004197 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4198 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004199 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004200 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004201 // Remember that we legalized the chain.
4202 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4203 break;
4204 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004205 case ISD::ATOMIC_LOAD_ADD:
4206 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang078a62d2008-05-05 19:05:59 +00004207 case ISD::ATOMIC_LOAD_AND:
4208 case ISD::ATOMIC_LOAD_OR:
4209 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharthaf02d592008-06-14 05:48:15 +00004210 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang078a62d2008-05-05 19:05:59 +00004211 case ISD::ATOMIC_LOAD_MIN:
4212 case ISD::ATOMIC_LOAD_MAX:
4213 case ISD::ATOMIC_LOAD_UMIN:
4214 case ISD::ATOMIC_LOAD_UMAX:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004215 case ISD::ATOMIC_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004216 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004217 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004218 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4219 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004220 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004221 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004222 // Remember that we legalized the chain.
4223 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4224 break;
4225 }
4226
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004227 case ISD::AND:
4228 case ISD::OR:
4229 case ISD::XOR:
4230 case ISD::ADD:
4231 case ISD::SUB:
4232 case ISD::MUL:
4233 // The input may have strange things in the top bits of the registers, but
4234 // these operations don't care. They may have weird bits going out, but
4235 // that too is okay if they are integer operations.
4236 Tmp1 = PromoteOp(Node->getOperand(0));
4237 Tmp2 = PromoteOp(Node->getOperand(1));
4238 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4239 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4240 break;
4241 case ISD::FADD:
4242 case ISD::FSUB:
4243 case ISD::FMUL:
4244 Tmp1 = PromoteOp(Node->getOperand(0));
4245 Tmp2 = PromoteOp(Node->getOperand(1));
4246 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4247 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4248
4249 // Floating point operations will give excess precision that we may not be
4250 // able to tolerate. If we DO allow excess precision, just leave it,
4251 // otherwise excise it.
4252 // FIXME: Why would we need to round FP ops more than integer ones?
4253 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4254 if (NoExcessFPPrecision)
4255 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4256 DAG.getValueType(VT));
4257 break;
4258
4259 case ISD::SDIV:
4260 case ISD::SREM:
4261 // These operators require that their input be sign extended.
4262 Tmp1 = PromoteOp(Node->getOperand(0));
4263 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004264 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4266 DAG.getValueType(VT));
4267 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4268 DAG.getValueType(VT));
4269 }
4270 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4271
4272 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004273 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4275 DAG.getValueType(VT));
4276 break;
4277 case ISD::FDIV:
4278 case ISD::FREM:
4279 case ISD::FCOPYSIGN:
4280 // These operators require that their input be fp extended.
4281 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004282 case Expand: assert(0 && "not implemented");
4283 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4284 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285 }
4286 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004287 case Expand: assert(0 && "not implemented");
4288 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4289 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 }
4291 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4292
4293 // Perform FP_ROUND: this is probably overly pessimistic.
4294 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4295 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4296 DAG.getValueType(VT));
4297 break;
4298
4299 case ISD::UDIV:
4300 case ISD::UREM:
4301 // These operators require that their input be zero extended.
4302 Tmp1 = PromoteOp(Node->getOperand(0));
4303 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004304 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4306 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4307 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4308 break;
4309
4310 case ISD::SHL:
4311 Tmp1 = PromoteOp(Node->getOperand(0));
4312 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4313 break;
4314 case ISD::SRA:
4315 // The input value must be properly sign extended.
4316 Tmp1 = PromoteOp(Node->getOperand(0));
4317 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4318 DAG.getValueType(VT));
4319 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4320 break;
4321 case ISD::SRL:
4322 // The input value must be properly zero extended.
4323 Tmp1 = PromoteOp(Node->getOperand(0));
4324 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4325 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4326 break;
4327
4328 case ISD::VAARG:
4329 Tmp1 = Node->getOperand(0); // Get the chain.
4330 Tmp2 = Node->getOperand(1); // Get the pointer.
4331 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4332 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004333 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004335 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4336 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337 // Increment the pointer, VAList, to the next vaarg
4338 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004339 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340 TLI.getPointerTy()));
4341 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004342 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 // Load the actual argument out of the pointer VAList
4344 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4345 }
4346 // Remember that we legalized the chain.
4347 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4348 break;
4349
4350 case ISD::LOAD: {
4351 LoadSDNode *LD = cast<LoadSDNode>(Node);
4352 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4353 ? ISD::EXTLOAD : LD->getExtensionType();
4354 Result = DAG.getExtLoad(ExtType, NVT,
4355 LD->getChain(), LD->getBasePtr(),
4356 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004357 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 LD->isVolatile(),
4359 LD->getAlignment());
4360 // Remember that we legalized the chain.
4361 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4362 break;
4363 }
Scott Michel67224b22008-06-02 22:18:03 +00004364 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4366 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004367
Duncan Sands92c43912008-06-06 12:08:01 +00004368 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004369 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004370 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4371 // Ensure that the resulting node is at least the same size as the operands'
4372 // value types, because we cannot assume that TLI.getSetCCValueType() is
4373 // constant.
4374 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375 break;
Scott Michel67224b22008-06-02 22:18:03 +00004376 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 case ISD::SELECT_CC:
4378 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4379 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4380 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4381 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4382 break;
4383 case ISD::BSWAP:
4384 Tmp1 = Node->getOperand(0);
4385 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4386 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4387 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004388 DAG.getConstant(NVT.getSizeInBits() -
4389 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390 TLI.getShiftAmountTy()));
4391 break;
4392 case ISD::CTPOP:
4393 case ISD::CTTZ:
4394 case ISD::CTLZ:
4395 // Zero extend the argument
4396 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4397 // Perform the larger operation, then subtract if needed.
4398 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4399 switch(Node->getOpcode()) {
4400 case ISD::CTPOP:
4401 Result = Tmp1;
4402 break;
4403 case ISD::CTTZ:
4404 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004405 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004406 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407 ISD::SETEQ);
4408 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004409 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 break;
4411 case ISD::CTLZ:
4412 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4413 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004414 DAG.getConstant(NVT.getSizeInBits() -
4415 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416 break;
4417 }
4418 break;
4419 case ISD::EXTRACT_SUBVECTOR:
4420 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4421 break;
4422 case ISD::EXTRACT_VECTOR_ELT:
4423 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4424 break;
4425 }
4426
4427 assert(Result.Val && "Didn't set a result!");
4428
4429 // Make sure the result is itself legal.
4430 Result = LegalizeOp(Result);
4431
4432 // Remember that we promoted this!
4433 AddPromotedOperand(Op, Result);
4434 return Result;
4435}
4436
4437/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4438/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4439/// based on the vector type. The return type of this matches the element type
4440/// of the vector, which may not be legal for the target.
4441SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4442 // We know that operand #0 is the Vec vector. If the index is a constant
4443 // or if the invec is a supported hardware type, we can use it. Otherwise,
4444 // lower to a store then an indexed load.
4445 SDOperand Vec = Op.getOperand(0);
4446 SDOperand Idx = Op.getOperand(1);
4447
Duncan Sands92c43912008-06-06 12:08:01 +00004448 MVT TVT = Vec.getValueType();
4449 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450
4451 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4452 default: assert(0 && "This action is not supported yet!");
4453 case TargetLowering::Custom: {
4454 Vec = LegalizeOp(Vec);
4455 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4456 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4457 if (Tmp3.Val)
4458 return Tmp3;
4459 break;
4460 }
4461 case TargetLowering::Legal:
4462 if (isTypeLegal(TVT)) {
4463 Vec = LegalizeOp(Vec);
4464 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004465 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 }
4467 break;
4468 case TargetLowering::Expand:
4469 break;
4470 }
4471
4472 if (NumElems == 1) {
4473 // This must be an access of the only element. Return it.
4474 Op = ScalarizeVectorOp(Vec);
4475 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004476 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4478 SDOperand Lo, Hi;
4479 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004480 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004481 Vec = Lo;
4482 } else {
4483 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004484 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 Idx.getValueType());
4486 }
4487
4488 // It's now an extract from the appropriate high or low part. Recurse.
4489 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4490 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4491 } else {
4492 // Store the value to a temporary stack slot, then LOAD the scalar
4493 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004494 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4496
4497 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004498 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4500 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004501
Duncan Sandsec142ee2008-06-08 20:54:56 +00004502 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004503 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004504 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004505 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4508
4509 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4510 }
4511 return Op;
4512}
4513
4514/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4515/// we assume the operation can be split if it is not already legal.
4516SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4517 // We know that operand #0 is the Vec vector. For now we assume the index
4518 // is a constant and that the extracted result is a supported hardware type.
4519 SDOperand Vec = Op.getOperand(0);
4520 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4521
Duncan Sands92c43912008-06-06 12:08:01 +00004522 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523
Duncan Sands92c43912008-06-06 12:08:01 +00004524 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004525 // This must be an access of the desired vector length. Return it.
4526 return Vec;
4527 }
4528
4529 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4530 SDOperand Lo, Hi;
4531 SplitVectorOp(Vec, Lo, Hi);
4532 if (CIdx->getValue() < NumElems/2) {
4533 Vec = Lo;
4534 } else {
4535 Vec = Hi;
4536 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4537 }
4538
4539 // It's now an extract from the appropriate high or low part. Recurse.
4540 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4541 return ExpandEXTRACT_SUBVECTOR(Op);
4542}
4543
4544/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4545/// with condition CC on the current target. This usually involves legalizing
4546/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4547/// there may be no choice but to create a new SetCC node to represent the
4548/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4549/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4550void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4551 SDOperand &RHS,
4552 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004553 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554
4555 switch (getTypeAction(LHS.getValueType())) {
4556 case Legal:
4557 Tmp1 = LegalizeOp(LHS); // LHS
4558 Tmp2 = LegalizeOp(RHS); // RHS
4559 break;
4560 case Promote:
4561 Tmp1 = PromoteOp(LHS); // LHS
4562 Tmp2 = PromoteOp(RHS); // RHS
4563
4564 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00004565 if (LHS.getValueType().isInteger()) {
4566 MVT VT = LHS.getValueType();
4567 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568
4569 // Otherwise, we have to insert explicit sign or zero extends. Note
4570 // that we could insert sign extends for ALL conditions, but zero extend
4571 // is cheaper on many machines (an AND instead of two shifts), so prefer
4572 // it.
4573 switch (cast<CondCodeSDNode>(CC)->get()) {
4574 default: assert(0 && "Unknown integer comparison!");
4575 case ISD::SETEQ:
4576 case ISD::SETNE:
4577 case ISD::SETUGE:
4578 case ISD::SETUGT:
4579 case ISD::SETULE:
4580 case ISD::SETULT:
4581 // ALL of these operations will work if we either sign or zero extend
4582 // the operands (including the unsigned comparisons!). Zero extend is
4583 // usually a simpler/cheaper operation, so prefer it.
4584 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4585 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4586 break;
4587 case ISD::SETGE:
4588 case ISD::SETGT:
4589 case ISD::SETLT:
4590 case ISD::SETLE:
4591 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4592 DAG.getValueType(VT));
4593 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4594 DAG.getValueType(VT));
4595 break;
4596 }
4597 }
4598 break;
4599 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004600 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 if (VT == MVT::f32 || VT == MVT::f64) {
4602 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00004603 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 switch (cast<CondCodeSDNode>(CC)->get()) {
4605 case ISD::SETEQ:
4606 case ISD::SETOEQ:
4607 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4608 break;
4609 case ISD::SETNE:
4610 case ISD::SETUNE:
4611 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4612 break;
4613 case ISD::SETGE:
4614 case ISD::SETOGE:
4615 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4616 break;
4617 case ISD::SETLT:
4618 case ISD::SETOLT:
4619 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4620 break;
4621 case ISD::SETLE:
4622 case ISD::SETOLE:
4623 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4624 break;
4625 case ISD::SETGT:
4626 case ISD::SETOGT:
4627 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4628 break;
4629 case ISD::SETUO:
4630 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4631 break;
4632 case ISD::SETO:
4633 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4634 break;
4635 default:
4636 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4637 switch (cast<CondCodeSDNode>(CC)->get()) {
4638 case ISD::SETONE:
4639 // SETONE = SETOLT | SETOGT
4640 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4641 // Fallthrough
4642 case ISD::SETUGT:
4643 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4644 break;
4645 case ISD::SETUGE:
4646 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4647 break;
4648 case ISD::SETULT:
4649 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4650 break;
4651 case ISD::SETULE:
4652 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4653 break;
4654 case ISD::SETUEQ:
4655 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4656 break;
4657 default: assert(0 && "Unsupported FP setcc!");
4658 }
4659 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00004660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661 SDOperand Dummy;
Duncan Sands698842f2008-07-02 17:40:58 +00004662 SDOperand Ops[2] = { LHS, RHS };
4663 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).Val,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 false /*sign irrelevant*/, Dummy);
4665 Tmp2 = DAG.getConstant(0, MVT::i32);
4666 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4667 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00004668 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004669 CC);
Duncan Sands698842f2008-07-02 17:40:58 +00004670 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).Val,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00004672 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4674 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4675 Tmp2 = SDOperand();
4676 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00004677 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678 RHS = Tmp2;
4679 return;
4680 }
4681
4682 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4683 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004684 ExpandOp(RHS, RHSLo, RHSHi);
4685 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4686
4687 if (VT==MVT::ppcf128) {
4688 // FIXME: This generated code sucks. We want to generate
4689 // FCMP crN, hi1, hi2
4690 // BNE crN, L:
4691 // FCMP crN, lo1, lo2
4692 // The following can be improved, but not that much.
Scott Michel502151f2008-03-10 15:42:14 +00004693 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4694 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004695 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00004696 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4697 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004698 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4699 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4700 Tmp2 = SDOperand();
4701 break;
4702 }
4703
4704 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004705 case ISD::SETEQ:
4706 case ISD::SETNE:
4707 if (RHSLo == RHSHi)
4708 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4709 if (RHSCST->isAllOnesValue()) {
4710 // Comparison to -1.
4711 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4712 Tmp2 = RHSLo;
4713 break;
4714 }
4715
4716 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4717 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4718 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4719 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4720 break;
4721 default:
4722 // If this is a comparison of the sign bit, just look at the top part.
4723 // X > -1, x < 0
4724 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4725 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004726 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004727 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4728 CST->isAllOnesValue())) { // X > -1
4729 Tmp1 = LHSHi;
4730 Tmp2 = RHSHi;
4731 break;
4732 }
4733
4734 // FIXME: This generated code sucks.
4735 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004736 switch (CCCode) {
4737 default: assert(0 && "Unknown integer setcc!");
4738 case ISD::SETLT:
4739 case ISD::SETULT: LowCC = ISD::SETULT; break;
4740 case ISD::SETGT:
4741 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4742 case ISD::SETLE:
4743 case ISD::SETULE: LowCC = ISD::SETULE; break;
4744 case ISD::SETGE:
4745 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4746 }
4747
4748 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4749 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4750 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4751
4752 // NOTE: on targets without efficient SELECT of bools, we can always use
4753 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4754 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00004755 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004756 LowCC, false, DagCombineInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 if (!Tmp1.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004758 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4759 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004760 CCCode, false, DagCombineInfo);
4761 if (!Tmp2.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004762 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004763 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764
4765 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4766 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohman9d24dc72008-03-13 22:13:53 +00004767 if ((Tmp1C && Tmp1C->isNullValue()) ||
4768 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4770 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00004771 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004772 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4773 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4774 // low part is known false, returns high part.
4775 // For LE / GE, if high part is known false, ignore the low part.
4776 // For LT / GT, if high part is known true, ignore the low part.
4777 Tmp1 = Tmp2;
4778 Tmp2 = SDOperand();
4779 } else {
Scott Michel502151f2008-03-10 15:42:14 +00004780 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781 ISD::SETEQ, false, DagCombineInfo);
4782 if (!Result.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004783 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004784 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004785 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4786 Result, Tmp1, Tmp2));
4787 Tmp1 = Result;
4788 Tmp2 = SDOperand();
4789 }
4790 }
4791 }
4792 }
4793 LHS = Tmp1;
4794 RHS = Tmp2;
4795}
4796
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004797/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4798/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4799/// a load from the stack slot to DestVT, extending it if needed.
4800/// The resultant code need not be legal.
4801SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
Duncan Sands92c43912008-06-06 12:08:01 +00004802 MVT SlotVT,
4803 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004804 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00004805 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
4806 SrcOp.getValueType().getTypeForMVT());
4807 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
4808
Dan Gohman20e37962008-02-11 18:58:42 +00004809 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004810 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00004811
Duncan Sands92c43912008-06-06 12:08:01 +00004812 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
4813 unsigned SlotSize = SlotVT.getSizeInBits();
4814 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00004815 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
4816 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004818 // Emit a store to the stack slot. Use a truncstore if the input value is
4819 // later than DestVT.
4820 SDOperand Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00004821
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004822 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004823 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004824 PseudoSourceValue::getFixedStack(SPFI), 0,
4825 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004826 else {
4827 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004828 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004829 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00004830 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004831 }
4832
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004833 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004834 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00004835 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004836
4837 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00004838 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
4839 false, DestAlign);
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 Gohman1fc34bc2008-07-11 22:44:52 +00004851 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00004852 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004853 PseudoSourceValue::getFixedStack(SPFI), 0);
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) {
Duncan Sands92c43912008-06-06 12:08:01 +00004902 MVT 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 =
Duncan Sands92c43912008-06-06 12:08:01 +00004914 Node->getOperand(0).getValueType().getTypeForMVT();
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>
Duncan Sands92c43912008-06-06 12:08:01 +00004926 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4927 SDOperand Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004928 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4929 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4930 &ZeroVec[0], ZeroVec.size());
4931
4932 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4933 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4934 // Get the splatted value into the low element of a vector register.
4935 SDOperand LowValVec =
4936 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4937
4938 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4939 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4940 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4941 SplatMask);
4942 }
4943 }
4944
4945 // If there are only two unique elements, we may be able to turn this into a
4946 // vector shuffle.
4947 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00004948 // Get the two values in deterministic order.
4949 SDOperand Val1 = Node->getOperand(1);
4950 SDOperand Val2;
4951 std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin();
4952 if (MI->first != Val1)
4953 Val2 = MI->first;
4954 else
4955 Val2 = (++MI)->first;
4956
4957 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4958 // vector shuffle has the undef vector on the RHS.
4959 if (Val1.getOpcode() == ISD::UNDEF)
4960 std::swap(Val1, Val2);
4961
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00004963 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4964 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004965 std::vector<SDOperand> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00004966
4967 // Set elements of the shuffle mask for Val1.
4968 std::vector<unsigned> &Val1Elts = Values[Val1];
4969 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4970 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4971
4972 // Set elements of the shuffle mask for Val2.
4973 std::vector<unsigned> &Val2Elts = Values[Val2];
4974 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4975 if (Val2.getOpcode() != ISD::UNDEF)
4976 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4977 else
4978 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4981 &MaskVec[0], MaskVec.size());
4982
Chris Lattnerd8cee732008-03-09 00:29:42 +00004983 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4985 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00004986 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
4987 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
4988 SDOperand Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989
4990 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00004991 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004992 }
4993 }
4994
4995 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4996 // aligned object on the stack, store each element into it, then load
4997 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00004998 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00005000 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001
5002 // Emit a store of each element to the stack slot.
5003 SmallVector<SDOperand, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00005004 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005 // Store (in the right endianness) the elements to memory.
5006 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5007 // Ignore undef elements.
5008 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5009
5010 unsigned Offset = TypeByteSize*i;
5011
5012 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5013 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5014
5015 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
5016 NULL, 0));
5017 }
5018
5019 SDOperand StoreChain;
5020 if (!Stores.empty()) // Not all undef elements?
5021 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5022 &Stores[0], Stores.size());
5023 else
5024 StoreChain = DAG.getEntryNode();
5025
5026 // Result is a load from the stack slot.
5027 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
5028}
5029
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5031 SDOperand Op, SDOperand Amt,
5032 SDOperand &Lo, SDOperand &Hi) {
5033 // Expand the subcomponents.
5034 SDOperand LHSL, LHSH;
5035 ExpandOp(Op, LHSL, LHSH);
5036
5037 SDOperand Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005038 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5040 Hi = Lo.getValue(1);
5041}
5042
5043
5044/// ExpandShift - Try to find a clever way to expand this shift operation out to
5045/// smaller elements. If we can't find a way that is more efficient than a
5046/// libcall on this target, return false. Otherwise, return true with the
5047/// low-parts expanded into Lo and Hi.
5048bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5049 SDOperand &Lo, SDOperand &Hi) {
5050 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5051 "This is not a shift!");
5052
Duncan Sands92c43912008-06-06 12:08:01 +00005053 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054 SDOperand ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005055 MVT ShTy = ShAmt.getValueType();
5056 unsigned ShBits = ShTy.getSizeInBits();
5057 unsigned VTBits = Op.getValueType().getSizeInBits();
5058 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059
Chris Lattner8c931452007-10-14 20:35:12 +00005060 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5062 unsigned Cst = CN->getValue();
5063 // Expand the incoming operand to be shifted, so that we have its parts
5064 SDOperand InL, InH;
5065 ExpandOp(Op, InL, InH);
5066 switch(Opc) {
5067 case ISD::SHL:
5068 if (Cst > VTBits) {
5069 Lo = DAG.getConstant(0, NVT);
5070 Hi = DAG.getConstant(0, NVT);
5071 } else if (Cst > NVTBits) {
5072 Lo = DAG.getConstant(0, NVT);
5073 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5074 } else if (Cst == NVTBits) {
5075 Lo = DAG.getConstant(0, NVT);
5076 Hi = InL;
5077 } else {
5078 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5079 Hi = DAG.getNode(ISD::OR, NVT,
5080 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5081 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5082 }
5083 return true;
5084 case ISD::SRL:
5085 if (Cst > VTBits) {
5086 Lo = DAG.getConstant(0, NVT);
5087 Hi = DAG.getConstant(0, NVT);
5088 } else if (Cst > NVTBits) {
5089 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5090 Hi = DAG.getConstant(0, NVT);
5091 } else if (Cst == NVTBits) {
5092 Lo = InH;
5093 Hi = DAG.getConstant(0, NVT);
5094 } else {
5095 Lo = DAG.getNode(ISD::OR, NVT,
5096 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5097 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5098 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5099 }
5100 return true;
5101 case ISD::SRA:
5102 if (Cst > VTBits) {
5103 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5104 DAG.getConstant(NVTBits-1, ShTy));
5105 } else if (Cst > NVTBits) {
5106 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5107 DAG.getConstant(Cst-NVTBits, ShTy));
5108 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5109 DAG.getConstant(NVTBits-1, ShTy));
5110 } else if (Cst == NVTBits) {
5111 Lo = InH;
5112 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5113 DAG.getConstant(NVTBits-1, ShTy));
5114 } else {
5115 Lo = DAG.getNode(ISD::OR, NVT,
5116 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5117 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5118 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5119 }
5120 return true;
5121 }
5122 }
5123
5124 // Okay, the shift amount isn't constant. However, if we can tell that it is
5125 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005126 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5127 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5129
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005130 // If we know that if any of the high bits of the shift amount are one, then
5131 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005132 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005133 // Mask out the high bit, which we know is set.
5134 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005135 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136
5137 // Expand the incoming operand to be shifted, so that we have its parts
5138 SDOperand InL, InH;
5139 ExpandOp(Op, InL, InH);
5140 switch(Opc) {
5141 case ISD::SHL:
5142 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5143 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5144 return true;
5145 case ISD::SRL:
5146 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5147 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5148 return true;
5149 case ISD::SRA:
5150 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5151 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5152 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5153 return true;
5154 }
5155 }
5156
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005157 // If we know that the high bits of the shift amount are all zero, then we can
5158 // do this as a couple of simple shifts.
5159 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160 // Compute 32-amt.
5161 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5162 DAG.getConstant(NVTBits, Amt.getValueType()),
5163 Amt);
5164
5165 // Expand the incoming operand to be shifted, so that we have its parts
5166 SDOperand InL, InH;
5167 ExpandOp(Op, InL, InH);
5168 switch(Opc) {
5169 case ISD::SHL:
5170 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5171 Hi = DAG.getNode(ISD::OR, NVT,
5172 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5173 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5174 return true;
5175 case ISD::SRL:
5176 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5177 Lo = DAG.getNode(ISD::OR, NVT,
5178 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5179 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5180 return true;
5181 case ISD::SRA:
5182 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5183 Lo = DAG.getNode(ISD::OR, NVT,
5184 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5185 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5186 return true;
5187 }
5188 }
5189
5190 return false;
5191}
5192
5193
5194// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5195// does not fit into a register, return the lo part and set the hi part to the
5196// by-reg argument. If it does fit into a single register, return the result
5197// and leave the Hi part unset.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005198SDOperand SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199 bool isSigned, SDOperand &Hi) {
5200 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5201 // The input chain to this libcall is the entry node of the function.
5202 // Legalizing the call will automatically add the previous call to the
5203 // dependence.
5204 SDOperand InChain = DAG.getEntryNode();
5205
5206 TargetLowering::ArgListTy Args;
5207 TargetLowering::ArgListEntry Entry;
5208 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005209 MVT ArgVT = Node->getOperand(i).getValueType();
5210 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005211 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5212 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005213 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214 Args.push_back(Entry);
5215 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005216 SDOperand Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
5217 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005218
5219 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005220 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005221 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005222 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5223 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224
5225 // Legalize the call sequence, starting with the chain. This will advance
5226 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5227 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5228 LegalizeOp(CallInfo.second);
5229 SDOperand Result;
5230 switch (getTypeAction(CallInfo.first.getValueType())) {
5231 default: assert(0 && "Unknown thing");
5232 case Legal:
5233 Result = CallInfo.first;
5234 break;
5235 case Expand:
5236 ExpandOp(CallInfo.first, Result, Hi);
5237 break;
5238 }
5239 return Result;
5240}
5241
5242
5243/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5244///
5245SDOperand SelectionDAGLegalize::
Duncan Sands92c43912008-06-06 12:08:01 +00005246ExpandIntToFP(bool isSigned, MVT DestTy, SDOperand Source) {
5247 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005248 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005249
Evan Chengf99a7752008-04-01 02:18:22 +00005250 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5251 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005252 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005253 // incoming integer is set. To handle this, we dynamically test to see if
5254 // it is set, and, if so, add a fudge factor.
Dan Gohman8b232ff2008-03-11 01:59:03 +00005255 SDOperand Hi;
5256 if (ExpandSource) {
5257 SDOperand Lo;
5258 ExpandOp(Source, Lo, Hi);
5259 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5260 } else {
5261 // The comparison for the sign bit will use the entire operand.
5262 Hi = Source;
5263 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005264
5265 // If this is unsigned, and not supported, first perform the conversion to
5266 // signed, then adjust the result if the sign bit is set.
Dan Gohman8b232ff2008-03-11 01:59:03 +00005267 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005268
Scott Michel502151f2008-03-10 15:42:14 +00005269 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005270 DAG.getConstant(0, Hi.getValueType()),
5271 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005272 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005273 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5274 SignSet, Four, Zero);
5275 uint64_t FF = 0x5f800000ULL;
5276 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005277 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278
5279 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5280 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5281 SDOperand FudgeInReg;
5282 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005283 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005284 PseudoSourceValue::getConstantPool(), 0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005285 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005287 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005288 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005289 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005290 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005291 else
5292 assert(0 && "Unexpected conversion");
5293
Duncan Sands92c43912008-06-06 12:08:01 +00005294 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005295 if (SCVT != DestTy) {
5296 // Destination type needs to be expanded as well. The FADD now we are
5297 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005298 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5299 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005300 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 SignedConv, SignedConv.getValue(1));
5302 }
5303 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5304 }
5305 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5306 }
5307
5308 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005309 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005310 default: assert(0 && "This action not implemented for this operation!");
5311 case TargetLowering::Legal:
5312 case TargetLowering::Expand:
5313 break; // This case is handled below.
5314 case TargetLowering::Custom: {
5315 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5316 Source), DAG);
5317 if (NV.Val)
5318 return LegalizeOp(NV);
5319 break; // The target decided this was legal after all
5320 }
5321 }
5322
5323 // Expand the source, then glue it back together for the call. We must expand
5324 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005325 if (ExpandSource) {
5326 SDOperand SrcLo, SrcHi;
5327 ExpandOp(Source, SrcLo, SrcHi);
5328 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5329 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005331 RTLIB::Libcall LC = isSigned ?
5332 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5333 RTLIB::getUINTTOFP(SourceVT, DestTy);
5334 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohmanec51f642008-03-10 23:03:31 +00005337 SDOperand HiPart;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005338 SDOperand Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart);
Evan Chenga8740032008-04-01 01:50:16 +00005339 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmanec51f642008-03-10 23:03:31 +00005340 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5341 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005342}
5343
5344/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5345/// INT_TO_FP operation of the specified operand when the target requests that
5346/// we expand it. At this point, we know that the result and operand types are
5347/// legal for the target.
5348SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5349 SDOperand Op0,
Duncan Sands92c43912008-06-06 12:08:01 +00005350 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005351 if (Op0.getValueType() == MVT::i32) {
5352 // simple 32-bit [signed|unsigned] integer to float/double expansion
5353
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005354 // Get the stack frame index of a 8 byte buffer.
5355 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357 // word offset constant for Hi/Lo address computation
5358 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5359 // set up Hi and Lo (into buffer) address based on endian
5360 SDOperand Hi = StackSlot;
5361 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5362 if (TLI.isLittleEndian())
5363 std::swap(Hi, Lo);
5364
5365 // if signed map to unsigned space
5366 SDOperand Op0Mapped;
5367 if (isSigned) {
5368 // constant used to invert sign bit (signed to unsigned mapping)
5369 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5370 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5371 } else {
5372 Op0Mapped = Op0;
5373 }
5374 // store the lo of the constructed double - based on integer input
5375 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5376 Op0Mapped, Lo, NULL, 0);
5377 // initial hi portion of constructed double
5378 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5379 // store the hi of the constructed double - biased exponent
5380 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5381 // load the constructed double
5382 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5383 // FP constant to bias correct the final result
5384 SDOperand Bias = DAG.getConstantFP(isSigned ?
5385 BitsToDouble(0x4330000080000000ULL)
5386 : BitsToDouble(0x4330000000000000ULL),
5387 MVT::f64);
5388 // subtract the bias
5389 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5390 // final result
5391 SDOperand Result;
5392 // handle final rounding
5393 if (DestVT == MVT::f64) {
5394 // do nothing
5395 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00005396 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005397 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5398 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00005399 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005400 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005401 }
5402 return Result;
5403 }
5404 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5405 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5406
Scott Michel502151f2008-03-10 15:42:14 +00005407 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005408 DAG.getConstant(0, Op0.getValueType()),
5409 ISD::SETLT);
Chris Lattner5872a362008-01-17 07:00:52 +00005410 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005411 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5412 SignSet, Four, Zero);
5413
5414 // If the sign bit of the integer is set, the large number will be treated
5415 // as a negative number. To counteract this, the dynamic code adds an
5416 // offset depending on the data type.
5417 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00005418 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005419 default: assert(0 && "Unsupported integer type!");
5420 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5421 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5422 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5423 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5424 }
5425 if (TLI.isLittleEndian()) FF <<= 32;
5426 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5427
5428 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5429 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5430 SDOperand FudgeInReg;
5431 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005432 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005433 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005435 FudgeInReg =
5436 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5437 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005438 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005439 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005440 }
5441
5442 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5443}
5444
5445/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5446/// *INT_TO_FP operation of the specified operand when the target requests that
5447/// we promote it. At this point, we know that the result and operand types are
5448/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5449/// operation that takes a larger input.
5450SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
Duncan Sands92c43912008-06-06 12:08:01 +00005451 MVT DestVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005452 bool isSigned) {
5453 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005454 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455
5456 unsigned OpToUse = 0;
5457
5458 // Scan for the appropriate larger type to use.
5459 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005460 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5461 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005462
5463 // If the target supports SINT_TO_FP of this type, use it.
5464 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5465 default: break;
5466 case TargetLowering::Legal:
5467 if (!TLI.isTypeLegal(NewInTy))
5468 break; // Can't use this datatype.
5469 // FALL THROUGH.
5470 case TargetLowering::Custom:
5471 OpToUse = ISD::SINT_TO_FP;
5472 break;
5473 }
5474 if (OpToUse) break;
5475 if (isSigned) continue;
5476
5477 // If the target supports UINT_TO_FP of this type, use it.
5478 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5479 default: break;
5480 case TargetLowering::Legal:
5481 if (!TLI.isTypeLegal(NewInTy))
5482 break; // Can't use this datatype.
5483 // FALL THROUGH.
5484 case TargetLowering::Custom:
5485 OpToUse = ISD::UINT_TO_FP;
5486 break;
5487 }
5488 if (OpToUse) break;
5489
5490 // Otherwise, try a larger type.
5491 }
5492
5493 // Okay, we found the operation and type to use. Zero extend our input to the
5494 // desired type then run the operation on it.
5495 return DAG.getNode(OpToUse, DestVT,
5496 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5497 NewInTy, LegalOp));
5498}
5499
5500/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5501/// FP_TO_*INT operation of the specified operand when the target requests that
5502/// we promote it. At this point, we know that the result and operand types are
5503/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5504/// operation that returns a larger result.
5505SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
Duncan Sands92c43912008-06-06 12:08:01 +00005506 MVT DestVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 bool isSigned) {
5508 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005509 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005510
5511 unsigned OpToUse = 0;
5512
5513 // Scan for the appropriate larger type to use.
5514 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005515 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5516 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005517
5518 // If the target supports FP_TO_SINT returning this type, use it.
5519 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5520 default: break;
5521 case TargetLowering::Legal:
5522 if (!TLI.isTypeLegal(NewOutTy))
5523 break; // Can't use this datatype.
5524 // FALL THROUGH.
5525 case TargetLowering::Custom:
5526 OpToUse = ISD::FP_TO_SINT;
5527 break;
5528 }
5529 if (OpToUse) break;
5530
5531 // If the target supports FP_TO_UINT of this type, use it.
5532 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5533 default: break;
5534 case TargetLowering::Legal:
5535 if (!TLI.isTypeLegal(NewOutTy))
5536 break; // Can't use this datatype.
5537 // FALL THROUGH.
5538 case TargetLowering::Custom:
5539 OpToUse = ISD::FP_TO_UINT;
5540 break;
5541 }
5542 if (OpToUse) break;
5543
5544 // Otherwise, try a larger type.
5545 }
5546
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005547
5548 // Okay, we found the operation and type to use.
5549 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00005550
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005551 // If the operation produces an invalid type, it must be custom lowered. Use
5552 // the target lowering hooks to expand it. Just keep the low part of the
5553 // expanded operation, we know that we're truncating anyway.
5554 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sandsac496a12008-07-04 11:47:58 +00005555 Operation = SDOperand(TLI.ReplaceNodeResults(Operation.Val, DAG), 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005556 assert(Operation.Val && "Didn't return anything");
5557 }
Duncan Sandsac496a12008-07-04 11:47:58 +00005558
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005559 // Truncate the result of the extended FP_TO_*INT operation to the desired
5560 // size.
5561 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562}
5563
5564/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5565///
5566SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00005567 MVT VT = Op.getValueType();
5568 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00005570 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5572 case MVT::i16:
5573 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5574 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5575 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5576 case MVT::i32:
5577 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5578 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5579 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5580 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5581 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5582 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5583 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5584 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5585 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5586 case MVT::i64:
5587 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5588 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5589 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5590 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5591 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5592 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5593 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5594 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5595 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5596 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5597 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5598 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5599 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5600 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5601 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5602 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5603 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5604 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5605 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5606 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5607 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5608 }
5609}
5610
5611/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5612///
5613SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5614 switch (Opc) {
5615 default: assert(0 && "Cannot expand this yet!");
5616 case ISD::CTPOP: {
5617 static const uint64_t mask[6] = {
5618 0x5555555555555555ULL, 0x3333333333333333ULL,
5619 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5620 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5621 };
Duncan Sands92c43912008-06-06 12:08:01 +00005622 MVT VT = Op.getValueType();
5623 MVT ShVT = TLI.getShiftAmountTy();
5624 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5626 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5627 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5628 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5629 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5630 DAG.getNode(ISD::AND, VT,
5631 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5632 }
5633 return Op;
5634 }
5635 case ISD::CTLZ: {
5636 // for now, we do this:
5637 // x = x | (x >> 1);
5638 // x = x | (x >> 2);
5639 // ...
5640 // x = x | (x >>16);
5641 // x = x | (x >>32); // for 64-bit input
5642 // return popcount(~x);
5643 //
5644 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00005645 MVT VT = Op.getValueType();
5646 MVT ShVT = TLI.getShiftAmountTy();
5647 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005648 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5649 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5650 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5651 }
5652 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5653 return DAG.getNode(ISD::CTPOP, VT, Op);
5654 }
5655 case ISD::CTTZ: {
5656 // for now, we use: { return popcount(~x & (x - 1)); }
5657 // unless the target has ctlz but not ctpop, in which case we use:
5658 // { return 32 - nlz(~x & (x-1)); }
5659 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00005660 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005661 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5662 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5663 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5664 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5665 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5666 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5667 TLI.isOperationLegal(ISD::CTLZ, VT))
5668 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00005669 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005670 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5671 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5672 }
5673 }
5674}
5675
5676/// ExpandOp - Expand the specified SDOperand into its two component pieces
5677/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5678/// LegalizeNodes map is filled in for any results that are not expanded, the
5679/// ExpandedNodes map is filled in for any results that are expanded, and the
5680/// Lo/Hi values are returned.
5681void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00005682 MVT VT = Op.getValueType();
5683 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005684 SDNode *Node = Op.Val;
5685 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00005686 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00005687 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005688
5689 // See if we already expanded it.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00005690 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 = ExpandedNodes.find(Op);
5692 if (I != ExpandedNodes.end()) {
5693 Lo = I->second.first;
5694 Hi = I->second.second;
5695 return;
5696 }
5697
5698 switch (Node->getOpcode()) {
5699 case ISD::CopyFromReg:
5700 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005701 case ISD::FP_ROUND_INREG:
5702 if (VT == MVT::ppcf128 &&
5703 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5704 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005705 SDOperand SrcLo, SrcHi, Src;
5706 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5707 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5708 SDOperand Result = TLI.LowerOperation(
5709 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005710 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5711 Lo = Result.Val->getOperand(0);
5712 Hi = Result.Val->getOperand(1);
5713 break;
5714 }
5715 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005716 default:
5717#ifndef NDEBUG
5718 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5719#endif
5720 assert(0 && "Do not know how to expand this operator!");
5721 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00005722 case ISD::EXTRACT_ELEMENT:
5723 ExpandOp(Node->getOperand(0), Lo, Hi);
5724 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5725 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00005726 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005727 case ISD::EXTRACT_VECTOR_ELT:
5728 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5729 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5730 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5731 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733 Lo = DAG.getNode(ISD::UNDEF, NVT);
5734 Hi = DAG.getNode(ISD::UNDEF, NVT);
5735 break;
5736 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00005737 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00005738 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5739 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5740 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005741 break;
5742 }
5743 case ISD::ConstantFP: {
5744 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005745 if (CFP->getValueType(0) == MVT::ppcf128) {
5746 APInt api = CFP->getValueAPF().convertToAPInt();
5747 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5748 MVT::f64);
5749 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5750 MVT::f64);
5751 break;
5752 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005753 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5754 if (getTypeAction(Lo.getValueType()) == Expand)
5755 ExpandOp(Lo, Lo, Hi);
5756 break;
5757 }
5758 case ISD::BUILD_PAIR:
5759 // Return the operands.
5760 Lo = Node->getOperand(0);
5761 Hi = Node->getOperand(1);
5762 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005763
5764 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005765 if (Node->getNumValues() == 1) {
5766 ExpandOp(Op.getOperand(0), Lo, Hi);
5767 break;
5768 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005769 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5770 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5771 Op.getValue(1).getValueType() == MVT::Other &&
5772 "unhandled MERGE_VALUES");
5773 ExpandOp(Op.getOperand(0), Lo, Hi);
5774 // Remember that we legalized the chain.
5775 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5776 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777
5778 case ISD::SIGN_EXTEND_INREG:
5779 ExpandOp(Node->getOperand(0), Lo, Hi);
5780 // sext_inreg the low part if needed.
5781 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5782
5783 // The high part gets the sign extension from the lo-part. This handles
5784 // things like sextinreg V:i64 from i8.
5785 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00005786 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 TLI.getShiftAmountTy()));
5788 break;
5789
5790 case ISD::BSWAP: {
5791 ExpandOp(Node->getOperand(0), Lo, Hi);
5792 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5793 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5794 Lo = TempLo;
5795 break;
5796 }
5797
5798 case ISD::CTPOP:
5799 ExpandOp(Node->getOperand(0), Lo, Hi);
5800 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5801 DAG.getNode(ISD::CTPOP, NVT, Lo),
5802 DAG.getNode(ISD::CTPOP, NVT, Hi));
5803 Hi = DAG.getConstant(0, NVT);
5804 break;
5805
5806 case ISD::CTLZ: {
5807 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5808 ExpandOp(Node->getOperand(0), Lo, Hi);
Duncan Sands92c43912008-06-06 12:08:01 +00005809 SDOperand BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005810 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Scott Michel502151f2008-03-10 15:42:14 +00005811 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005812 ISD::SETNE);
5813 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5814 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5815
5816 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5817 Hi = DAG.getConstant(0, NVT);
5818 break;
5819 }
5820
5821 case ISD::CTTZ: {
5822 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5823 ExpandOp(Node->getOperand(0), Lo, Hi);
Duncan Sands92c43912008-06-06 12:08:01 +00005824 SDOperand BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005825 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Scott Michel502151f2008-03-10 15:42:14 +00005826 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005827 ISD::SETNE);
5828 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5829 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5830
5831 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5832 Hi = DAG.getConstant(0, NVT);
5833 break;
5834 }
5835
5836 case ISD::VAARG: {
5837 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5838 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5839 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5840 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5841
5842 // Remember that we legalized the chain.
5843 Hi = LegalizeOp(Hi);
5844 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005845 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005846 std::swap(Lo, Hi);
5847 break;
5848 }
5849
5850 case ISD::LOAD: {
5851 LoadSDNode *LD = cast<LoadSDNode>(Node);
5852 SDOperand Ch = LD->getChain(); // Legalize the chain.
5853 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5854 ISD::LoadExtType ExtType = LD->getExtensionType();
5855 int SVOffset = LD->getSrcValueOffset();
5856 unsigned Alignment = LD->getAlignment();
5857 bool isVolatile = LD->isVolatile();
5858
5859 if (ExtType == ISD::NON_EXTLOAD) {
5860 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5861 isVolatile, Alignment);
5862 if (VT == MVT::f32 || VT == MVT::f64) {
5863 // f32->i32 or f64->i64 one to one expansion.
5864 // Remember that we legalized the chain.
5865 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5866 // Recursively expand the new load.
5867 if (getTypeAction(NVT) == Expand)
5868 ExpandOp(Lo, Lo, Hi);
5869 break;
5870 }
5871
5872 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00005873 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005874 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005875 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005876 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005877 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5879 isVolatile, Alignment);
5880
5881 // Build a factor node to remember that this load is independent of the
5882 // other one.
5883 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5884 Hi.getValue(1));
5885
5886 // Remember that we legalized the chain.
5887 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005888 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005889 std::swap(Lo, Hi);
5890 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00005891 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005892
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005893 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5894 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005895 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5896 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5897 SVOffset, isVolatile, Alignment);
5898 // Remember that we legalized the chain.
5899 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5900 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5901 break;
5902 }
5903
5904 if (EVT == NVT)
5905 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5906 SVOffset, isVolatile, Alignment);
5907 else
5908 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5909 SVOffset, EVT, isVolatile,
5910 Alignment);
5911
5912 // Remember that we legalized the chain.
5913 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5914
5915 if (ExtType == ISD::SEXTLOAD) {
5916 // The high part is obtained by SRA'ing all but one of the bits of the
5917 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00005918 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005919 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5920 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5921 } else if (ExtType == ISD::ZEXTLOAD) {
5922 // The high part is just a zero.
5923 Hi = DAG.getConstant(0, NVT);
5924 } else /* if (ExtType == ISD::EXTLOAD) */ {
5925 // The high part is undefined.
5926 Hi = DAG.getNode(ISD::UNDEF, NVT);
5927 }
5928 }
5929 break;
5930 }
5931 case ISD::AND:
5932 case ISD::OR:
5933 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5934 SDOperand LL, LH, RL, RH;
5935 ExpandOp(Node->getOperand(0), LL, LH);
5936 ExpandOp(Node->getOperand(1), RL, RH);
5937 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5938 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5939 break;
5940 }
5941 case ISD::SELECT: {
5942 SDOperand LL, LH, RL, RH;
5943 ExpandOp(Node->getOperand(1), LL, LH);
5944 ExpandOp(Node->getOperand(2), RL, RH);
5945 if (getTypeAction(NVT) == Expand)
5946 NVT = TLI.getTypeToExpandTo(NVT);
5947 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5948 if (VT != MVT::f32)
5949 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5950 break;
5951 }
5952 case ISD::SELECT_CC: {
5953 SDOperand TL, TH, FL, FH;
5954 ExpandOp(Node->getOperand(2), TL, TH);
5955 ExpandOp(Node->getOperand(3), FL, FH);
5956 if (getTypeAction(NVT) == Expand)
5957 NVT = TLI.getTypeToExpandTo(NVT);
5958 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5959 Node->getOperand(1), TL, FL, Node->getOperand(4));
5960 if (VT != MVT::f32)
5961 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5962 Node->getOperand(1), TH, FH, Node->getOperand(4));
5963 break;
5964 }
5965 case ISD::ANY_EXTEND:
5966 // The low part is any extension of the input (which degenerates to a copy).
5967 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5968 // The high part is undefined.
5969 Hi = DAG.getNode(ISD::UNDEF, NVT);
5970 break;
5971 case ISD::SIGN_EXTEND: {
5972 // The low part is just a sign extension of the input (which degenerates to
5973 // a copy).
5974 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
5975
5976 // The high part is obtained by SRA'ing all but one of the bits of the lo
5977 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00005978 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5980 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5981 break;
5982 }
5983 case ISD::ZERO_EXTEND:
5984 // The low part is just a zero extension of the input (which degenerates to
5985 // a copy).
5986 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
5987
5988 // The high part is just a zero.
5989 Hi = DAG.getConstant(0, NVT);
5990 break;
5991
5992 case ISD::TRUNCATE: {
5993 // The input value must be larger than this value. Expand *it*.
5994 SDOperand NewLo;
5995 ExpandOp(Node->getOperand(0), NewLo, Hi);
5996
5997 // The low part is now either the right size, or it is closer. If not the
5998 // right size, make an illegal truncate so we recursively expand it.
5999 if (NewLo.getValueType() != Node->getValueType(0))
6000 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6001 ExpandOp(NewLo, Lo, Hi);
6002 break;
6003 }
6004
6005 case ISD::BIT_CONVERT: {
6006 SDOperand Tmp;
6007 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6008 // If the target wants to, allow it to lower this itself.
6009 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6010 case Expand: assert(0 && "cannot expand FP!");
6011 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6012 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6013 }
6014 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6015 }
6016
6017 // f32 / f64 must be expanded to i32 / i64.
6018 if (VT == MVT::f32 || VT == MVT::f64) {
6019 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6020 if (getTypeAction(NVT) == Expand)
6021 ExpandOp(Lo, Lo, Hi);
6022 break;
6023 }
6024
6025 // If source operand will be expanded to the same type as VT, i.e.
6026 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006027 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006028 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6029 ExpandOp(Node->getOperand(0), Lo, Hi);
6030 break;
6031 }
6032
6033 // Turn this into a load/store pair by default.
6034 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006035 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006036
6037 ExpandOp(Tmp, Lo, Hi);
6038 break;
6039 }
6040
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006041 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006042 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6043 TargetLowering::Custom &&
6044 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006045 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6046 assert(Tmp.Val && "Node must be custom expanded!");
6047 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006048 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006049 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006051 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052
Mon P Wang6bde9ec2008-06-25 08:15:39 +00006053 case ISD::ATOMIC_CMP_SWAP: {
Andrew Lenharth81580822008-03-05 01:15:49 +00006054 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6055 assert(Tmp.Val && "Node must be custom expanded!");
6056 ExpandOp(Tmp.getValue(0), Lo, Hi);
6057 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6058 LegalizeOp(Tmp.getValue(1)));
6059 break;
6060 }
6061
6062
6063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064 // These operators cannot be expanded directly, emit them as calls to
6065 // library functions.
6066 case ISD::FP_TO_SINT: {
6067 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
6068 SDOperand Op;
6069 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6070 case Expand: assert(0 && "cannot expand FP!");
6071 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6072 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6073 }
6074
6075 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6076
6077 // Now that the custom expander is done, expand the result, which is still
6078 // VT.
6079 if (Op.Val) {
6080 ExpandOp(Op, Lo, Hi);
6081 break;
6082 }
6083 }
6084
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006085 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6086 VT);
6087 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6088 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006089 break;
6090 }
6091
6092 case ISD::FP_TO_UINT: {
6093 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
6094 SDOperand Op;
6095 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6096 case Expand: assert(0 && "cannot expand FP!");
6097 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6098 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6099 }
6100
6101 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6102
6103 // Now that the custom expander is done, expand the result.
6104 if (Op.Val) {
6105 ExpandOp(Op, Lo, Hi);
6106 break;
6107 }
6108 }
6109
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006110 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6111 VT);
6112 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6113 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006114 break;
6115 }
6116
6117 case ISD::SHL: {
6118 // If the target wants custom lowering, do so.
6119 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6120 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
6121 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
6122 Op = TLI.LowerOperation(Op, DAG);
6123 if (Op.Val) {
6124 // Now that the custom expander is done, expand the result, which is
6125 // still VT.
6126 ExpandOp(Op, Lo, Hi);
6127 break;
6128 }
6129 }
6130
6131 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6132 // this X << 1 as X+X.
6133 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006134 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006135 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6136 SDOperand LoOps[2], HiOps[3];
6137 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6138 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6139 LoOps[1] = LoOps[0];
6140 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6141
6142 HiOps[1] = HiOps[0];
6143 HiOps[2] = Lo.getValue(1);
6144 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6145 break;
6146 }
6147 }
6148
6149 // If we can emit an efficient shift operation, do so now.
6150 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6151 break;
6152
6153 // If this target supports SHL_PARTS, use it.
6154 TargetLowering::LegalizeAction Action =
6155 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6156 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6157 Action == TargetLowering::Custom) {
6158 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6159 break;
6160 }
6161
6162 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006163 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006164 break;
6165 }
6166
6167 case ISD::SRA: {
6168 // If the target wants custom lowering, do so.
6169 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6170 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
6171 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
6172 Op = TLI.LowerOperation(Op, DAG);
6173 if (Op.Val) {
6174 // Now that the custom expander is done, expand the result, which is
6175 // still VT.
6176 ExpandOp(Op, Lo, Hi);
6177 break;
6178 }
6179 }
6180
6181 // If we can emit an efficient shift operation, do so now.
6182 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6183 break;
6184
6185 // If this target supports SRA_PARTS, use it.
6186 TargetLowering::LegalizeAction Action =
6187 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6188 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6189 Action == TargetLowering::Custom) {
6190 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6191 break;
6192 }
6193
6194 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006195 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006196 break;
6197 }
6198
6199 case ISD::SRL: {
6200 // If the target wants custom lowering, do so.
6201 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
6202 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
6203 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
6204 Op = TLI.LowerOperation(Op, DAG);
6205 if (Op.Val) {
6206 // Now that the custom expander is done, expand the result, which is
6207 // still VT.
6208 ExpandOp(Op, Lo, Hi);
6209 break;
6210 }
6211 }
6212
6213 // If we can emit an efficient shift operation, do so now.
6214 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6215 break;
6216
6217 // If this target supports SRL_PARTS, use it.
6218 TargetLowering::LegalizeAction Action =
6219 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6220 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6221 Action == TargetLowering::Custom) {
6222 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6223 break;
6224 }
6225
6226 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006227 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006228 break;
6229 }
6230
6231 case ISD::ADD:
6232 case ISD::SUB: {
6233 // If the target wants to custom expand this, let them.
6234 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6235 TargetLowering::Custom) {
Duncan Sands4c3885b2008-06-22 09:42:16 +00006236 SDOperand Result = TLI.LowerOperation(Op, DAG);
6237 if (Result.Val) {
6238 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006239 break;
6240 }
6241 }
6242
6243 // Expand the subcomponents.
6244 SDOperand LHSL, LHSH, RHSL, RHSH;
6245 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6246 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6247 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6248 SDOperand LoOps[2], HiOps[3];
6249 LoOps[0] = LHSL;
6250 LoOps[1] = RHSL;
6251 HiOps[0] = LHSH;
6252 HiOps[1] = RHSH;
6253 if (Node->getOpcode() == ISD::ADD) {
6254 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6255 HiOps[2] = Lo.getValue(1);
6256 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6257 } else {
6258 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6259 HiOps[2] = Lo.getValue(1);
6260 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6261 }
6262 break;
6263 }
6264
6265 case ISD::ADDC:
6266 case ISD::SUBC: {
6267 // Expand the subcomponents.
6268 SDOperand LHSL, LHSH, RHSL, RHSH;
6269 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6270 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6271 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6272 SDOperand LoOps[2] = { LHSL, RHSL };
6273 SDOperand HiOps[3] = { LHSH, RHSH };
6274
6275 if (Node->getOpcode() == ISD::ADDC) {
6276 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6277 HiOps[2] = Lo.getValue(1);
6278 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6279 } else {
6280 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6281 HiOps[2] = Lo.getValue(1);
6282 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6283 }
6284 // Remember that we legalized the flag.
6285 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6286 break;
6287 }
6288 case ISD::ADDE:
6289 case ISD::SUBE: {
6290 // Expand the subcomponents.
6291 SDOperand LHSL, LHSH, RHSL, RHSH;
6292 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6293 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6294 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6295 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6296 SDOperand HiOps[3] = { LHSH, RHSH };
6297
6298 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6299 HiOps[2] = Lo.getValue(1);
6300 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6301
6302 // Remember that we legalized the flag.
6303 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6304 break;
6305 }
6306 case ISD::MUL: {
6307 // If the target wants to custom expand this, let them.
6308 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6309 SDOperand New = TLI.LowerOperation(Op, DAG);
6310 if (New.Val) {
6311 ExpandOp(New, Lo, Hi);
6312 break;
6313 }
6314 }
6315
6316 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6317 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006318 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6319 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6320 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006321 SDOperand LL, LH, RL, RH;
6322 ExpandOp(Node->getOperand(0), LL, LH);
6323 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006324 unsigned OuterBitSize = Op.getValueSizeInBits();
6325 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006326 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6327 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006328 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6329 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6330 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006331 // The inputs are both zero-extended.
6332 if (HasUMUL_LOHI) {
6333 // We can emit a umul_lohi.
6334 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6335 Hi = SDOperand(Lo.Val, 1);
6336 break;
6337 }
6338 if (HasMULHU) {
6339 // We can emit a mulhu+mul.
6340 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6341 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6342 break;
6343 }
Dan Gohman5a199552007-10-08 18:33:35 +00006344 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006345 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006346 // The input values are both sign-extended.
6347 if (HasSMUL_LOHI) {
6348 // We can emit a smul_lohi.
6349 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6350 Hi = SDOperand(Lo.Val, 1);
6351 break;
6352 }
6353 if (HasMULHS) {
6354 // We can emit a mulhs+mul.
6355 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6356 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6357 break;
6358 }
6359 }
6360 if (HasUMUL_LOHI) {
6361 // Lo,Hi = umul LHS, RHS.
6362 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6363 DAG.getVTList(NVT, NVT), LL, RL);
6364 Lo = UMulLOHI;
6365 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006366 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6367 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6368 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6369 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6370 break;
6371 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006372 if (HasMULHU) {
6373 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6374 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6375 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6376 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6377 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6378 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6379 break;
6380 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006381 }
6382
Dan Gohman5a199552007-10-08 18:33:35 +00006383 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006384 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006385 break;
6386 }
6387 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006388 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006389 break;
6390 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006391 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006392 break;
6393 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006394 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006395 break;
6396 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006397 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006398 break;
6399
6400 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006401 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6402 RTLIB::ADD_F64,
6403 RTLIB::ADD_F80,
6404 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006405 Node, false, Hi);
6406 break;
6407 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006408 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6409 RTLIB::SUB_F64,
6410 RTLIB::SUB_F80,
6411 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006412 Node, false, Hi);
6413 break;
6414 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006415 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6416 RTLIB::MUL_F64,
6417 RTLIB::MUL_F80,
6418 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006419 Node, false, Hi);
6420 break;
6421 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006422 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6423 RTLIB::DIV_F64,
6424 RTLIB::DIV_F80,
6425 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006426 Node, false, Hi);
6427 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006428 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006429 if (VT == MVT::ppcf128) {
6430 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6431 Node->getOperand(0).getValueType()==MVT::f64);
6432 const uint64_t zero = 0;
6433 if (Node->getOperand(0).getValueType()==MVT::f32)
6434 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6435 else
6436 Hi = Node->getOperand(0);
6437 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6438 break;
6439 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006440 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6441 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6442 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006443 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006444 }
6445 case ISD::FP_ROUND: {
6446 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6447 VT);
6448 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6449 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006451 }
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006452 case ISD::FPOWI:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006453 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32,
6454 RTLIB::POWI_F64,
6455 RTLIB::POWI_F80,
6456 RTLIB::POWI_PPCF128),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006457 Node, false, Hi);
6458 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459 case ISD::FSQRT:
6460 case ISD::FSIN:
6461 case ISD::FCOS: {
6462 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6463 switch(Node->getOpcode()) {
6464 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006465 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6466 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006467 break;
6468 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006469 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6470 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 break;
6472 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006473 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6474 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006475 break;
6476 default: assert(0 && "Unreachable!");
6477 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006478 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006479 break;
6480 }
6481 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006482 if (VT == MVT::ppcf128) {
6483 SDOperand Tmp;
6484 ExpandOp(Node->getOperand(0), Lo, Tmp);
6485 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6486 // lo = hi==fabs(hi) ? lo : -lo;
6487 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6488 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6489 DAG.getCondCode(ISD::SETEQ));
6490 break;
6491 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492 SDOperand Mask = (VT == MVT::f64)
6493 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6494 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6495 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6496 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6497 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6498 if (getTypeAction(NVT) == Expand)
6499 ExpandOp(Lo, Lo, Hi);
6500 break;
6501 }
6502 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006503 if (VT == MVT::ppcf128) {
6504 ExpandOp(Node->getOperand(0), Lo, Hi);
6505 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6506 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6507 break;
6508 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006509 SDOperand Mask = (VT == MVT::f64)
6510 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6511 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6512 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6513 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6514 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6515 if (getTypeAction(NVT) == Expand)
6516 ExpandOp(Lo, Lo, Hi);
6517 break;
6518 }
6519 case ISD::FCOPYSIGN: {
6520 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6521 if (getTypeAction(NVT) == Expand)
6522 ExpandOp(Lo, Lo, Hi);
6523 break;
6524 }
6525 case ISD::SINT_TO_FP:
6526 case ISD::UINT_TO_FP: {
6527 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00006528 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00006529
6530 // Promote the operand if needed. Do this before checking for
6531 // ppcf128 so conversions of i16 and i8 work.
6532 if (getTypeAction(SrcVT) == Promote) {
6533 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6534 Tmp = isSigned
6535 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6536 DAG.getValueType(SrcVT))
6537 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6538 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6539 SrcVT = Node->getOperand(0).getValueType();
6540 }
6541
Dan Gohmanec51f642008-03-10 23:03:31 +00006542 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00006543 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00006544 if (isSigned) {
6545 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6546 Node->getOperand(0)));
6547 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6548 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00006549 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00006550 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6551 Node->getOperand(0)));
6552 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6553 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006554 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006555 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6556 DAG.getConstant(0, MVT::i32),
6557 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6558 DAG.getConstantFP(
6559 APFloat(APInt(128, 2, TwoE32)),
6560 MVT::ppcf128)),
6561 Hi,
6562 DAG.getCondCode(ISD::SETLT)),
6563 Lo, Hi);
6564 }
6565 break;
6566 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006567 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6568 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00006569 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006570 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6571 Lo, Hi);
6572 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6573 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6574 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6575 DAG.getConstant(0, MVT::i64),
6576 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6577 DAG.getConstantFP(
6578 APFloat(APInt(128, 2, TwoE64)),
6579 MVT::ppcf128)),
6580 Hi,
6581 DAG.getCondCode(ISD::SETLT)),
6582 Lo, Hi);
6583 break;
6584 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006585
Dan Gohmanec51f642008-03-10 23:03:31 +00006586 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6587 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00006588 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00006589 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00006590 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006591 break;
6592 }
6593 }
6594
6595 // Make sure the resultant values have been legalized themselves, unless this
6596 // is a type that requires multi-step expansion.
6597 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6598 Lo = LegalizeOp(Lo);
6599 if (Hi.Val)
6600 // Don't legalize the high part if it is expanded to a single node.
6601 Hi = LegalizeOp(Hi);
6602 }
6603
6604 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00006605 bool isNew =
6606 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006607 assert(isNew && "Value already expanded?!?");
6608}
6609
6610/// SplitVectorOp - Given an operand of vector type, break it down into
6611/// two smaller values, still of vector type.
6612void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6613 SDOperand &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00006614 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006615 SDNode *Node = Op.Val;
Duncan Sands92c43912008-06-06 12:08:01 +00006616 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006617 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006618
Duncan Sands92c43912008-06-06 12:08:01 +00006619 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00006620
6621 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6622 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6623
Duncan Sands92c43912008-06-06 12:08:01 +00006624 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
6625 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006627 // See if we already split it.
6628 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6629 = SplitNodes.find(Op);
6630 if (I != SplitNodes.end()) {
6631 Lo = I->second.first;
6632 Hi = I->second.second;
6633 return;
6634 }
6635
6636 switch (Node->getOpcode()) {
6637 default:
6638#ifndef NDEBUG
6639 Node->dump(&DAG);
6640#endif
6641 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006642 case ISD::UNDEF:
6643 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6644 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6645 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006646 case ISD::BUILD_PAIR:
6647 Lo = Node->getOperand(0);
6648 Hi = Node->getOperand(1);
6649 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006650 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00006651 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
6652 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6653 unsigned Index = Idx->getValue();
6654 SDOperand ScalarOp = Node->getOperand(1);
6655 if (Index < NewNumElts_Lo)
6656 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
6657 DAG.getIntPtrConstant(Index));
6658 else
6659 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6660 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
6661 break;
6662 }
6663 SDOperand Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
6664 Node->getOperand(1),
6665 Node->getOperand(2));
6666 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006667 break;
6668 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006669 case ISD::VECTOR_SHUFFLE: {
6670 // Build the low part.
6671 SDOperand Mask = Node->getOperand(2);
6672 SmallVector<SDOperand, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00006673 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00006674
6675 // Insert all of the elements from the input that are needed. We use
6676 // buildvector of extractelement here because the input vectors will have
6677 // to be legalized, so this makes the code simpler.
6678 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006679 SDOperand IdxNode = Mask.getOperand(i);
6680 if (IdxNode.getOpcode() == ISD::UNDEF) {
6681 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6682 continue;
6683 }
6684 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner587c46d2007-11-19 21:16:54 +00006685 SDOperand InVec = Node->getOperand(0);
6686 if (Idx >= NumElements) {
6687 InVec = Node->getOperand(1);
6688 Idx -= NumElements;
6689 }
6690 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6691 DAG.getConstant(Idx, PtrVT)));
6692 }
6693 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6694 Ops.clear();
6695
6696 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006697 SDOperand IdxNode = Mask.getOperand(i);
6698 if (IdxNode.getOpcode() == ISD::UNDEF) {
6699 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6700 continue;
6701 }
6702 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner587c46d2007-11-19 21:16:54 +00006703 SDOperand InVec = Node->getOperand(0);
6704 if (Idx >= NumElements) {
6705 InVec = Node->getOperand(1);
6706 Idx -= NumElements;
6707 }
6708 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6709 DAG.getConstant(Idx, PtrVT)));
6710 }
Mon P Wang2e89b112008-07-25 01:30:26 +00006711 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00006712 break;
6713 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 case ISD::BUILD_VECTOR: {
6715 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006716 Node->op_begin()+NewNumElts_Lo);
6717 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006718
Nate Begeman4a365ad2007-11-15 21:15:26 +00006719 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006720 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006721 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006722 break;
6723 }
6724 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006725 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006726 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6727 if (NewNumSubvectors == 1) {
6728 Lo = Node->getOperand(0);
6729 Hi = Node->getOperand(1);
6730 } else {
6731 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6732 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006733 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006734
6735 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6736 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006737 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006738 }
6739 break;
6740 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006741 case ISD::SELECT: {
6742 SDOperand Cond = Node->getOperand(0);
6743
6744 SDOperand LL, LH, RL, RH;
6745 SplitVectorOp(Node->getOperand(1), LL, LH);
6746 SplitVectorOp(Node->getOperand(2), RL, RH);
6747
Duncan Sands92c43912008-06-06 12:08:01 +00006748 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00006749 // Handle a vector merge.
6750 SDOperand CL, CH;
6751 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006752 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6753 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006754 } else {
6755 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006756 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6757 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006758 }
6759 break;
6760 }
Chris Lattnerc7471452008-06-30 02:43:01 +00006761 case ISD::SELECT_CC: {
6762 SDOperand CondLHS = Node->getOperand(0);
6763 SDOperand CondRHS = Node->getOperand(1);
6764 SDOperand CondCode = Node->getOperand(4);
6765
6766 SDOperand LL, LH, RL, RH;
6767 SplitVectorOp(Node->getOperand(2), LL, LH);
6768 SplitVectorOp(Node->getOperand(3), RL, RH);
6769
6770 // Handle a simple select with vector operands.
6771 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
6772 LL, RL, CondCode);
6773 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
6774 LH, RH, CondCode);
6775 break;
6776 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00006777 case ISD::VSETCC: {
6778 SDOperand LL, LH, RL, RH;
6779 SplitVectorOp(Node->getOperand(0), LL, LH);
6780 SplitVectorOp(Node->getOperand(1), RL, RH);
6781 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
6782 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
6783 break;
6784 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006785 case ISD::ADD:
6786 case ISD::SUB:
6787 case ISD::MUL:
6788 case ISD::FADD:
6789 case ISD::FSUB:
6790 case ISD::FMUL:
6791 case ISD::SDIV:
6792 case ISD::UDIV:
6793 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006794 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006795 case ISD::AND:
6796 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006797 case ISD::XOR:
6798 case ISD::UREM:
6799 case ISD::SREM:
6800 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006801 SDOperand LL, LH, RL, RH;
6802 SplitVectorOp(Node->getOperand(0), LL, LH);
6803 SplitVectorOp(Node->getOperand(1), RL, RH);
6804
Nate Begeman4a365ad2007-11-15 21:15:26 +00006805 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6806 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006807 break;
6808 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006809 case ISD::FPOWI: {
6810 SDOperand L, H;
6811 SplitVectorOp(Node->getOperand(0), L, H);
6812
Nate Begeman4a365ad2007-11-15 21:15:26 +00006813 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6814 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006815 break;
6816 }
6817 case ISD::CTTZ:
6818 case ISD::CTLZ:
6819 case ISD::CTPOP:
6820 case ISD::FNEG:
6821 case ISD::FABS:
6822 case ISD::FSQRT:
6823 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006824 case ISD::FCOS:
6825 case ISD::FP_TO_SINT:
6826 case ISD::FP_TO_UINT:
6827 case ISD::SINT_TO_FP:
6828 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006829 SDOperand L, H;
6830 SplitVectorOp(Node->getOperand(0), L, H);
6831
Nate Begeman4a365ad2007-11-15 21:15:26 +00006832 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6833 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006834 break;
6835 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006836 case ISD::LOAD: {
6837 LoadSDNode *LD = cast<LoadSDNode>(Node);
6838 SDOperand Ch = LD->getChain();
6839 SDOperand Ptr = LD->getBasePtr();
6840 const Value *SV = LD->getSrcValue();
6841 int SVOffset = LD->getSrcValueOffset();
6842 unsigned Alignment = LD->getAlignment();
6843 bool isVolatile = LD->isVolatile();
6844
Nate Begeman4a365ad2007-11-15 21:15:26 +00006845 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Duncan Sands92c43912008-06-06 12:08:01 +00006846 unsigned IncrementSize = NewNumElts_Lo * NewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006847 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006848 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006849 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006850 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006851 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006852
6853 // Build a factor node to remember that this load is independent of the
6854 // other one.
6855 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6856 Hi.getValue(1));
6857
6858 // Remember that we legalized the chain.
6859 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6860 break;
6861 }
6862 case ISD::BIT_CONVERT: {
6863 // We know the result is a vector. The input may be either a vector or a
6864 // scalar value.
6865 SDOperand InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00006866 if (!InOp.getValueType().isVector() ||
6867 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006868 // The input is a scalar or single-element vector.
6869 // Lower to a store/load so that it can be split.
6870 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00006871 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
6872 Op.getValueType().getTypeForMVT());
6873 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Dan Gohman1fc34bc2008-07-11 22:44:52 +00006874 int FI = cast<FrameIndexSDNode>(Ptr.Val)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006875
6876 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006877 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00006878 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00006879 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00006880 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006881 }
6882 // Split the vector and convert each of the pieces now.
6883 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006884 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6885 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006886 break;
6887 }
6888 }
6889
6890 // Remember in a map if the values will be reused later.
6891 bool isNew =
6892 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6893 assert(isNew && "Value already split?!?");
6894}
6895
6896
6897/// ScalarizeVectorOp - Given an operand of single-element vector type
6898/// (e.g. v1f32), convert it into the equivalent operation that returns a
6899/// scalar (e.g. f32) value.
6900SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006901 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006902 SDNode *Node = Op.Val;
Duncan Sands92c43912008-06-06 12:08:01 +00006903 MVT NewVT = Op.getValueType().getVectorElementType();
6904 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006905
6906 // See if we already scalarized it.
6907 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6908 if (I != ScalarizedNodes.end()) return I->second;
6909
6910 SDOperand Result;
6911 switch (Node->getOpcode()) {
6912 default:
6913#ifndef NDEBUG
6914 Node->dump(&DAG); cerr << "\n";
6915#endif
6916 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6917 case ISD::ADD:
6918 case ISD::FADD:
6919 case ISD::SUB:
6920 case ISD::FSUB:
6921 case ISD::MUL:
6922 case ISD::FMUL:
6923 case ISD::SDIV:
6924 case ISD::UDIV:
6925 case ISD::FDIV:
6926 case ISD::SREM:
6927 case ISD::UREM:
6928 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006929 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006930 case ISD::AND:
6931 case ISD::OR:
6932 case ISD::XOR:
6933 Result = DAG.getNode(Node->getOpcode(),
6934 NewVT,
6935 ScalarizeVectorOp(Node->getOperand(0)),
6936 ScalarizeVectorOp(Node->getOperand(1)));
6937 break;
6938 case ISD::FNEG:
6939 case ISD::FABS:
6940 case ISD::FSQRT:
6941 case ISD::FSIN:
6942 case ISD::FCOS:
6943 Result = DAG.getNode(Node->getOpcode(),
6944 NewVT,
6945 ScalarizeVectorOp(Node->getOperand(0)));
6946 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006947 case ISD::FPOWI:
6948 Result = DAG.getNode(Node->getOpcode(),
6949 NewVT,
6950 ScalarizeVectorOp(Node->getOperand(0)),
6951 Node->getOperand(1));
6952 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006953 case ISD::LOAD: {
6954 LoadSDNode *LD = cast<LoadSDNode>(Node);
6955 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6956 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6957
6958 const Value *SV = LD->getSrcValue();
6959 int SVOffset = LD->getSrcValueOffset();
6960 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6961 LD->isVolatile(), LD->getAlignment());
6962
6963 // Remember that we legalized the chain.
6964 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6965 break;
6966 }
6967 case ISD::BUILD_VECTOR:
6968 Result = Node->getOperand(0);
6969 break;
6970 case ISD::INSERT_VECTOR_ELT:
6971 // Returning the inserted scalar element.
6972 Result = Node->getOperand(1);
6973 break;
6974 case ISD::CONCAT_VECTORS:
6975 assert(Node->getOperand(0).getValueType() == NewVT &&
6976 "Concat of non-legal vectors not yet supported!");
6977 Result = Node->getOperand(0);
6978 break;
6979 case ISD::VECTOR_SHUFFLE: {
6980 // Figure out if the scalar is the LHS or RHS and return it.
6981 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6982 if (cast<ConstantSDNode>(EltNum)->getValue())
6983 Result = ScalarizeVectorOp(Node->getOperand(1));
6984 else
6985 Result = ScalarizeVectorOp(Node->getOperand(0));
6986 break;
6987 }
6988 case ISD::EXTRACT_SUBVECTOR:
6989 Result = Node->getOperand(0);
6990 assert(Result.getValueType() == NewVT);
6991 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00006992 case ISD::BIT_CONVERT: {
6993 SDOperand Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00006994 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00006995 Op0 = ScalarizeVectorOp(Op0);
6996 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006997 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00006998 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006999 case ISD::SELECT:
7000 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7001 ScalarizeVectorOp(Op.getOperand(1)),
7002 ScalarizeVectorOp(Op.getOperand(2)));
7003 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007004 case ISD::SELECT_CC:
7005 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7006 Node->getOperand(1),
7007 ScalarizeVectorOp(Op.getOperand(2)),
7008 ScalarizeVectorOp(Op.getOperand(3)),
7009 Node->getOperand(4));
7010 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007011 case ISD::VSETCC: {
7012 SDOperand Op0 = ScalarizeVectorOp(Op.getOperand(0));
7013 SDOperand Op1 = ScalarizeVectorOp(Op.getOperand(1));
7014 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7015 Op.getOperand(2));
7016 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7017 DAG.getConstant(-1ULL, NewVT),
7018 DAG.getConstant(0ULL, NewVT));
7019 break;
7020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007021 }
7022
7023 if (TLI.isTypeLegal(NewVT))
7024 Result = LegalizeOp(Result);
7025 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7026 assert(isNew && "Value already scalarized?");
7027 return Result;
7028}
7029
7030
7031// SelectionDAG::Legalize - This is the entry point for the file.
7032//
7033void SelectionDAG::Legalize() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007034 /// run - This is the main entry point to this class.
7035 ///
7036 SelectionDAGLegalize(*this).LegalizeDAG();
7037}
7038