blob: 55cff6806f9ffb871a688c89afdc9d58b4b8570c [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.
Dan Gohman8181bd12008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
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.
Dan Gohman8181bd12008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> 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.
Dan Gohman8181bd12008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> 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.
Dan Gohman8181bd12008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > 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.
Dan Gohman8181bd12008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
Dan Gohman8181bd12008-07-27 21:46:04 +0000103 void AddLegalizedOperand(SDValue From, SDValue To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 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 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000109 void AddPromotedOperand(SDValue From, SDValue 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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000137 void HandleOp(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000142 SDValue LegalizeOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000148 SDValue UnrollVectorOp(SDValue 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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000154 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
155 SDValue 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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 SDValue PromoteOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 /// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 /// 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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000170 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171
172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
Dan Gohman8181bd12008-07-27 21:46:04 +0000174 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000179 SDValue ScalarizeVectorOp(SDValue O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180
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.
Dan Gohman8181bd12008-07-27 21:46:04 +0000190 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
194
Dan Gohman8181bd12008-07-27 21:46:04 +0000195 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
Dan Gohman8181bd12008-07-27 21:46:04 +0000197 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
198 SDValue &Hi);
199 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
202 SDValue ExpandBUILD_VECTOR(SDNode *Node);
203 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman29c3cef2008-08-14 20:04:46 +0000204 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman8181bd12008-07-27 21:46:04 +0000205 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
206 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
207 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208
Dan Gohman8181bd12008-07-27 21:46:04 +0000209 SDValue ExpandBSWAP(SDValue Op);
210 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
211 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
212 SDValue &Lo, SDValue &Hi);
213 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
214 SDValue &Lo, SDValue &Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215
Dan Gohman8181bd12008-07-27 21:46:04 +0000216 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
217 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218};
219}
220
221/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
222/// specified mask and type. Targets can specify exactly which masks they
223/// support and the code generator is tasked with not creating illegal masks.
224///
225/// Note that this will also return true for shuffles that are promoted to a
226/// different type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000227SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
229 default: return 0;
230 case TargetLowering::Legal:
231 case TargetLowering::Custom:
232 break;
233 case TargetLowering::Promote: {
234 // If this is promoted to a different type, convert the shuffle mask and
235 // ask if it is legal in the promoted type!
Duncan Sands92c43912008-06-06 12:08:01 +0000236 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd3ace282008-07-21 10:20:31 +0000237 MVT EltVT = NVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238
239 // If we changed # elements, change the shuffle mask.
240 unsigned NumEltsGrowth =
Duncan Sands92c43912008-06-06 12:08:01 +0000241 NVT.getVectorNumElements() / VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
243 if (NumEltsGrowth > 1) {
244 // Renumber the elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000245 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000247 SDValue InOp = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
249 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd3ace282008-07-21 10:20:31 +0000250 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 else {
252 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
Duncan Sandsd3ace282008-07-21 10:20:31 +0000253 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 }
255 }
256 }
257 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
258 }
259 VT = NVT;
260 break;
261 }
262 }
263 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
264}
265
266SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
267 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
268 ValueTypeActions(TLI.getValueTypeActions()) {
269 assert(MVT::LAST_VALUETYPE <= 32 &&
270 "Too many value types for ValueTypeActions to hold!");
271}
272
273/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
274/// contains all of a nodes operands before it contains the node.
275static void ComputeTopDownOrdering(SelectionDAG &DAG,
276 SmallVector<SDNode*, 64> &Order) {
277
278 DenseMap<SDNode*, unsigned> Visited;
279 std::vector<SDNode*> Worklist;
280 Worklist.reserve(128);
281
282 // Compute ordering from all of the leaves in the graphs, those (like the
283 // entry node) that have no operands.
284 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
285 E = DAG.allnodes_end(); I != E; ++I) {
286 if (I->getNumOperands() == 0) {
287 Visited[I] = 0 - 1U;
288 Worklist.push_back(I);
289 }
290 }
291
292 while (!Worklist.empty()) {
293 SDNode *N = Worklist.back();
294 Worklist.pop_back();
295
296 if (++Visited[N] != N->getNumOperands())
297 continue; // Haven't visited all operands yet
298
299 Order.push_back(N);
300
301 // Now that we have N in, add anything that uses it if all of their operands
302 // are now done.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000303 Worklist.insert(Worklist.end(), N->use_begin(), N->use_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 }
305
306 assert(Order.size() == Visited.size() &&
Dan Gohman17495de2008-06-20 17:15:19 +0000307 Order.size() == DAG.allnodes_size() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 "Error: DAG is cyclic!");
309}
310
311
312void SelectionDAGLegalize::LegalizeDAG() {
313 LastCALLSEQ_END = DAG.getEntryNode();
314 IsLegalizingCall = false;
315
316 // The legalize process is inherently a bottom-up recursive process (users
317 // legalize their uses before themselves). Given infinite stack space, we
318 // could just start legalizing on the root and traverse the whole graph. In
319 // practice however, this causes us to run out of stack space on large basic
320 // blocks. To avoid this problem, compute an ordering of the nodes where each
321 // node is only legalized after all of its operands are legalized.
322 SmallVector<SDNode*, 64> Order;
323 ComputeTopDownOrdering(DAG, Order);
324
325 for (unsigned i = 0, e = Order.size(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +0000326 HandleOp(SDValue(Order[i], 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
328 // Finally, it's possible the root changed. Get the new root.
Dan Gohman8181bd12008-07-27 21:46:04 +0000329 SDValue OldRoot = DAG.getRoot();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
331 DAG.setRoot(LegalizedNodes[OldRoot]);
332
333 ExpandedNodes.clear();
334 LegalizedNodes.clear();
335 PromotedNodes.clear();
336 SplitNodes.clear();
337 ScalarizedNodes.clear();
338
339 // Remove dead nodes now.
340 DAG.RemoveDeadNodes();
341}
342
343
344/// FindCallEndFromCallStart - Given a chained node that is part of a call
345/// sequence, find the CALLSEQ_END node that terminates the call sequence.
346static SDNode *FindCallEndFromCallStart(SDNode *Node) {
347 if (Node->getOpcode() == ISD::CALLSEQ_END)
348 return Node;
349 if (Node->use_empty())
350 return 0; // No CallSeqEnd
351
352 // The chain is usually at the end.
Dan Gohman8181bd12008-07-27 21:46:04 +0000353 SDValue TheChain(Node, Node->getNumValues()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 if (TheChain.getValueType() != MVT::Other) {
355 // Sometimes it's at the beginning.
Dan Gohman8181bd12008-07-27 21:46:04 +0000356 TheChain = SDValue(Node, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 if (TheChain.getValueType() != MVT::Other) {
358 // Otherwise, hunt for it.
359 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
360 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000361 TheChain = SDValue(Node, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 break;
363 }
364
365 // Otherwise, we walked into a node without a chain.
366 if (TheChain.getValueType() != MVT::Other)
367 return 0;
368 }
369 }
370
371 for (SDNode::use_iterator UI = Node->use_begin(),
372 E = Node->use_end(); UI != E; ++UI) {
373
374 // Make sure to only follow users of our token chain.
Dan Gohman0c97f1d2008-07-27 20:43:25 +0000375 SDNode *User = *UI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
377 if (User->getOperand(i) == TheChain)
378 if (SDNode *Result = FindCallEndFromCallStart(User))
379 return Result;
380 }
381 return 0;
382}
383
384/// FindCallStartFromCallEnd - Given a chained node that is part of a call
385/// sequence, find the CALLSEQ_START node that initiates the call sequence.
386static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
387 assert(Node && "Didn't find callseq_start for a call??");
388 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
389
390 assert(Node->getOperand(0).getValueType() == MVT::Other &&
391 "Node doesn't have a token chain argument!");
392 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
393}
394
395/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
396/// see if any uses can reach Dest. If no dest operands can get to dest,
397/// legalize them, legalize ourself, and return false, otherwise, return true.
398///
399/// Keep track of the nodes we fine that actually do lead to Dest in
400/// NodesLeadingTo. This avoids retraversing them exponential number of times.
401///
402bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
403 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
404 if (N == Dest) return true; // N certainly leads to Dest :)
405
406 // If we've already processed this node and it does lead to Dest, there is no
407 // need to reprocess it.
408 if (NodesLeadingTo.count(N)) return true;
409
410 // If the first result of this node has been already legalized, then it cannot
411 // reach N.
412 switch (getTypeAction(N->getValueType(0))) {
413 case Legal:
Dan Gohman8181bd12008-07-27 21:46:04 +0000414 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 break;
416 case Promote:
Dan Gohman8181bd12008-07-27 21:46:04 +0000417 if (PromotedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 break;
419 case Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +0000420 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 break;
422 }
423
424 // Okay, this node has not already been legalized. Check and legalize all
425 // operands. If none lead to Dest, then we can legalize this node.
426 bool OperandsLeadToDest = false;
427 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
428 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
429 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
430
431 if (OperandsLeadToDest) {
432 NodesLeadingTo.insert(N);
433 return true;
434 }
435
436 // Okay, this node looks safe, legalize it and return false.
Dan Gohman8181bd12008-07-27 21:46:04 +0000437 HandleOp(SDValue(N, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 return false;
439}
440
441/// HandleOp - Legalize, Promote, or Expand the specified operand as
442/// appropriate for its type.
Dan Gohman8181bd12008-07-27 21:46:04 +0000443void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000444 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 switch (getTypeAction(VT)) {
446 default: assert(0 && "Bad type action!");
447 case Legal: (void)LegalizeOp(Op); break;
448 case Promote: (void)PromoteOp(Op); break;
449 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +0000450 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 // If this is an illegal scalar, expand it into its two component
452 // pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +0000453 SDValue X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000454 if (Op.getOpcode() == ISD::TargetConstant)
455 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 ExpandOp(Op, X, Y);
Duncan Sands92c43912008-06-06 12:08:01 +0000457 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 // If this is an illegal single element vector, convert it to a
459 // scalar operation.
460 (void)ScalarizeVectorOp(Op);
461 } else {
462 // Otherwise, this is an illegal multiple element vector.
463 // Split it in half and legalize both parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000464 SDValue X, Y;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 SplitVectorOp(Op, X, Y);
466 }
467 break;
468 }
469}
470
471/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
472/// a load from the constant pool.
Dan Gohman8181bd12008-07-27 21:46:04 +0000473static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 SelectionDAG &DAG, TargetLowering &TLI) {
475 bool Extend = false;
476
477 // If a FP immediate is precise when represented as a float and if the
478 // target can do an extending load from float to double, we put it into
479 // the constant pool as a float, even if it's is statically typed as a
Chris Lattnere718cc52008-03-05 06:46:58 +0000480 // double. This shrinks FP constants and canonicalizes them for targets where
481 // an FP extending load is the same cost as a normal load (such as on the x87
482 // fp stack or PPC FP unit).
Duncan Sands92c43912008-06-06 12:08:01 +0000483 MVT VT = CFP->getValueType(0);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000484 ConstantFP *LLVMC = ConstantFP::get(CFP->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000486 if (VT!=MVT::f64 && VT!=MVT::f32)
487 assert(0 && "Invalid type expansion");
Dan Gohman39509762008-03-11 00:11:06 +0000488 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Cheng354be062008-03-04 08:05:30 +0000489 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 }
491
Duncan Sands92c43912008-06-06 12:08:01 +0000492 MVT OrigVT = VT;
493 MVT SVT = VT;
Evan Cheng354be062008-03-04 08:05:30 +0000494 while (SVT != MVT::f32) {
Duncan Sands92c43912008-06-06 12:08:01 +0000495 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng354be062008-03-04 08:05:30 +0000496 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
497 // Only do this if the target has a native EXTLOAD instruction from
498 // smaller type.
Evan Cheng35190fd2008-03-05 01:30:59 +0000499 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattnere718cc52008-03-05 06:46:58 +0000500 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000501 const Type *SType = SVT.getTypeForMVT();
Evan Cheng354be062008-03-04 08:05:30 +0000502 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
503 VT = SVT;
504 Extend = true;
505 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 }
507
Dan Gohman8181bd12008-07-27 21:46:04 +0000508 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng354be062008-03-04 08:05:30 +0000509 if (Extend)
510 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohmanfb020b62008-02-07 18:41:25 +0000511 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Cheng354be062008-03-04 08:05:30 +0000512 0, VT);
513 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
514 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515}
516
517
518/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
519/// operations.
520static
Dan Gohman8181bd12008-07-27 21:46:04 +0000521SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
522 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +0000523 MVT VT = Node->getValueType(0);
524 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
526 "fcopysign expansion only supported for f32 and f64");
Duncan Sands92c43912008-06-06 12:08:01 +0000527 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528
529 // First get the sign bit of second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000530 SDValue Mask1 = (SrcVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
532 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
533 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000534 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
536 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands92c43912008-06-06 12:08:01 +0000537 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 if (SizeDiff > 0) {
539 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
540 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
541 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnere6fa1452008-07-10 23:46:13 +0000542 } else if (SizeDiff < 0) {
543 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
544 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
545 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
546 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547
548 // Clear the sign bit of first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000549 SDValue Mask2 = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
551 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
552 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman8181bd12008-07-27 21:46:04 +0000553 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
555
556 // Or the value with the sign bit.
557 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
558 return Result;
559}
560
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000561/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
562static
Dan Gohman8181bd12008-07-27 21:46:04 +0000563SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
564 TargetLowering &TLI) {
565 SDValue Chain = ST->getChain();
566 SDValue Ptr = ST->getBasePtr();
567 SDValue Val = ST->getValue();
Duncan Sands92c43912008-06-06 12:08:01 +0000568 MVT VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000569 int Alignment = ST->getAlignment();
570 int SVOffset = ST->getSrcValueOffset();
Duncan Sands92c43912008-06-06 12:08:01 +0000571 if (ST->getMemoryVT().isFloatingPoint() ||
572 ST->getMemoryVT().isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
Duncan Sands92c43912008-06-06 12:08:01 +0000575 MVT intVT;
576 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000577 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000578 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000579 intVT = MVT::i64;
580 else if (VT==MVT::f32)
581 intVT = MVT::i32;
582 else
Dale Johannesenb1d1ab92008-02-28 18:36:51 +0000583 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000584
Dan Gohman8181bd12008-07-27 21:46:04 +0000585 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen08275382007-09-08 19:29:23 +0000586 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
587 SVOffset, ST->isVolatile(), Alignment);
588 }
Duncan Sands92c43912008-06-06 12:08:01 +0000589 assert(ST->getMemoryVT().isInteger() &&
590 !ST->getMemoryVT().isVector() &&
Dale Johannesen08275382007-09-08 19:29:23 +0000591 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000592 // Get the half-size VT
Duncan Sands92c43912008-06-06 12:08:01 +0000593 MVT NewStoredVT =
594 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
595 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000596 int IncrementSize = NumBits / 8;
597
598 // Divide the stored value in two parts.
Dan Gohman8181bd12008-07-27 21:46:04 +0000599 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
600 SDValue Lo = Val;
601 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000602
603 // Store the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000604 SDValue Store1, Store2;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000605 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
606 ST->getSrcValue(), SVOffset, NewStoredVT,
607 ST->isVolatile(), Alignment);
608 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
609 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000610 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000611 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
612 ST->getSrcValue(), SVOffset + IncrementSize,
613 NewStoredVT, ST->isVolatile(), Alignment);
614
615 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
616}
617
618/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
619static
Dan Gohman8181bd12008-07-27 21:46:04 +0000620SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
621 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000622 int SVOffset = LD->getSrcValueOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +0000623 SDValue Chain = LD->getChain();
624 SDValue Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +0000625 MVT VT = LD->getValueType(0);
626 MVT LoadedVT = LD->getMemoryVT();
627 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen08275382007-09-08 19:29:23 +0000628 // Expand to a (misaligned) integer load of the same size,
Dale Johannesendc0ee192008-02-27 22:36:00 +0000629 // then bitconvert to floating point or vector.
Duncan Sands92c43912008-06-06 12:08:01 +0000630 MVT intVT;
631 if (LoadedVT.is128BitVector() ||
Dale Johannesenf8c1e852008-03-01 03:40:57 +0000632 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesendc0ee192008-02-27 22:36:00 +0000633 intVT = MVT::i128;
Duncan Sands92c43912008-06-06 12:08:01 +0000634 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000635 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000636 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000637 intVT = MVT::i32;
638 else
Dale Johannesendc0ee192008-02-27 22:36:00 +0000639 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen08275382007-09-08 19:29:23 +0000640
Dan Gohman8181bd12008-07-27 21:46:04 +0000641 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen08275382007-09-08 19:29:23 +0000642 SVOffset, LD->isVolatile(),
643 LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +0000644 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands92c43912008-06-06 12:08:01 +0000645 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen08275382007-09-08 19:29:23 +0000646 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
647
Dan Gohman8181bd12008-07-27 21:46:04 +0000648 SDValue Ops[] = { Result, Chain };
Duncan Sands698842f2008-07-02 17:40:58 +0000649 return DAG.getMergeValues(Ops, 2);
Dale Johannesen08275382007-09-08 19:29:23 +0000650 }
Duncan Sands92c43912008-06-06 12:08:01 +0000651 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000652 "Unaligned load of unsupported type.");
653
Dale Johannesendc0ee192008-02-27 22:36:00 +0000654 // Compute the new VT that is half the size of the old one. This is an
655 // integer MVT.
Duncan Sands92c43912008-06-06 12:08:01 +0000656 unsigned NumBits = LoadedVT.getSizeInBits();
657 MVT NewLoadedVT;
658 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000659 NumBits >>= 1;
660
661 unsigned Alignment = LD->getAlignment();
662 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000663 ISD::LoadExtType HiExtType = LD->getExtensionType();
664
665 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
666 if (HiExtType == ISD::NON_EXTLOAD)
667 HiExtType = ISD::ZEXTLOAD;
668
669 // Load the value in two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000670 SDValue Lo, Hi;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000671 if (TLI.isLittleEndian()) {
672 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
673 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
674 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
675 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
676 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
677 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000678 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000679 } else {
680 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
681 NewLoadedVT,LD->isVolatile(), Alignment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
683 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
684 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
685 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000686 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000687 }
688
689 // aggregate the two parts
Dan Gohman8181bd12008-07-27 21:46:04 +0000690 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
691 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000692 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
693
Dan Gohman8181bd12008-07-27 21:46:04 +0000694 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000695 Hi.getValue(1));
696
Dan Gohman8181bd12008-07-27 21:46:04 +0000697 SDValue Ops[] = { Result, TF };
Duncan Sands698842f2008-07-02 17:40:58 +0000698 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000699}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700
Dan Gohman6d05cac2007-10-11 23:57:53 +0000701/// UnrollVectorOp - We know that the given vector has a legal type, however
702/// the operation it performs is not legal and is an operation that we have
703/// no way of lowering. "Unroll" the vector, splitting out the scalars and
704/// operating on each element individually.
Dan Gohman8181bd12008-07-27 21:46:04 +0000705SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +0000706 MVT VT = Op.getValueType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000707 assert(isTypeLegal(VT) &&
708 "Caller should expand or promote operands that are not legal!");
709 assert(Op.Val->getNumValues() == 1 &&
710 "Can't unroll a vector with multiple results!");
Duncan Sands92c43912008-06-06 12:08:01 +0000711 unsigned NE = VT.getVectorNumElements();
712 MVT EltVT = VT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000713
Dan Gohman8181bd12008-07-27 21:46:04 +0000714 SmallVector<SDValue, 8> Scalars;
715 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman6d05cac2007-10-11 23:57:53 +0000716 for (unsigned i = 0; i != NE; ++i) {
717 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000718 SDValue Operand = Op.getOperand(j);
Duncan Sands92c43912008-06-06 12:08:01 +0000719 MVT OperandVT = Operand.getValueType();
720 if (OperandVT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +0000721 // A vector operand; extract a single element.
Duncan Sands92c43912008-06-06 12:08:01 +0000722 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman6d05cac2007-10-11 23:57:53 +0000723 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
724 OperandEltVT,
725 Operand,
726 DAG.getConstant(i, MVT::i32));
727 } else {
728 // A scalar operand; just use it as is.
729 Operands[j] = Operand;
730 }
731 }
732 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
733 &Operands[0], Operands.size()));
734 }
735
736 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
737}
738
Duncan Sands37a3f472008-01-10 10:28:30 +0000739/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands92c43912008-06-06 12:08:01 +0000740static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands37a3f472008-01-10 10:28:30 +0000741 RTLIB::Libcall Call_F32,
742 RTLIB::Libcall Call_F64,
743 RTLIB::Libcall Call_F80,
744 RTLIB::Libcall Call_PPCF128) {
745 return
746 VT == MVT::f32 ? Call_F32 :
747 VT == MVT::f64 ? Call_F64 :
748 VT == MVT::f80 ? Call_F80 :
749 VT == MVT::ppcf128 ? Call_PPCF128 :
750 RTLIB::UNKNOWN_LIBCALL;
751}
752
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000753/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
754/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
755/// is necessary to spill the vector being inserted into to memory, perform
756/// the insert there, and then read the result back.
Dan Gohman8181bd12008-07-27 21:46:04 +0000757SDValue SelectionDAGLegalize::
758PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
759 SDValue Tmp1 = Vec;
760 SDValue Tmp2 = Val;
761 SDValue Tmp3 = Idx;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000762
763 // If the target doesn't support this, we have to spill the input vector
764 // to a temporary stack slot, update the element, then reload it. This is
765 // badness. We could also load the value into a vector register (either
766 // with a "move to register" or "extload into register" instruction, then
767 // permute it into place, if the idx is a constant and if the idx is
768 // supported by the target.
Duncan Sands92c43912008-06-06 12:08:01 +0000769 MVT VT = Tmp1.getValueType();
770 MVT EltVT = VT.getVectorElementType();
771 MVT IdxVT = Tmp3.getValueType();
772 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +0000773 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000774
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000775 int SPFI = cast<FrameIndexSDNode>(StackPtr.Val)->getIndex();
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000776
777 // Store the vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000778 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000779 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000780
781 // Truncate or zero extend offset to target pointer type.
Duncan Sandsec142ee2008-06-08 20:54:56 +0000782 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000783 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
784 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +0000785 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000786 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman8181bd12008-07-27 21:46:04 +0000787 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000788 // Store the scalar value.
789 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000790 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000791 // Load the updated vector.
Dan Gohman1fc34bc2008-07-11 22:44:52 +0000792 return DAG.getLoad(VT, Ch, StackPtr,
793 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman7c9e4b72008-04-25 18:07:40 +0000794}
795
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796/// LegalizeOp - We know that the specified value has a legal type, and
797/// that its operands are legal. Now ensure that the operation itself
798/// is legal, recursively ensuring that the operands' operations remain
799/// legal.
Dan Gohman8181bd12008-07-27 21:46:04 +0000800SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000801 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
802 return Op;
803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 assert(isTypeLegal(Op.getValueType()) &&
805 "Caller should expand or promote operands that are not legal!");
806 SDNode *Node = Op.Val;
807
808 // If this operation defines any values that cannot be represented in a
809 // register on this target, make sure to expand or promote them.
810 if (Node->getNumValues() > 1) {
811 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
812 if (getTypeAction(Node->getValueType(i)) != Legal) {
813 HandleOp(Op.getValue(i));
814 assert(LegalizedNodes.count(Op) &&
815 "Handling didn't add legal operands!");
816 return LegalizedNodes[Op];
817 }
818 }
819
820 // Note that LegalizeOp may be reentered even from single-use nodes, which
821 // means that we always must cache transformed nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +0000822 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 if (I != LegalizedNodes.end()) return I->second;
824
Dan Gohman8181bd12008-07-27 21:46:04 +0000825 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
826 SDValue Result = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 bool isCustom = false;
828
829 switch (Node->getOpcode()) {
830 case ISD::FrameIndex:
831 case ISD::EntryToken:
832 case ISD::Register:
833 case ISD::BasicBlock:
834 case ISD::TargetFrameIndex:
835 case ISD::TargetJumpTable:
836 case ISD::TargetConstant:
837 case ISD::TargetConstantFP:
838 case ISD::TargetConstantPool:
839 case ISD::TargetGlobalAddress:
840 case ISD::TargetGlobalTLSAddress:
841 case ISD::TargetExternalSymbol:
842 case ISD::VALUETYPE:
843 case ISD::SRCVALUE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000844 case ISD::MEMOPERAND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 case ISD::CONDCODE:
Duncan Sandsc93fae32008-03-21 09:14:45 +0000846 case ISD::ARG_FLAGS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000848 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 "This must be legal!");
850 break;
851 default:
852 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
853 // If this is a target node, legalize it by legalizing the operands then
854 // passing it through.
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
857 Ops.push_back(LegalizeOp(Node->getOperand(i)));
858
859 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
860
861 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
862 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
863 return Result.getValue(Op.ResNo);
864 }
865 // Otherwise this is an unhandled builtin node. splat.
866#ifndef NDEBUG
867 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
868#endif
869 assert(0 && "Do not know how to legalize this operator!");
870 abort();
871 case ISD::GLOBAL_OFFSET_TABLE:
872 case ISD::GlobalAddress:
873 case ISD::GlobalTLSAddress:
874 case ISD::ExternalSymbol:
875 case ISD::ConstantPool:
876 case ISD::JumpTable: // Nothing to do.
877 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
878 default: assert(0 && "This action is not supported yet!");
879 case TargetLowering::Custom:
880 Tmp1 = TLI.LowerOperation(Op, DAG);
881 if (Tmp1.Val) Result = Tmp1;
882 // FALLTHROUGH if the target doesn't want to lower this op after all.
883 case TargetLowering::Legal:
884 break;
885 }
886 break;
887 case ISD::FRAMEADDR:
888 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 // The only option for these nodes is to custom lower them. If the target
890 // does not custom lower them, then return zero.
891 Tmp1 = TLI.LowerOperation(Op, DAG);
892 if (Tmp1.Val)
893 Result = Tmp1;
894 else
895 Result = DAG.getConstant(0, TLI.getPointerTy());
896 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000897 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands92c43912008-06-06 12:08:01 +0000898 MVT VT = Node->getValueType(0);
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000899 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
900 default: assert(0 && "This action is not supported yet!");
901 case TargetLowering::Custom:
902 Result = TLI.LowerOperation(Op, DAG);
903 if (Result.Val) break;
904 // Fall Thru
905 case TargetLowering::Legal:
906 Result = DAG.getConstant(0, VT);
907 break;
908 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000909 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000910 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 case ISD::EXCEPTIONADDR: {
912 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +0000913 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
915 default: assert(0 && "This action is not supported yet!");
916 case TargetLowering::Expand: {
917 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000918 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
920 break;
921 case TargetLowering::Custom:
922 Result = TLI.LowerOperation(Op, DAG);
923 if (Result.Val) break;
924 // Fall Thru
925 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000926 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands698842f2008-07-02 17:40:58 +0000927 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 break;
929 }
930 }
931 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000932 if (Result.Val->getNumValues() == 1) break;
933
934 assert(Result.Val->getNumValues() == 2 &&
935 "Cannot return more than two values!");
936
937 // Since we produced two values, make sure to remember that we
938 // legalized both of them.
939 Tmp1 = LegalizeOp(Result);
940 Tmp2 = LegalizeOp(Result.getValue(1));
941 AddLegalizedOperand(Op.getValue(0), Tmp1);
942 AddLegalizedOperand(Op.getValue(1), Tmp2);
943 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 case ISD::EHSELECTION: {
945 Tmp1 = LegalizeOp(Node->getOperand(0));
946 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +0000947 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
949 default: assert(0 && "This action is not supported yet!");
950 case TargetLowering::Expand: {
951 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000952 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 }
954 break;
955 case TargetLowering::Custom:
956 Result = TLI.LowerOperation(Op, DAG);
957 if (Result.Val) break;
958 // Fall Thru
959 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000960 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands698842f2008-07-02 17:40:58 +0000961 Result = DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 break;
963 }
964 }
965 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000966 if (Result.Val->getNumValues() == 1) break;
967
968 assert(Result.Val->getNumValues() == 2 &&
969 "Cannot return more than two values!");
970
971 // Since we produced two values, make sure to remember that we
972 // legalized both of them.
973 Tmp1 = LegalizeOp(Result);
974 Tmp2 = LegalizeOp(Result.getValue(1));
975 AddLegalizedOperand(Op.getValue(0), Tmp1);
976 AddLegalizedOperand(Op.getValue(1), Tmp2);
977 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 case ISD::EH_RETURN: {
Duncan Sands92c43912008-06-06 12:08:01 +0000979 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 // The only "good" option for this node is to custom lower it.
981 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
982 default: assert(0 && "This action is not supported at all!");
983 case TargetLowering::Custom:
984 Result = TLI.LowerOperation(Op, DAG);
985 if (Result.Val) break;
986 // Fall Thru
987 case TargetLowering::Legal:
988 // Target does not know, how to lower this, lower to noop
989 Result = LegalizeOp(Node->getOperand(0));
990 break;
991 }
992 }
993 break;
994 case ISD::AssertSext:
995 case ISD::AssertZext:
996 Tmp1 = LegalizeOp(Node->getOperand(0));
997 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
998 break;
999 case ISD::MERGE_VALUES:
1000 // Legalize eliminates MERGE_VALUES nodes.
1001 Result = Node->getOperand(Op.ResNo);
1002 break;
1003 case ISD::CopyFromReg:
1004 Tmp1 = LegalizeOp(Node->getOperand(0));
1005 Result = Op.getValue(0);
1006 if (Node->getNumValues() == 2) {
1007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1008 } else {
1009 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
1010 if (Node->getNumOperands() == 3) {
1011 Tmp2 = LegalizeOp(Node->getOperand(2));
1012 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1013 } else {
1014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1015 }
1016 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1017 }
1018 // Since CopyFromReg produces two values, make sure to remember that we
1019 // legalized both of them.
1020 AddLegalizedOperand(Op.getValue(0), Result);
1021 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1022 return Result.getValue(Op.ResNo);
1023 case ISD::UNDEF: {
Duncan Sands92c43912008-06-06 12:08:01 +00001024 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
1026 default: assert(0 && "This action is not supported yet!");
1027 case TargetLowering::Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00001028 if (VT.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 Result = DAG.getConstant(0, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00001030 else if (VT.isFloatingPoint())
1031 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesen20b76352007-09-26 17:26:49 +00001032 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033 else
1034 assert(0 && "Unknown value type!");
1035 break;
1036 case TargetLowering::Legal:
1037 break;
1038 }
1039 break;
1040 }
1041
1042 case ISD::INTRINSIC_W_CHAIN:
1043 case ISD::INTRINSIC_WO_CHAIN:
1044 case ISD::INTRINSIC_VOID: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001045 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1047 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1048 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1049
1050 // Allow the target to custom lower its intrinsics if it wants to.
1051 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1052 TargetLowering::Custom) {
1053 Tmp3 = TLI.LowerOperation(Result, DAG);
1054 if (Tmp3.Val) Result = Tmp3;
1055 }
1056
1057 if (Result.Val->getNumValues() == 1) break;
1058
1059 // Must have return value and chain result.
1060 assert(Result.Val->getNumValues() == 2 &&
1061 "Cannot return more than two values!");
1062
1063 // Since loads produce two values, make sure to remember that we
1064 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001065 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1066 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 return Result.getValue(Op.ResNo);
1068 }
1069
Dan Gohman472d12c2008-06-30 20:59:49 +00001070 case ISD::DBG_STOPPOINT:
1071 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1073
Dan Gohman472d12c2008-06-30 20:59:49 +00001074 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 case TargetLowering::Promote:
1076 default: assert(0 && "This action is not supported yet!");
1077 case TargetLowering::Expand: {
1078 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1079 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001080 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081
Dan Gohman472d12c2008-06-30 20:59:49 +00001082 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman472d12c2008-06-30 20:59:49 +00001084 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1085 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086
Dan Gohman472d12c2008-06-30 20:59:49 +00001087 unsigned Line = DSP->getLine();
1088 unsigned Col = DSP->getColumn();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089
1090 if (useDEBUG_LOC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001091 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Chengd6f57682008-07-08 20:06:39 +00001092 DAG.getConstant(Col, MVT::i32),
1093 DAG.getConstant(SrcFile, MVT::i32) };
1094 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095 } else {
Evan Cheng69eda822008-02-01 02:05:57 +00001096 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001097 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 }
1099 } else {
1100 Result = Tmp1; // chain
1101 }
1102 break;
1103 }
Evan Chengd6f57682008-07-08 20:06:39 +00001104 case TargetLowering::Legal: {
1105 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1106 if (Action == Legal && Tmp1 == Node->getOperand(0))
1107 break;
1108
Dan Gohman8181bd12008-07-27 21:46:04 +00001109 SmallVector<SDValue, 8> Ops;
Evan Chengd6f57682008-07-08 20:06:39 +00001110 Ops.push_back(Tmp1);
1111 if (Action == Legal) {
1112 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1113 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1114 } else {
1115 // Otherwise promote them.
1116 Ops.push_back(PromoteOp(Node->getOperand(1)));
1117 Ops.push_back(PromoteOp(Node->getOperand(2)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 }
Evan Chengd6f57682008-07-08 20:06:39 +00001119 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1120 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1121 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 break;
1123 }
Evan Chengd6f57682008-07-08 20:06:39 +00001124 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001126
1127 case ISD::DECLARE:
1128 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1129 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1130 default: assert(0 && "This action is not supported yet!");
1131 case TargetLowering::Legal:
1132 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1133 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1134 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1136 break;
Chris Lattner203cd052008-02-28 05:53:40 +00001137 case TargetLowering::Expand:
1138 Result = LegalizeOp(Node->getOperand(0));
1139 break;
Evan Cheng2e28d622008-02-02 04:07:54 +00001140 }
1141 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142
1143 case ISD::DEBUG_LOC:
1144 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1145 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1146 default: assert(0 && "This action is not supported yet!");
Evan Chengd6f57682008-07-08 20:06:39 +00001147 case TargetLowering::Legal: {
1148 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001149 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Chengd6f57682008-07-08 20:06:39 +00001150 if (Action == Legal && Tmp1 == Node->getOperand(0))
1151 break;
1152 if (Action == Legal) {
1153 Tmp2 = Node->getOperand(1);
1154 Tmp3 = Node->getOperand(2);
1155 Tmp4 = Node->getOperand(3);
1156 } else {
1157 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1158 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1159 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1160 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1162 break;
1163 }
Evan Chengd6f57682008-07-08 20:06:39 +00001164 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 break;
1166
Dan Gohmanfa607c92008-07-01 00:05:16 +00001167 case ISD::DBG_LABEL:
1168 case ISD::EH_LABEL:
1169 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1170 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001171 default: assert(0 && "This action is not supported yet!");
1172 case TargetLowering::Legal:
1173 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfa607c92008-07-01 00:05:16 +00001174 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 break;
1176 case TargetLowering::Expand:
1177 Result = LegalizeOp(Node->getOperand(0));
1178 break;
1179 }
1180 break;
1181
Evan Chengd1d68072008-03-08 00:58:38 +00001182 case ISD::PREFETCH:
1183 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1184 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1185 default: assert(0 && "This action is not supported yet!");
1186 case TargetLowering::Legal:
1187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1188 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1189 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1190 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1191 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1192 break;
1193 case TargetLowering::Expand:
1194 // It's a noop.
1195 Result = LegalizeOp(Node->getOperand(0));
1196 break;
1197 }
1198 break;
1199
Andrew Lenharth785610d2008-02-16 01:24:58 +00001200 case ISD::MEMBARRIER: {
1201 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001202 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1203 default: assert(0 && "This action is not supported yet!");
1204 case TargetLowering::Legal: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001205 SDValue Ops[6];
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001206 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands3ee041a2008-02-27 08:53:44 +00001207 for (int x = 1; x < 6; ++x) {
1208 Ops[x] = Node->getOperand(x);
1209 if (!isTypeLegal(Ops[x].getValueType()))
1210 Ops[x] = PromoteOp(Ops[x]);
1211 }
Andrew Lenharth0531ec52008-02-16 14:46:26 +00001212 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1213 break;
1214 }
1215 case TargetLowering::Expand:
1216 //There is no libgcc call for this op
1217 Result = Node->getOperand(0); // Noop
1218 break;
1219 }
Andrew Lenharth785610d2008-02-16 01:24:58 +00001220 break;
1221 }
1222
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001223 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang078a62d2008-05-05 19:05:59 +00001224 unsigned int num_operands = 4;
1225 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001226 SDValue Ops[4];
Mon P Wang078a62d2008-05-05 19:05:59 +00001227 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001228 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang078a62d2008-05-05 19:05:59 +00001229 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1230
1231 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1232 default: assert(0 && "This action is not supported yet!");
1233 case TargetLowering::Custom:
1234 Result = TLI.LowerOperation(Result, DAG);
1235 break;
1236 case TargetLowering::Legal:
1237 break;
1238 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001239 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1240 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Mon P Wang078a62d2008-05-05 19:05:59 +00001241 return Result.getValue(Op.ResNo);
Duncan Sandsac496a12008-07-04 11:47:58 +00001242 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001243 case ISD::ATOMIC_LOAD_ADD:
1244 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang078a62d2008-05-05 19:05:59 +00001245 case ISD::ATOMIC_LOAD_AND:
1246 case ISD::ATOMIC_LOAD_OR:
1247 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharthaf02d592008-06-14 05:48:15 +00001248 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang078a62d2008-05-05 19:05:59 +00001249 case ISD::ATOMIC_LOAD_MIN:
1250 case ISD::ATOMIC_LOAD_MAX:
1251 case ISD::ATOMIC_LOAD_UMIN:
1252 case ISD::ATOMIC_LOAD_UMAX:
1253 case ISD::ATOMIC_SWAP: {
1254 unsigned int num_operands = 3;
1255 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001256 SDValue Ops[3];
Mon P Wang078a62d2008-05-05 19:05:59 +00001257 for (unsigned int x = 0; x < num_operands; ++x)
1258 Ops[x] = LegalizeOp(Node->getOperand(x));
1259 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sandsac496a12008-07-04 11:47:58 +00001260
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001261 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001262 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001263 case TargetLowering::Custom:
1264 Result = TLI.LowerOperation(Result, DAG);
1265 break;
Mon P Wang078a62d2008-05-05 19:05:59 +00001266 case TargetLowering::Expand:
Dan Gohman8181bd12008-07-27 21:46:04 +00001267 Result = SDValue(TLI.ReplaceNodeResults(Op.Val, DAG),0);
Mon P Wang078a62d2008-05-05 19:05:59 +00001268 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001269 case TargetLowering::Legal:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00001270 break;
1271 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001272 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1273 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00001274 return Result.getValue(Op.ResNo);
Duncan Sandsac496a12008-07-04 11:47:58 +00001275 }
Scott Michelf2e2b702007-08-08 23:23:31 +00001276 case ISD::Constant: {
1277 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1278 unsigned opAction =
1279 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 // We know we don't need to expand constants here, constants only have one
1282 // value and we check that it is fine above.
1283
Scott Michelf2e2b702007-08-08 23:23:31 +00001284 if (opAction == TargetLowering::Custom) {
1285 Tmp1 = TLI.LowerOperation(Result, DAG);
1286 if (Tmp1.Val)
1287 Result = Tmp1;
1288 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001290 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 case ISD::ConstantFP: {
1292 // Spill FP immediates to the constant pool if the target cannot directly
1293 // codegen them. Targets often have some immediate values that can be
1294 // efficiently generated into an FP register without a load. We explicitly
1295 // leave these constants as ConstantFP nodes for the target to deal with.
1296 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1299 default: assert(0 && "This action is not supported yet!");
Nate Begemane2ba64f2008-02-14 08:57:00 +00001300 case TargetLowering::Legal:
1301 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 case TargetLowering::Custom:
1303 Tmp3 = TLI.LowerOperation(Result, DAG);
1304 if (Tmp3.Val) {
1305 Result = Tmp3;
1306 break;
1307 }
1308 // FALLTHROUGH
Nate Begemane2ba64f2008-02-14 08:57:00 +00001309 case TargetLowering::Expand: {
1310 // Check to see if this FP immediate is already legal.
1311 bool isLegal = false;
1312 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1313 E = TLI.legal_fpimm_end(); I != E; ++I) {
1314 if (CFP->isExactlyValue(*I)) {
1315 isLegal = true;
1316 break;
1317 }
1318 }
1319 // If this is a legal constant, turn it into a TargetConstantFP node.
1320 if (isLegal)
1321 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1323 }
Nate Begemane2ba64f2008-02-14 08:57:00 +00001324 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 break;
1326 }
1327 case ISD::TokenFactor:
1328 if (Node->getNumOperands() == 2) {
1329 Tmp1 = LegalizeOp(Node->getOperand(0));
1330 Tmp2 = LegalizeOp(Node->getOperand(1));
1331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1332 } else if (Node->getNumOperands() == 3) {
1333 Tmp1 = LegalizeOp(Node->getOperand(0));
1334 Tmp2 = LegalizeOp(Node->getOperand(1));
1335 Tmp3 = LegalizeOp(Node->getOperand(2));
1336 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1337 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00001338 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 // Legalize the operands.
1340 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1341 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1342 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1343 }
1344 break;
1345
1346 case ISD::FORMAL_ARGUMENTS:
1347 case ISD::CALL:
1348 // The only option for this is to custom lower it.
1349 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1350 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesenac246272008-03-05 19:14:03 +00001351 // A call within a calling sequence must be legalized to something
1352 // other than the normal CALLSEQ_END. Violating this gets Legalize
1353 // into an infinite loop.
1354 assert ((!IsLegalizingCall ||
1355 Node->getOpcode() != ISD::CALL ||
1356 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1357 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001358
1359 // The number of incoming and outgoing values should match; unless the final
1360 // outgoing value is a flag.
1361 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1362 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1363 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1364 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 "Lowering call/formal_arguments produced unexpected # results!");
1366
1367 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1368 // remember that we legalized all of them, so it doesn't get relegalized.
1369 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001370 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1371 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1373 if (Op.ResNo == i)
1374 Tmp2 = Tmp1;
Dan Gohman8181bd12008-07-27 21:46:04 +00001375 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 }
1377 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001378 case ISD::EXTRACT_SUBREG: {
1379 Tmp1 = LegalizeOp(Node->getOperand(0));
1380 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1381 assert(idx && "Operand must be a constant");
1382 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1383 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1384 }
1385 break;
1386 case ISD::INSERT_SUBREG: {
1387 Tmp1 = LegalizeOp(Node->getOperand(0));
1388 Tmp2 = LegalizeOp(Node->getOperand(1));
1389 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1390 assert(idx && "Operand must be a constant");
1391 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1393 }
1394 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 case ISD::BUILD_VECTOR:
1396 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1397 default: assert(0 && "This action is not supported yet!");
1398 case TargetLowering::Custom:
1399 Tmp3 = TLI.LowerOperation(Result, DAG);
1400 if (Tmp3.Val) {
1401 Result = Tmp3;
1402 break;
1403 }
1404 // FALLTHROUGH
1405 case TargetLowering::Expand:
1406 Result = ExpandBUILD_VECTOR(Result.Val);
1407 break;
1408 }
1409 break;
1410 case ISD::INSERT_VECTOR_ELT:
1411 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001413
1414 // The type of the value to insert may not be legal, even though the vector
1415 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1416 // here.
1417 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1418 default: assert(0 && "Cannot expand insert element operand");
1419 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1420 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1421 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1423
1424 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1425 Node->getValueType(0))) {
1426 default: assert(0 && "This action is not supported yet!");
1427 case TargetLowering::Legal:
1428 break;
1429 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001430 Tmp4 = TLI.LowerOperation(Result, DAG);
1431 if (Tmp4.Val) {
1432 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 break;
1434 }
1435 // FALLTHROUGH
1436 case TargetLowering::Expand: {
1437 // If the insert index is a constant, codegen this as a scalar_to_vector,
1438 // then a shuffle that inserts it into the right position in the vector.
1439 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001440 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1441 // match the element type of the vector being created.
1442 if (Tmp2.getValueType() ==
Duncan Sands92c43912008-06-06 12:08:01 +00001443 Op.getValueType().getVectorElementType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001444 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001445 Tmp1.getValueType(), Tmp2);
1446
Duncan Sands92c43912008-06-06 12:08:01 +00001447 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1448 MVT ShufMaskVT =
1449 MVT::getIntVectorWithNumElements(NumElts);
1450 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001451
1452 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1453 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1454 // elt 0 of the RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00001455 SmallVector<SDValue, 8> ShufOps;
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001456 for (unsigned i = 0; i != NumElts; ++i) {
1457 if (i != InsertPos->getValue())
1458 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1459 else
1460 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1461 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001462 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman6fb7ebd2008-02-13 06:43:04 +00001463 &ShufOps[0], ShufOps.size());
1464
1465 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1466 Tmp1, ScVec, ShufMask);
1467 Result = LegalizeOp(Result);
1468 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 }
Nate Begeman7c9e4b72008-04-25 18:07:40 +00001471 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 break;
1473 }
1474 }
1475 break;
1476 case ISD::SCALAR_TO_VECTOR:
1477 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1478 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1479 break;
1480 }
1481
1482 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1483 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1484 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1485 Node->getValueType(0))) {
1486 default: assert(0 && "This action is not supported yet!");
1487 case TargetLowering::Legal:
1488 break;
1489 case TargetLowering::Custom:
1490 Tmp3 = TLI.LowerOperation(Result, DAG);
1491 if (Tmp3.Val) {
1492 Result = Tmp3;
1493 break;
1494 }
1495 // FALLTHROUGH
1496 case TargetLowering::Expand:
1497 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1498 break;
1499 }
1500 break;
1501 case ISD::VECTOR_SHUFFLE:
1502 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1503 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1505
1506 // Allow targets to custom lower the SHUFFLEs they support.
1507 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1508 default: assert(0 && "Unknown operation action!");
1509 case TargetLowering::Legal:
1510 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1511 "vector shuffle should not be created if not legal!");
1512 break;
1513 case TargetLowering::Custom:
1514 Tmp3 = TLI.LowerOperation(Result, DAG);
1515 if (Tmp3.Val) {
1516 Result = Tmp3;
1517 break;
1518 }
1519 // FALLTHROUGH
1520 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00001521 MVT VT = Node->getValueType(0);
1522 MVT EltVT = VT.getVectorElementType();
1523 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00001524 SDValue Mask = Node->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00001526 SmallVector<SDValue,8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001528 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 if (Arg.getOpcode() == ISD::UNDEF) {
1530 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1531 } else {
1532 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1533 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1534 if (Idx < NumElems)
1535 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1536 DAG.getConstant(Idx, PtrVT)));
1537 else
1538 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1539 DAG.getConstant(Idx - NumElems, PtrVT)));
1540 }
1541 }
1542 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1543 break;
1544 }
1545 case TargetLowering::Promote: {
1546 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001547 MVT OVT = Node->getValueType(0);
1548 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549
1550 // Cast the two input vectors.
1551 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1552 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1553
1554 // Convert the shuffle mask to the right # elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00001555 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 assert(Tmp3.Val && "Shuffle not legal?");
1557 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1558 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1559 break;
1560 }
1561 }
1562 break;
1563
1564 case ISD::EXTRACT_VECTOR_ELT:
1565 Tmp1 = Node->getOperand(0);
1566 Tmp2 = LegalizeOp(Node->getOperand(1));
1567 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1568 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1569 break;
1570
1571 case ISD::EXTRACT_SUBVECTOR:
1572 Tmp1 = Node->getOperand(0);
1573 Tmp2 = LegalizeOp(Node->getOperand(1));
1574 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1575 Result = ExpandEXTRACT_SUBVECTOR(Result);
1576 break;
1577
1578 case ISD::CALLSEQ_START: {
1579 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1580
1581 // Recursively Legalize all of the inputs of the call end that do not lead
1582 // to this call start. This ensures that any libcalls that need be inserted
1583 // are inserted *before* the CALLSEQ_START.
1584 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1585 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1586 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1587 NodesLeadingTo);
1588 }
1589
1590 // Now that we legalized all of the inputs (which may have inserted
1591 // libcalls) create the new CALLSEQ_START node.
1592 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1593
1594 // Merge in the last call, to ensure that this call start after the last
1595 // call ended.
1596 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1597 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1598 Tmp1 = LegalizeOp(Tmp1);
1599 }
1600
1601 // Do not try to legalize the target-specific arguments (#1+).
1602 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001603 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001604 Ops[0] = Tmp1;
1605 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1606 }
1607
1608 // Remember that the CALLSEQ_START is legalized.
1609 AddLegalizedOperand(Op.getValue(0), Result);
1610 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1611 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1612
1613 // Now that the callseq_start and all of the non-call nodes above this call
1614 // sequence have been legalized, legalize the call itself. During this
1615 // process, no libcalls can/will be inserted, guaranteeing that no calls
1616 // can overlap.
1617 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001618 // Note that we are selecting this call!
Dan Gohman8181bd12008-07-27 21:46:04 +00001619 LastCALLSEQ_END = SDValue(CallEnd, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620 IsLegalizingCall = true;
1621
1622 // Legalize the call, starting from the CALLSEQ_END.
1623 LegalizeOp(LastCALLSEQ_END);
1624 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1625 return Result;
1626 }
1627 case ISD::CALLSEQ_END:
1628 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1629 // will cause this node to be legalized as well as handling libcalls right.
1630 if (LastCALLSEQ_END.Val != Node) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001631 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1632 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 assert(I != LegalizedNodes.end() &&
1634 "Legalizing the call start should have legalized this node!");
1635 return I->second;
1636 }
1637
1638 // Otherwise, the call start has been legalized and everything is going
1639 // according to plan. Just legalize ourselves normally here.
1640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 // Do not try to legalize the target-specific arguments (#1+), except for
1642 // an optional flag input.
1643 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1644 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001645 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 Ops[0] = Tmp1;
1647 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1648 }
1649 } else {
1650 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1651 if (Tmp1 != Node->getOperand(0) ||
1652 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001653 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001654 Ops[0] = Tmp1;
1655 Ops.back() = Tmp2;
1656 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1657 }
1658 }
1659 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1660 // This finishes up call legalization.
1661 IsLegalizingCall = false;
1662
1663 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001664 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665 if (Node->getNumValues() == 2)
Dan Gohman8181bd12008-07-27 21:46:04 +00001666 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001667 return Result.getValue(Op.ResNo);
1668 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands92c43912008-06-06 12:08:01 +00001669 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001670 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1671 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1672 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1673 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1674
1675 Tmp1 = Result.getValue(0);
1676 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001677 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 default: assert(0 && "This action is not supported yet!");
1679 case TargetLowering::Expand: {
1680 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1681 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1682 " not tell us which reg is the stack pointer!");
Dan Gohman8181bd12008-07-27 21:46:04 +00001683 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001684
1685 // Chain the dynamic stack allocation so that it doesn't modify the stack
1686 // pointer when other instructions are using the stack.
1687 Chain = DAG.getCALLSEQ_START(Chain,
1688 DAG.getConstant(0, TLI.getPointerTy()));
1689
Dan Gohman8181bd12008-07-27 21:46:04 +00001690 SDValue Size = Tmp2.getOperand(1);
1691 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Chenga448bc42007-08-16 23:50:06 +00001692 Chain = SP.getValue(1);
1693 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1694 unsigned StackAlign =
1695 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1696 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001697 SP = DAG.getNode(ISD::AND, VT, SP,
1698 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001699 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001700 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1701
1702 Tmp2 =
1703 DAG.getCALLSEQ_END(Chain,
1704 DAG.getConstant(0, TLI.getPointerTy()),
1705 DAG.getConstant(0, TLI.getPointerTy()),
Dan Gohman8181bd12008-07-27 21:46:04 +00001706 SDValue());
Bill Wendling22f8deb2007-11-13 00:44:25 +00001707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 Tmp1 = LegalizeOp(Tmp1);
1709 Tmp2 = LegalizeOp(Tmp2);
1710 break;
1711 }
1712 case TargetLowering::Custom:
1713 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1714 if (Tmp3.Val) {
1715 Tmp1 = LegalizeOp(Tmp3);
1716 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1717 }
1718 break;
1719 case TargetLowering::Legal:
1720 break;
1721 }
1722 // Since this op produce two values, make sure to remember that we
1723 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001724 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1725 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001726 return Op.ResNo ? Tmp2 : Tmp1;
1727 }
1728 case ISD::INLINEASM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001729 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 bool Changed = false;
1731 // Legalize all of the operands of the inline asm, in case they are nodes
1732 // that need to be expanded or something. Note we skip the asm string and
1733 // all of the TargetConstant flags.
Dan Gohman8181bd12008-07-27 21:46:04 +00001734 SDValue Op = LegalizeOp(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 Changed = Op != Ops[0];
1736 Ops[0] = Op;
1737
1738 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1739 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1740 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1741 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001742 SDValue Op = LegalizeOp(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001743 if (Op != Ops[i]) {
1744 Changed = true;
1745 Ops[i] = Op;
1746 }
1747 }
1748 }
1749
1750 if (HasInFlag) {
1751 Op = LegalizeOp(Ops.back());
1752 Changed |= Op != Ops.back();
1753 Ops.back() = Op;
1754 }
1755
1756 if (Changed)
1757 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1758
1759 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman8181bd12008-07-27 21:46:04 +00001760 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1761 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 return Result.getValue(Op.ResNo);
1763 }
1764 case ISD::BR:
1765 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1766 // Ensure that libcalls are emitted before a branch.
1767 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1768 Tmp1 = LegalizeOp(Tmp1);
1769 LastCALLSEQ_END = DAG.getEntryNode();
1770
1771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1772 break;
1773 case ISD::BRIND:
1774 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1775 // Ensure that libcalls are emitted before a branch.
1776 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1777 Tmp1 = LegalizeOp(Tmp1);
1778 LastCALLSEQ_END = DAG.getEntryNode();
1779
1780 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1781 default: assert(0 && "Indirect target must be legal type (pointer)!");
1782 case Legal:
1783 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1784 break;
1785 }
1786 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1787 break;
1788 case ISD::BR_JT:
1789 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1790 // Ensure that libcalls are emitted before a branch.
1791 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1792 Tmp1 = LegalizeOp(Tmp1);
1793 LastCALLSEQ_END = DAG.getEntryNode();
1794
1795 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1796 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1797
1798 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1799 default: assert(0 && "This action is not supported yet!");
1800 case TargetLowering::Legal: break;
1801 case TargetLowering::Custom:
1802 Tmp1 = TLI.LowerOperation(Result, DAG);
1803 if (Tmp1.Val) Result = Tmp1;
1804 break;
1805 case TargetLowering::Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00001806 SDValue Chain = Result.getOperand(0);
1807 SDValue Table = Result.getOperand(1);
1808 SDValue Index = Result.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809
Duncan Sands92c43912008-06-06 12:08:01 +00001810 MVT PTy = TLI.getPointerTy();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 MachineFunction &MF = DAG.getMachineFunction();
1812 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1813 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman8181bd12008-07-27 21:46:04 +00001814 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001815
Dan Gohman8181bd12008-07-27 21:46:04 +00001816 SDValue LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001817 switch (EntrySize) {
1818 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001819 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001820 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman12a9c082008-02-06 22:27:42 +00001821 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001822 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001823 }
1824
Evan Cheng6fb06762007-11-09 01:32:10 +00001825 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1827 // For PIC, the sequence is:
1828 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001829 // RelocBase can be JumpTable, GOT or some sort of global base.
1830 if (PTy != MVT::i32)
1831 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1832 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1833 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001834 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001835 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001836 }
1837 }
1838 break;
1839 case ISD::BRCOND:
1840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1841 // Ensure that libcalls are emitted before a return.
1842 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1843 Tmp1 = LegalizeOp(Tmp1);
1844 LastCALLSEQ_END = DAG.getEntryNode();
1845
1846 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1847 case Expand: assert(0 && "It's impossible to expand bools");
1848 case Legal:
1849 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1850 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00001851 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001852 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1853
1854 // The top bits of the promoted condition are not necessarily zero, ensure
1855 // that the value is properly zero extended.
Dan Gohman07961cd2008-02-25 21:11:39 +00001856 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001857 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman07961cd2008-02-25 21:11:39 +00001858 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1860 break;
1861 }
Dan Gohman07961cd2008-02-25 21:11:39 +00001862 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001863
1864 // Basic block destination (Op#2) is always legal.
1865 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1866
1867 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1868 default: assert(0 && "This action is not supported yet!");
1869 case TargetLowering::Legal: break;
1870 case TargetLowering::Custom:
1871 Tmp1 = TLI.LowerOperation(Result, DAG);
1872 if (Tmp1.Val) Result = Tmp1;
1873 break;
1874 case TargetLowering::Expand:
1875 // Expand brcond's setcc into its constituent parts and create a BR_CC
1876 // Node.
1877 if (Tmp2.getOpcode() == ISD::SETCC) {
1878 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1879 Tmp2.getOperand(0), Tmp2.getOperand(1),
1880 Node->getOperand(2));
1881 } else {
1882 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1883 DAG.getCondCode(ISD::SETNE), Tmp2,
1884 DAG.getConstant(0, Tmp2.getValueType()),
1885 Node->getOperand(2));
1886 }
1887 break;
1888 }
1889 break;
1890 case ISD::BR_CC:
1891 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1892 // Ensure that libcalls are emitted before a branch.
1893 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1894 Tmp1 = LegalizeOp(Tmp1);
1895 Tmp2 = Node->getOperand(2); // LHS
1896 Tmp3 = Node->getOperand(3); // RHS
1897 Tmp4 = Node->getOperand(1); // CC
1898
1899 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1900 LastCALLSEQ_END = DAG.getEntryNode();
1901
1902 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1903 // the LHS is a legal SETCC itself. In this case, we need to compare
1904 // the result against zero to select between true and false values.
1905 if (Tmp3.Val == 0) {
1906 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1907 Tmp4 = DAG.getCondCode(ISD::SETNE);
1908 }
1909
1910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1911 Node->getOperand(4));
1912
1913 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1914 default: assert(0 && "Unexpected action for BR_CC!");
1915 case TargetLowering::Legal: break;
1916 case TargetLowering::Custom:
1917 Tmp4 = TLI.LowerOperation(Result, DAG);
1918 if (Tmp4.Val) Result = Tmp4;
1919 break;
1920 }
1921 break;
1922 case ISD::LOAD: {
1923 LoadSDNode *LD = cast<LoadSDNode>(Node);
1924 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1925 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1926
1927 ISD::LoadExtType ExtType = LD->getExtensionType();
1928 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands92c43912008-06-06 12:08:01 +00001929 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1931 Tmp3 = Result.getValue(0);
1932 Tmp4 = Result.getValue(1);
1933
1934 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1935 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001936 case TargetLowering::Legal:
1937 // If this is an unaligned load and the target doesn't support it,
1938 // expand it.
1939 if (!TLI.allowsUnalignedMemoryAccesses()) {
1940 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00001941 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001942 if (LD->getAlignment() < ABIAlignment){
1943 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1944 TLI);
1945 Tmp3 = Result.getOperand(0);
1946 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001947 Tmp3 = LegalizeOp(Tmp3);
1948 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001949 }
1950 }
1951 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001952 case TargetLowering::Custom:
1953 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1954 if (Tmp1.Val) {
1955 Tmp3 = LegalizeOp(Tmp1);
1956 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1957 }
1958 break;
1959 case TargetLowering::Promote: {
1960 // Only promote a load of vector type to another.
Duncan Sands92c43912008-06-06 12:08:01 +00001961 assert(VT.isVector() && "Cannot promote this load!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 // Change base type to a different vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00001963 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964
1965 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1966 LD->getSrcValueOffset(),
1967 LD->isVolatile(), LD->getAlignment());
1968 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1969 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1970 break;
1971 }
1972 }
1973 // Since loads produce two values, make sure to remember that we
1974 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00001975 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1976 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977 return Op.ResNo ? Tmp4 : Tmp3;
1978 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00001979 MVT SrcVT = LD->getMemoryVT();
1980 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands082524c2008-01-23 20:39:46 +00001981 int SVOffset = LD->getSrcValueOffset();
1982 unsigned Alignment = LD->getAlignment();
1983 bool isVolatile = LD->isVolatile();
1984
Duncan Sands92c43912008-06-06 12:08:01 +00001985 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands082524c2008-01-23 20:39:46 +00001986 // Some targets pretend to have an i1 loading operation, and actually
1987 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1988 // bits are guaranteed to be zero; it helps the optimizers understand
1989 // that these bits are zero. It is also useful for EXTLOAD, since it
1990 // tells the optimizers that those bits are undefined. It would be
1991 // nice to have an effective generic way of getting these benefits...
1992 // Until such a way is found, don't insist on promoting i1 here.
1993 (SrcVT != MVT::i1 ||
1994 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1995 // Promote to a byte-sized load if not loading an integral number of
1996 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands92c43912008-06-06 12:08:01 +00001997 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1998 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00001999 SDValue Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002000
2001 // The extra bits are guaranteed to be zero, since we stored them that
2002 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2003
2004 ISD::LoadExtType NewExtType =
2005 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2006
2007 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2008 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2009 NVT, isVolatile, Alignment);
2010
2011 Ch = Result.getValue(1); // The chain.
2012
2013 if (ExtType == ISD::SEXTLOAD)
2014 // Having the top bits zero doesn't help when sign extending.
2015 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2016 Result, DAG.getValueType(SrcVT));
2017 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2018 // All the top bits are guaranteed to be zero - inform the optimizers.
2019 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2020 DAG.getValueType(SrcVT));
2021
2022 Tmp1 = LegalizeOp(Result);
2023 Tmp2 = LegalizeOp(Ch);
2024 } else if (SrcWidth & (SrcWidth - 1)) {
2025 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands92c43912008-06-06 12:08:01 +00002026 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands082524c2008-01-23 20:39:46 +00002027 "Unsupported extload!");
2028 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2029 assert(RoundWidth < SrcWidth);
2030 unsigned ExtraWidth = SrcWidth - RoundWidth;
2031 assert(ExtraWidth < RoundWidth);
2032 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2033 "Load size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002034 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2035 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002036 SDValue Lo, Hi, Ch;
Duncan Sands082524c2008-01-23 20:39:46 +00002037 unsigned IncrementSize;
2038
2039 if (TLI.isLittleEndian()) {
2040 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2041 // Load the bottom RoundWidth bits.
2042 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2043 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2044 Alignment);
2045
2046 // Load the remaining ExtraWidth bits.
2047 IncrementSize = RoundWidth / 8;
2048 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2049 DAG.getIntPtrConstant(IncrementSize));
2050 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2051 LD->getSrcValue(), SVOffset + IncrementSize,
2052 ExtraVT, isVolatile,
2053 MinAlign(Alignment, IncrementSize));
2054
2055 // Build a factor node to remember that this load is independent of the
2056 // other one.
2057 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2058 Hi.getValue(1));
2059
2060 // Move the top bits to the right place.
2061 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2062 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2063
2064 // Join the hi and lo parts.
2065 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002066 } else {
Duncan Sands082524c2008-01-23 20:39:46 +00002067 // Big endian - avoid unaligned loads.
2068 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2069 // Load the top RoundWidth bits.
2070 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2071 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2072 Alignment);
2073
2074 // Load the remaining ExtraWidth bits.
2075 IncrementSize = RoundWidth / 8;
2076 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2077 DAG.getIntPtrConstant(IncrementSize));
2078 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2079 LD->getSrcValue(), SVOffset + IncrementSize,
2080 ExtraVT, isVolatile,
2081 MinAlign(Alignment, IncrementSize));
2082
2083 // Build a factor node to remember that this load is independent of the
2084 // other one.
2085 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2086 Hi.getValue(1));
2087
2088 // Move the top bits to the right place.
2089 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2090 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2091
2092 // Join the hi and lo parts.
2093 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2094 }
2095
2096 Tmp1 = LegalizeOp(Result);
2097 Tmp2 = LegalizeOp(Ch);
2098 } else {
2099 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2100 default: assert(0 && "This action is not supported yet!");
2101 case TargetLowering::Custom:
2102 isCustom = true;
2103 // FALLTHROUGH
2104 case TargetLowering::Legal:
2105 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2106 Tmp1 = Result.getValue(0);
2107 Tmp2 = Result.getValue(1);
2108
2109 if (isCustom) {
2110 Tmp3 = TLI.LowerOperation(Result, DAG);
2111 if (Tmp3.Val) {
2112 Tmp1 = LegalizeOp(Tmp3);
2113 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2114 }
2115 } else {
2116 // If this is an unaligned load and the target doesn't support it,
2117 // expand it.
2118 if (!TLI.allowsUnalignedMemoryAccesses()) {
2119 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002120 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands082524c2008-01-23 20:39:46 +00002121 if (LD->getAlignment() < ABIAlignment){
2122 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2123 TLI);
2124 Tmp1 = Result.getOperand(0);
2125 Tmp2 = Result.getOperand(1);
2126 Tmp1 = LegalizeOp(Tmp1);
2127 Tmp2 = LegalizeOp(Tmp2);
2128 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002129 }
2130 }
Duncan Sands082524c2008-01-23 20:39:46 +00002131 break;
2132 case TargetLowering::Expand:
2133 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2134 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002135 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands082524c2008-01-23 20:39:46 +00002136 LD->getSrcValueOffset(),
2137 LD->isVolatile(), LD->getAlignment());
2138 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2139 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2140 Tmp2 = LegalizeOp(Load.getValue(1));
2141 break;
2142 }
2143 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2144 // Turn the unsupported load into an EXTLOAD followed by an explicit
2145 // zero/sign extend inreg.
2146 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2147 Tmp1, Tmp2, LD->getSrcValue(),
2148 LD->getSrcValueOffset(), SrcVT,
2149 LD->isVolatile(), LD->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00002150 SDValue ValRes;
Duncan Sands082524c2008-01-23 20:39:46 +00002151 if (ExtType == ISD::SEXTLOAD)
2152 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2153 Result, DAG.getValueType(SrcVT));
2154 else
2155 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2156 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2157 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 break;
2159 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 }
Duncan Sands082524c2008-01-23 20:39:46 +00002161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002162 // Since loads produce two values, make sure to remember that we legalized
2163 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002164 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2165 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002166 return Op.ResNo ? Tmp2 : Tmp1;
2167 }
2168 }
2169 case ISD::EXTRACT_ELEMENT: {
Duncan Sands92c43912008-06-06 12:08:01 +00002170 MVT OpTy = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 switch (getTypeAction(OpTy)) {
2172 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
2173 case Legal:
2174 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2175 // 1 -> Hi
2176 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands92c43912008-06-06 12:08:01 +00002177 DAG.getConstant(OpTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002178 TLI.getShiftAmountTy()));
2179 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2180 } else {
2181 // 0 -> Lo
2182 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2183 Node->getOperand(0));
2184 }
2185 break;
2186 case Expand:
2187 // Get both the low and high parts.
2188 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2189 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2190 Result = Tmp2; // 1 -> Hi
2191 else
2192 Result = Tmp1; // 0 -> Lo
2193 break;
2194 }
2195 break;
2196 }
2197
2198 case ISD::CopyToReg:
2199 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2200
2201 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
2202 "Register type must be legal!");
2203 // Legalize the incoming value (must be a legal type).
2204 Tmp2 = LegalizeOp(Node->getOperand(2));
2205 if (Node->getNumValues() == 1) {
2206 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
2207 } else {
2208 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
2209 if (Node->getNumOperands() == 4) {
2210 Tmp3 = LegalizeOp(Node->getOperand(3));
2211 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2212 Tmp3);
2213 } else {
2214 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
2215 }
2216
2217 // Since this produces two values, make sure to remember that we legalized
2218 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002219 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2220 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002221 return Result;
2222 }
2223 break;
2224
2225 case ISD::RET:
2226 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2227
2228 // Ensure that libcalls are emitted before a return.
2229 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2230 Tmp1 = LegalizeOp(Tmp1);
2231 LastCALLSEQ_END = DAG.getEntryNode();
2232
2233 switch (Node->getNumOperands()) {
2234 case 3: // ret val
2235 Tmp2 = Node->getOperand(1);
2236 Tmp3 = Node->getOperand(2); // Signness
2237 switch (getTypeAction(Tmp2.getValueType())) {
2238 case Legal:
2239 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
2240 break;
2241 case Expand:
Duncan Sands92c43912008-06-06 12:08:01 +00002242 if (!Tmp2.getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002243 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 ExpandOp(Tmp2, Lo, Hi);
2245
2246 // Big endian systems want the hi reg first.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002247 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002248 std::swap(Lo, Hi);
2249
2250 if (Hi.Val)
2251 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2252 else
2253 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
2254 Result = LegalizeOp(Result);
2255 } else {
2256 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002257 int InIx = Tmp2.ResNo;
Duncan Sands92c43912008-06-06 12:08:01 +00002258 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2259 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260
2261 // Figure out if there is a simple type corresponding to this Vector
2262 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002263 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 if (TLI.isTypeLegal(TVT)) {
2265 // Turn this into a return of the vector type.
2266 Tmp2 = LegalizeOp(Tmp2);
2267 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2268 } else if (NumElems == 1) {
2269 // Turn this into a return of the scalar type.
2270 Tmp2 = ScalarizeVectorOp(Tmp2);
2271 Tmp2 = LegalizeOp(Tmp2);
2272 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2273
2274 // FIXME: Returns of gcc generic vectors smaller than a legal type
2275 // should be returned in integer registers!
2276
2277 // The scalarized value type may not be legal, e.g. it might require
2278 // promotion or expansion. Relegalize the return.
2279 Result = LegalizeOp(Result);
2280 } else {
2281 // FIXME: Returns of gcc generic vectors larger than a legal vector
2282 // type should be returned by reference!
Dan Gohman8181bd12008-07-27 21:46:04 +00002283 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 SplitVectorOp(Tmp2, Lo, Hi);
2285 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2286 Result = LegalizeOp(Result);
2287 }
2288 }
2289 break;
2290 case Promote:
2291 Tmp2 = PromoteOp(Node->getOperand(1));
2292 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2293 Result = LegalizeOp(Result);
2294 break;
2295 }
2296 break;
2297 case 1: // ret void
2298 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2299 break;
2300 default: { // ret <values>
Dan Gohman8181bd12008-07-27 21:46:04 +00002301 SmallVector<SDValue, 8> NewValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 NewValues.push_back(Tmp1);
2303 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2304 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2305 case Legal:
2306 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2307 NewValues.push_back(Node->getOperand(i+1));
2308 break;
2309 case Expand: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002310 SDValue Lo, Hi;
Duncan Sands92c43912008-06-06 12:08:01 +00002311 assert(!Node->getOperand(i).getValueType().isExtended() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002312 "FIXME: TODO: implement returning non-legal vector types!");
2313 ExpandOp(Node->getOperand(i), Lo, Hi);
2314 NewValues.push_back(Lo);
2315 NewValues.push_back(Node->getOperand(i+1));
2316 if (Hi.Val) {
2317 NewValues.push_back(Hi);
2318 NewValues.push_back(Node->getOperand(i+1));
2319 }
2320 break;
2321 }
2322 case Promote:
2323 assert(0 && "Can't promote multiple return value yet!");
2324 }
2325
2326 if (NewValues.size() == Node->getNumOperands())
2327 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2328 else
2329 Result = DAG.getNode(ISD::RET, MVT::Other,
2330 &NewValues[0], NewValues.size());
2331 break;
2332 }
2333 }
2334
2335 if (Result.getOpcode() == ISD::RET) {
2336 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2337 default: assert(0 && "This action is not supported yet!");
2338 case TargetLowering::Legal: break;
2339 case TargetLowering::Custom:
2340 Tmp1 = TLI.LowerOperation(Result, DAG);
2341 if (Tmp1.Val) Result = Tmp1;
2342 break;
2343 }
2344 }
2345 break;
2346 case ISD::STORE: {
2347 StoreSDNode *ST = cast<StoreSDNode>(Node);
2348 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2349 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2350 int SVOffset = ST->getSrcValueOffset();
2351 unsigned Alignment = ST->getAlignment();
2352 bool isVolatile = ST->isVolatile();
2353
2354 if (!ST->isTruncatingStore()) {
2355 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2356 // FIXME: We shouldn't do this for TargetConstantFP's.
2357 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2358 // to phase ordering between legalized code and the dag combiner. This
2359 // probably means that we need to integrate dag combiner and legalizer
2360 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002361 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002362 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002363 if (CFP->getValueType(0) == MVT::f32 &&
2364 getTypeAction(MVT::i32) == Legal) {
Dan Gohman39509762008-03-11 00:11:06 +00002365 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2366 convertToAPInt().zextOrTrunc(32),
Dale Johannesen1616e902007-09-11 18:32:33 +00002367 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002368 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2369 SVOffset, isVolatile, Alignment);
2370 break;
2371 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002372 // If this target supports 64-bit registers, do a single 64-bit store.
2373 if (getTypeAction(MVT::i64) == Legal) {
2374 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman39509762008-03-11 00:11:06 +00002375 zextOrTrunc(64), MVT::i64);
Chris Lattner19f229a2007-10-15 05:46:06 +00002376 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2377 SVOffset, isVolatile, Alignment);
2378 break;
Duncan Sands2418bec2008-06-13 19:07:40 +00002379 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002380 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2381 // stores. If the target supports neither 32- nor 64-bits, this
2382 // xform is certainly not worth it.
Dan Gohman39509762008-03-11 00:11:06 +00002383 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
Dan Gohman8181bd12008-07-27 21:46:04 +00002384 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2385 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002386 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner19f229a2007-10-15 05:46:06 +00002387
2388 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2389 SVOffset, isVolatile, Alignment);
2390 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002391 DAG.getIntPtrConstant(4));
Chris Lattner19f229a2007-10-15 05:46:06 +00002392 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002393 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002394
2395 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2396 break;
2397 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 }
2400
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002401 switch (getTypeAction(ST->getMemoryVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002402 case Legal: {
2403 Tmp3 = LegalizeOp(ST->getValue());
2404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2405 ST->getOffset());
2406
Duncan Sands92c43912008-06-06 12:08:01 +00002407 MVT VT = Tmp3.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002408 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2409 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002410 case TargetLowering::Legal:
2411 // If this is an unaligned store and the target doesn't support it,
2412 // expand it.
2413 if (!TLI.allowsUnalignedMemoryAccesses()) {
2414 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002415 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002416 if (ST->getAlignment() < ABIAlignment)
2417 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2418 TLI);
2419 }
2420 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 case TargetLowering::Custom:
2422 Tmp1 = TLI.LowerOperation(Result, DAG);
2423 if (Tmp1.Val) Result = Tmp1;
2424 break;
2425 case TargetLowering::Promote:
Duncan Sands92c43912008-06-06 12:08:01 +00002426 assert(VT.isVector() && "Unknown legal promote case!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2428 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2429 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2430 ST->getSrcValue(), SVOffset, isVolatile,
2431 Alignment);
2432 break;
2433 }
2434 break;
2435 }
2436 case Promote:
2437 // Truncate the value and store the result.
2438 Tmp3 = PromoteOp(ST->getValue());
2439 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002440 SVOffset, ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 isVolatile, Alignment);
2442 break;
2443
2444 case Expand:
2445 unsigned IncrementSize = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002446 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447
2448 // If this is a vector type, then we have to calculate the increment as
2449 // the product of the element size in bytes, and the number of elements
2450 // in the high half of the vector.
Duncan Sands92c43912008-06-06 12:08:01 +00002451 if (ST->getValue().getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002453 int InIx = ST->getValue().ResNo;
Duncan Sands92c43912008-06-06 12:08:01 +00002454 MVT InVT = InVal->getValueType(InIx);
2455 unsigned NumElems = InVT.getVectorNumElements();
2456 MVT EVT = InVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457
2458 // Figure out if there is a simple type corresponding to this Vector
2459 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00002460 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461 if (TLI.isTypeLegal(TVT)) {
2462 // Turn this into a normal store of the vector type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002463 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002464 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2465 SVOffset, isVolatile, Alignment);
2466 Result = LegalizeOp(Result);
2467 break;
2468 } else if (NumElems == 1) {
2469 // Turn this into a normal store of the scalar type.
Dan Gohmane9f633d2008-02-15 18:11:59 +00002470 Tmp3 = ScalarizeVectorOp(ST->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2472 SVOffset, isVolatile, Alignment);
2473 // The scalarized value type may not be legal, e.g. it might require
2474 // promotion or expansion. Relegalize the scalar store.
2475 Result = LegalizeOp(Result);
2476 break;
2477 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002478 SplitVectorOp(ST->getValue(), Lo, Hi);
Duncan Sands92c43912008-06-06 12:08:01 +00002479 IncrementSize = Lo.Val->getValueType(0).getVectorNumElements() *
2480 EVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 }
2482 } else {
Dan Gohmane9f633d2008-02-15 18:11:59 +00002483 ExpandOp(ST->getValue(), Lo, Hi);
Duncan Sands92c43912008-06-06 12:08:01 +00002484 IncrementSize = Hi.Val ? Hi.getValueType().getSizeInBits()/8 : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002485
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00002486 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 std::swap(Lo, Hi);
2488 }
2489
2490 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2491 SVOffset, isVolatile, Alignment);
2492
2493 if (Hi.Val == NULL) {
2494 // Must be int <-> float one-to-one expansion.
2495 Result = Lo;
2496 break;
2497 }
2498
2499 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner5872a362008-01-17 07:00:52 +00002500 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 assert(isTypeLegal(Tmp2.getValueType()) &&
2502 "Pointers must be legal!");
2503 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002504 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2506 SVOffset, isVolatile, Alignment);
2507 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2508 break;
2509 }
2510 } else {
Chris Lattner3bc08502008-01-17 19:59:44 +00002511 switch (getTypeAction(ST->getValue().getValueType())) {
2512 case Legal:
2513 Tmp3 = LegalizeOp(ST->getValue());
2514 break;
2515 case Promote:
2516 // We can promote the value, the truncstore will still take care of it.
2517 Tmp3 = PromoteOp(ST->getValue());
2518 break;
2519 case Expand:
2520 // Just store the low part. This may become a non-trunc store, so make
2521 // sure to use getTruncStore, not UpdateNodeOperands below.
2522 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2523 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2524 SVOffset, MVT::i8, isVolatile, Alignment);
2525 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002526
Duncan Sands92c43912008-06-06 12:08:01 +00002527 MVT StVT = ST->getMemoryVT();
2528 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands40676662008-01-22 07:17:34 +00002529
Duncan Sands92c43912008-06-06 12:08:01 +00002530 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands40676662008-01-22 07:17:34 +00002531 // Promote to a byte-sized store with upper bits zero if not
2532 // storing an integral number of bytes. For example, promote
2533 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands92c43912008-06-06 12:08:01 +00002534 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands40676662008-01-22 07:17:34 +00002535 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2536 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2537 SVOffset, NVT, isVolatile, Alignment);
2538 } else if (StWidth & (StWidth - 1)) {
2539 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands92c43912008-06-06 12:08:01 +00002540 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands40676662008-01-22 07:17:34 +00002541 "Unsupported truncstore!");
2542 unsigned RoundWidth = 1 << Log2_32(StWidth);
2543 assert(RoundWidth < StWidth);
2544 unsigned ExtraWidth = StWidth - RoundWidth;
2545 assert(ExtraWidth < RoundWidth);
2546 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2547 "Store size not an integral number of bytes!");
Duncan Sands92c43912008-06-06 12:08:01 +00002548 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2549 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman8181bd12008-07-27 21:46:04 +00002550 SDValue Lo, Hi;
Duncan Sands40676662008-01-22 07:17:34 +00002551 unsigned IncrementSize;
2552
2553 if (TLI.isLittleEndian()) {
2554 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2555 // Store the bottom RoundWidth bits.
2556 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2557 SVOffset, RoundVT,
2558 isVolatile, Alignment);
2559
2560 // Store the remaining ExtraWidth bits.
2561 IncrementSize = RoundWidth / 8;
2562 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2563 DAG.getIntPtrConstant(IncrementSize));
2564 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2565 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2566 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2567 SVOffset + IncrementSize, ExtraVT, isVolatile,
2568 MinAlign(Alignment, IncrementSize));
2569 } else {
2570 // Big endian - avoid unaligned stores.
2571 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2572 // Store the top RoundWidth bits.
2573 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2574 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2575 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2576 RoundVT, isVolatile, Alignment);
2577
2578 // Store the remaining ExtraWidth bits.
2579 IncrementSize = RoundWidth / 8;
2580 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2581 DAG.getIntPtrConstant(IncrementSize));
2582 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2583 SVOffset + IncrementSize, ExtraVT, isVolatile,
2584 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002585 }
Duncan Sands40676662008-01-22 07:17:34 +00002586
2587 // The order of the stores doesn't matter.
2588 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2589 } else {
2590 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2591 Tmp2 != ST->getBasePtr())
2592 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2593 ST->getOffset());
2594
2595 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2596 default: assert(0 && "This action is not supported yet!");
2597 case TargetLowering::Legal:
2598 // If this is an unaligned store and the target doesn't support it,
2599 // expand it.
2600 if (!TLI.allowsUnalignedMemoryAccesses()) {
2601 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00002602 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands40676662008-01-22 07:17:34 +00002603 if (ST->getAlignment() < ABIAlignment)
2604 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2605 TLI);
2606 }
2607 break;
2608 case TargetLowering::Custom:
2609 Result = TLI.LowerOperation(Result, DAG);
2610 break;
2611 case Expand:
2612 // TRUNCSTORE:i16 i32 -> STORE i16
2613 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2614 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2615 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2616 isVolatile, Alignment);
2617 break;
2618 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 }
2620 }
2621 break;
2622 }
2623 case ISD::PCMARKER:
2624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2625 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2626 break;
2627 case ISD::STACKSAVE:
2628 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2629 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2630 Tmp1 = Result.getValue(0);
2631 Tmp2 = Result.getValue(1);
2632
2633 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2634 default: assert(0 && "This action is not supported yet!");
2635 case TargetLowering::Legal: break;
2636 case TargetLowering::Custom:
2637 Tmp3 = TLI.LowerOperation(Result, DAG);
2638 if (Tmp3.Val) {
2639 Tmp1 = LegalizeOp(Tmp3);
2640 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2641 }
2642 break;
2643 case TargetLowering::Expand:
2644 // Expand to CopyFromReg if the target set
2645 // StackPointerRegisterToSaveRestore.
2646 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2647 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2648 Node->getValueType(0));
2649 Tmp2 = Tmp1.getValue(1);
2650 } else {
2651 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2652 Tmp2 = Node->getOperand(0);
2653 }
2654 break;
2655 }
2656
2657 // Since stacksave produce two values, make sure to remember that we
2658 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002659 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2660 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661 return Op.ResNo ? Tmp2 : Tmp1;
2662
2663 case ISD::STACKRESTORE:
2664 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2665 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2666 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2667
2668 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2669 default: assert(0 && "This action is not supported yet!");
2670 case TargetLowering::Legal: break;
2671 case TargetLowering::Custom:
2672 Tmp1 = TLI.LowerOperation(Result, DAG);
2673 if (Tmp1.Val) Result = Tmp1;
2674 break;
2675 case TargetLowering::Expand:
2676 // Expand to CopyToReg if the target set
2677 // StackPointerRegisterToSaveRestore.
2678 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2679 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2680 } else {
2681 Result = Tmp1;
2682 }
2683 break;
2684 }
2685 break;
2686
2687 case ISD::READCYCLECOUNTER:
2688 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2689 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2690 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2691 Node->getValueType(0))) {
2692 default: assert(0 && "This action is not supported yet!");
2693 case TargetLowering::Legal:
2694 Tmp1 = Result.getValue(0);
2695 Tmp2 = Result.getValue(1);
2696 break;
2697 case TargetLowering::Custom:
2698 Result = TLI.LowerOperation(Result, DAG);
2699 Tmp1 = LegalizeOp(Result.getValue(0));
2700 Tmp2 = LegalizeOp(Result.getValue(1));
2701 break;
2702 }
2703
2704 // Since rdcc produce two values, make sure to remember that we legalized
2705 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00002706 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2707 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708 return Result;
2709
2710 case ISD::SELECT:
2711 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2712 case Expand: assert(0 && "It's impossible to expand bools");
2713 case Legal:
2714 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2715 break;
Dan Gohman07961cd2008-02-25 21:11:39 +00002716 case Promote: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2718 // Make sure the condition is either zero or one.
Dan Gohman07961cd2008-02-25 21:11:39 +00002719 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman07961cd2008-02-25 21:11:39 +00002721 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2723 break;
2724 }
Dan Gohman07961cd2008-02-25 21:11:39 +00002725 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2727 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2728
2729 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2730
2731 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2732 default: assert(0 && "This action is not supported yet!");
2733 case TargetLowering::Legal: break;
2734 case TargetLowering::Custom: {
2735 Tmp1 = TLI.LowerOperation(Result, DAG);
2736 if (Tmp1.Val) Result = Tmp1;
2737 break;
2738 }
2739 case TargetLowering::Expand:
2740 if (Tmp1.getOpcode() == ISD::SETCC) {
2741 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2742 Tmp2, Tmp3,
2743 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2744 } else {
2745 Result = DAG.getSelectCC(Tmp1,
2746 DAG.getConstant(0, Tmp1.getValueType()),
2747 Tmp2, Tmp3, ISD::SETNE);
2748 }
2749 break;
2750 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00002751 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002752 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2753 unsigned ExtOp, TruncOp;
Duncan Sands92c43912008-06-06 12:08:01 +00002754 if (Tmp2.getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755 ExtOp = ISD::BIT_CONVERT;
2756 TruncOp = ISD::BIT_CONVERT;
Duncan Sands92c43912008-06-06 12:08:01 +00002757 } else if (Tmp2.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002758 ExtOp = ISD::ANY_EXTEND;
2759 TruncOp = ISD::TRUNCATE;
2760 } else {
2761 ExtOp = ISD::FP_EXTEND;
2762 TruncOp = ISD::FP_ROUND;
2763 }
2764 // Promote each of the values to the new type.
2765 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2766 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2767 // Perform the larger operation, then round down.
2768 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner5872a362008-01-17 07:00:52 +00002769 if (TruncOp != ISD::FP_ROUND)
2770 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2771 else
2772 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2773 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 break;
2775 }
2776 }
2777 break;
2778 case ISD::SELECT_CC: {
2779 Tmp1 = Node->getOperand(0); // LHS
2780 Tmp2 = Node->getOperand(1); // RHS
2781 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2782 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman8181bd12008-07-27 21:46:04 +00002783 SDValue CC = Node->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784
2785 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2786
2787 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2788 // the LHS is a legal SETCC itself. In this case, we need to compare
2789 // the result against zero to select between true and false values.
2790 if (Tmp2.Val == 0) {
2791 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2792 CC = DAG.getCondCode(ISD::SETNE);
2793 }
2794 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2795
2796 // Everything is legal, see if we should expand this op or something.
2797 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2798 default: assert(0 && "This action is not supported yet!");
2799 case TargetLowering::Legal: break;
2800 case TargetLowering::Custom:
2801 Tmp1 = TLI.LowerOperation(Result, DAG);
2802 if (Tmp1.Val) Result = Tmp1;
2803 break;
2804 }
2805 break;
2806 }
2807 case ISD::SETCC:
2808 Tmp1 = Node->getOperand(0);
2809 Tmp2 = Node->getOperand(1);
2810 Tmp3 = Node->getOperand(2);
2811 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2812
2813 // If we had to Expand the SetCC operands into a SELECT node, then it may
2814 // not always be possible to return a true LHS & RHS. In this case, just
2815 // return the value we legalized, returned in the LHS
2816 if (Tmp2.Val == 0) {
2817 Result = Tmp1;
2818 break;
2819 }
2820
2821 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2822 default: assert(0 && "Cannot handle this action for SETCC yet!");
2823 case TargetLowering::Custom:
2824 isCustom = true;
2825 // FALLTHROUGH.
2826 case TargetLowering::Legal:
2827 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2828 if (isCustom) {
2829 Tmp4 = TLI.LowerOperation(Result, DAG);
2830 if (Tmp4.Val) Result = Tmp4;
2831 }
2832 break;
2833 case TargetLowering::Promote: {
2834 // First step, figure out the appropriate operation to use.
2835 // Allow SETCC to not be supported for all legal data types
2836 // Mostly this targets FP
Duncan Sands92c43912008-06-06 12:08:01 +00002837 MVT NewInTy = Node->getOperand(0).getValueType();
2838 MVT OldVT = NewInTy; OldVT = OldVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839
2840 // Scan for the appropriate larger type to use.
2841 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002842 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843
Duncan Sands92c43912008-06-06 12:08:01 +00002844 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 "Fell off of the edge of the integer world");
Duncan Sands92c43912008-06-06 12:08:01 +00002846 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 "Fell off of the edge of the floating point world");
2848
2849 // If the target supports SETCC of this type, use it.
2850 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2851 break;
2852 }
Duncan Sands92c43912008-06-06 12:08:01 +00002853 if (NewInTy.isInteger())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 assert(0 && "Cannot promote Legal Integer SETCC yet");
2855 else {
2856 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2857 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2858 }
2859 Tmp1 = LegalizeOp(Tmp1);
2860 Tmp2 = LegalizeOp(Tmp2);
2861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2862 Result = LegalizeOp(Result);
2863 break;
2864 }
2865 case TargetLowering::Expand:
2866 // Expand a setcc node into a select_cc of the same condition, lhs, and
2867 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands92c43912008-06-06 12:08:01 +00002868 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2870 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2871 Tmp3);
2872 break;
2873 }
2874 break;
Nate Begeman9a1ce152008-05-12 19:40:03 +00002875 case ISD::VSETCC: {
2876 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2877 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman8181bd12008-07-27 21:46:04 +00002878 SDValue CC = Node->getOperand(2);
Nate Begeman9a1ce152008-05-12 19:40:03 +00002879
2880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
2881
2882 // Everything is legal, see if we should expand this op or something.
2883 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
2884 default: assert(0 && "This action is not supported yet!");
2885 case TargetLowering::Legal: break;
2886 case TargetLowering::Custom:
2887 Tmp1 = TLI.LowerOperation(Result, DAG);
2888 if (Tmp1.Val) Result = Tmp1;
2889 break;
2890 }
2891 break;
2892 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893
2894 case ISD::SHL_PARTS:
2895 case ISD::SRA_PARTS:
2896 case ISD::SRL_PARTS: {
Dan Gohman8181bd12008-07-27 21:46:04 +00002897 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002898 bool Changed = false;
2899 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2900 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2901 Changed |= Ops.back() != Node->getOperand(i);
2902 }
2903 if (Changed)
2904 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2905
2906 switch (TLI.getOperationAction(Node->getOpcode(),
2907 Node->getValueType(0))) {
2908 default: assert(0 && "This action is not supported yet!");
2909 case TargetLowering::Legal: break;
2910 case TargetLowering::Custom:
2911 Tmp1 = TLI.LowerOperation(Result, DAG);
2912 if (Tmp1.Val) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SDValue Tmp2, RetVal(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2915 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 if (i == Op.ResNo)
2918 RetVal = Tmp2;
2919 }
2920 assert(RetVal.Val && "Illegal result number");
2921 return RetVal;
2922 }
2923 break;
2924 }
2925
2926 // Since these produce multiple values, make sure to remember that we
2927 // legalized all of them.
2928 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +00002929 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002930 return Result.getValue(Op.ResNo);
2931 }
2932
2933 // Binary operators
2934 case ISD::ADD:
2935 case ISD::SUB:
2936 case ISD::MUL:
2937 case ISD::MULHS:
2938 case ISD::MULHU:
2939 case ISD::UDIV:
2940 case ISD::SDIV:
2941 case ISD::AND:
2942 case ISD::OR:
2943 case ISD::XOR:
2944 case ISD::SHL:
2945 case ISD::SRL:
2946 case ISD::SRA:
2947 case ISD::FADD:
2948 case ISD::FSUB:
2949 case ISD::FMUL:
2950 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002951 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2953 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2954 case Expand: assert(0 && "Not possible");
2955 case Legal:
2956 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2957 break;
2958 case Promote:
2959 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2960 break;
2961 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00002962
2963 if ((Node->getOpcode() == ISD::SHL ||
2964 Node->getOpcode() == ISD::SRL ||
2965 Node->getOpcode() == ISD::SRA) &&
2966 !Node->getValueType(0).isVector()) {
2967 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
2968 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
2969 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
2970 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
2971 }
2972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2974
2975 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2976 default: assert(0 && "BinOp legalize operation not supported");
2977 case TargetLowering::Legal: break;
2978 case TargetLowering::Custom:
2979 Tmp1 = TLI.LowerOperation(Result, DAG);
Nate Begeman7569e762008-07-29 19:07:27 +00002980 if (Tmp1.Val) {
Nate Begemanbb1ce942008-07-29 15:49:41 +00002981 Result = Tmp1;
2982 break;
Nate Begeman7569e762008-07-29 19:07:27 +00002983 }
Nate Begemanbb1ce942008-07-29 15:49:41 +00002984 // Fall through if the custom lower can't deal with the operation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002985 case TargetLowering::Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00002986 MVT VT = Op.getValueType();
Dan Gohman5a199552007-10-08 18:33:35 +00002987
2988 // See if multiply or divide can be lowered using two-result operations.
2989 SDVTList VTs = DAG.getVTList(VT, VT);
2990 if (Node->getOpcode() == ISD::MUL) {
2991 // We just need the low half of the multiply; try both the signed
2992 // and unsigned forms. If the target supports both SMUL_LOHI and
2993 // UMUL_LOHI, form a preference by checking which forms of plain
2994 // MULH it supports.
2995 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2996 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2997 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2998 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2999 unsigned OpToUse = 0;
3000 if (HasSMUL_LOHI && !HasMULHS) {
3001 OpToUse = ISD::SMUL_LOHI;
3002 } else if (HasUMUL_LOHI && !HasMULHU) {
3003 OpToUse = ISD::UMUL_LOHI;
3004 } else if (HasSMUL_LOHI) {
3005 OpToUse = ISD::SMUL_LOHI;
3006 } else if (HasUMUL_LOHI) {
3007 OpToUse = ISD::UMUL_LOHI;
3008 }
3009 if (OpToUse) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003010 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003011 break;
3012 }
3013 }
3014 if (Node->getOpcode() == ISD::MULHS &&
3015 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003016 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003017 break;
3018 }
3019 if (Node->getOpcode() == ISD::MULHU &&
3020 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003021 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003022 break;
3023 }
3024 if (Node->getOpcode() == ISD::SDIV &&
3025 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003026 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003027 break;
3028 }
3029 if (Node->getOpcode() == ISD::UDIV &&
3030 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003031 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohman5a199552007-10-08 18:33:35 +00003032 break;
3033 }
3034
Dan Gohman6d05cac2007-10-11 23:57:53 +00003035 // Check to see if we have a libcall for this operator.
3036 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3037 bool isSigned = false;
3038 switch (Node->getOpcode()) {
3039 case ISD::UDIV:
3040 case ISD::SDIV:
3041 if (VT == MVT::i32) {
3042 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00003044 isSigned = Node->getOpcode() == ISD::SDIV;
3045 }
3046 break;
3047 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00003048 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3049 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003050 break;
3051 default: break;
3052 }
3053 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003054 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003055 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 break;
3057 }
3058
Duncan Sands92c43912008-06-06 12:08:01 +00003059 assert(Node->getValueType(0).isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060 "Cannot expand this binary operator!");
3061 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00003062 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003063 break;
3064 }
3065 case TargetLowering::Promote: {
3066 switch (Node->getOpcode()) {
3067 default: assert(0 && "Do not know how to promote this BinOp!");
3068 case ISD::AND:
3069 case ISD::OR:
3070 case ISD::XOR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003071 MVT OVT = Node->getValueType(0);
3072 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3073 assert(OVT.isVector() && "Cannot promote this BinOp!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 // Bit convert each of the values to the new type.
3075 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3076 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3077 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3078 // Bit convert the result back the original type.
3079 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3080 break;
3081 }
3082 }
3083 }
3084 }
3085 break;
3086
Dan Gohman475cd732007-10-05 14:17:22 +00003087 case ISD::SMUL_LOHI:
3088 case ISD::UMUL_LOHI:
3089 case ISD::SDIVREM:
3090 case ISD::UDIVREM:
3091 // These nodes will only be produced by target-specific lowering, so
3092 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003093 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00003094 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00003095
3096 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3097 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00003099 break;
3100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3102 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3103 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3104 case Expand: assert(0 && "Not possible");
3105 case Legal:
3106 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3107 break;
3108 case Promote:
3109 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3110 break;
3111 }
3112
3113 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3114
3115 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3116 default: assert(0 && "Operation not supported");
3117 case TargetLowering::Custom:
3118 Tmp1 = TLI.LowerOperation(Result, DAG);
3119 if (Tmp1.Val) Result = Tmp1;
3120 break;
3121 case TargetLowering::Legal: break;
3122 case TargetLowering::Expand: {
3123 // If this target supports fabs/fneg natively and select is cheap,
3124 // do this efficiently.
3125 if (!TLI.isSelectExpensive() &&
3126 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3127 TargetLowering::Legal &&
3128 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
3129 TargetLowering::Legal) {
3130 // Get the sign bit of the RHS.
Duncan Sands92c43912008-06-06 12:08:01 +00003131 MVT IVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman8181bd12008-07-27 21:46:04 +00003133 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00003134 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3136 // Get the absolute value of the result.
Dan Gohman8181bd12008-07-27 21:46:04 +00003137 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138 // Select between the nabs and abs value based on the sign bit of
3139 // the input.
3140 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3141 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3142 AbsVal),
3143 AbsVal);
3144 Result = LegalizeOp(Result);
3145 break;
3146 }
3147
3148 // Otherwise, do bitwise ops!
Duncan Sands92c43912008-06-06 12:08:01 +00003149 MVT NVT =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003150 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3151 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3152 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3153 Result = LegalizeOp(Result);
3154 break;
3155 }
3156 }
3157 break;
3158
3159 case ISD::ADDC:
3160 case ISD::SUBC:
3161 Tmp1 = LegalizeOp(Node->getOperand(0));
3162 Tmp2 = LegalizeOp(Node->getOperand(1));
3163 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3164 // Since this produces two values, make sure to remember that we legalized
3165 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003166 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3167 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 return Result;
3169
3170 case ISD::ADDE:
3171 case ISD::SUBE:
3172 Tmp1 = LegalizeOp(Node->getOperand(0));
3173 Tmp2 = LegalizeOp(Node->getOperand(1));
3174 Tmp3 = LegalizeOp(Node->getOperand(2));
3175 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3176 // Since this produces two values, make sure to remember that we legalized
3177 // both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003178 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3179 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 return Result;
3181
3182 case ISD::BUILD_PAIR: {
Duncan Sands92c43912008-06-06 12:08:01 +00003183 MVT PairTy = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 // TODO: handle the case where the Lo and Hi operands are not of legal type
3185 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3186 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3187 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
3188 case TargetLowering::Promote:
3189 case TargetLowering::Custom:
3190 assert(0 && "Cannot promote/custom this yet!");
3191 case TargetLowering::Legal:
3192 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3193 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3194 break;
3195 case TargetLowering::Expand:
3196 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3197 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3198 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003199 DAG.getConstant(PairTy.getSizeInBits()/2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 TLI.getShiftAmountTy()));
3201 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
3202 break;
3203 }
3204 break;
3205 }
3206
3207 case ISD::UREM:
3208 case ISD::SREM:
3209 case ISD::FREM:
3210 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3211 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3212
3213 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3214 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3215 case TargetLowering::Custom:
3216 isCustom = true;
3217 // FALLTHROUGH
3218 case TargetLowering::Legal:
3219 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3220 if (isCustom) {
3221 Tmp1 = TLI.LowerOperation(Result, DAG);
3222 if (Tmp1.Val) Result = Tmp1;
3223 }
3224 break;
Dan Gohman5a199552007-10-08 18:33:35 +00003225 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
3227 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands92c43912008-06-06 12:08:01 +00003228 MVT VT = Node->getValueType(0);
Dan Gohman5a199552007-10-08 18:33:35 +00003229
3230 // See if remainder can be lowered using two-result operations.
3231 SDVTList VTs = DAG.getVTList(VT, VT);
3232 if (Node->getOpcode() == ISD::SREM &&
3233 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003234 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003235 break;
3236 }
3237 if (Node->getOpcode() == ISD::UREM &&
3238 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003239 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00003240 break;
3241 }
3242
Duncan Sands92c43912008-06-06 12:08:01 +00003243 if (VT.isInteger()) {
Dan Gohman5a199552007-10-08 18:33:35 +00003244 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245 TargetLowering::Legal) {
3246 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
3248 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3249 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands92c43912008-06-06 12:08:01 +00003250 } else if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003251 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00003253 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 "Cannot expand this binary operator!");
3255 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3256 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman8181bd12008-07-27 21:46:04 +00003257 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003258 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003260 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003261 assert(VT.isFloatingPoint() &&
Dan Gohman59b4b102007-11-06 22:11:54 +00003262 "remainder op must have integer or floating-point type");
Duncan Sands92c43912008-06-06 12:08:01 +00003263 if (VT.isVector()) {
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003264 Result = LegalizeOp(UnrollVectorOp(Op));
3265 } else {
3266 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003267 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3268 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003270 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 }
3273 break;
3274 }
Dan Gohman5a199552007-10-08 18:33:35 +00003275 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276 break;
3277 case ISD::VAARG: {
3278 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3279 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3280
Duncan Sands92c43912008-06-06 12:08:01 +00003281 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3283 default: assert(0 && "This action is not supported yet!");
3284 case TargetLowering::Custom:
3285 isCustom = true;
3286 // FALLTHROUGH
3287 case TargetLowering::Legal:
3288 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3289 Result = Result.getValue(0);
3290 Tmp1 = Result.getValue(1);
3291
3292 if (isCustom) {
3293 Tmp2 = TLI.LowerOperation(Result, DAG);
3294 if (Tmp2.Val) {
3295 Result = LegalizeOp(Tmp2);
3296 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3297 }
3298 }
3299 break;
3300 case TargetLowering::Expand: {
Dan Gohman12a9c082008-02-06 22:27:42 +00003301 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003302 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003303 // Increment the pointer, VAList, to the next vaarg
3304 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00003305 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 TLI.getPointerTy()));
3307 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00003308 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 // Load the actual argument out of the pointer VAList
3310 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3311 Tmp1 = LegalizeOp(Result.getValue(1));
3312 Result = LegalizeOp(Result);
3313 break;
3314 }
3315 }
3316 // Since VAARG produces two values, make sure to remember that we
3317 // legalized both of them.
Dan Gohman8181bd12008-07-27 21:46:04 +00003318 AddLegalizedOperand(SDValue(Node, 0), Result);
3319 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 return Op.ResNo ? Tmp1 : Result;
3321 }
3322
3323 case ISD::VACOPY:
3324 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3325 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3326 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3327
3328 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3329 default: assert(0 && "This action is not supported yet!");
3330 case TargetLowering::Custom:
3331 isCustom = true;
3332 // FALLTHROUGH
3333 case TargetLowering::Legal:
3334 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3335 Node->getOperand(3), Node->getOperand(4));
3336 if (isCustom) {
3337 Tmp1 = TLI.LowerOperation(Result, DAG);
3338 if (Tmp1.Val) Result = Tmp1;
3339 }
3340 break;
3341 case TargetLowering::Expand:
3342 // This defaults to loading a pointer from the input and storing it to the
3343 // output, returning the chain.
Dan Gohman12a9c082008-02-06 22:27:42 +00003344 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3345 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman6b9a08e2008-04-17 02:09:26 +00003346 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3347 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 break;
3349 }
3350 break;
3351
3352 case ISD::VAEND:
3353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3354 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3355
3356 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3357 default: assert(0 && "This action is not supported yet!");
3358 case TargetLowering::Custom:
3359 isCustom = true;
3360 // FALLTHROUGH
3361 case TargetLowering::Legal:
3362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3363 if (isCustom) {
3364 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3365 if (Tmp1.Val) Result = Tmp1;
3366 }
3367 break;
3368 case TargetLowering::Expand:
3369 Result = Tmp1; // Default to a no-op, return the chain
3370 break;
3371 }
3372 break;
3373
3374 case ISD::VASTART:
3375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3377
3378 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3379
3380 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3381 default: assert(0 && "This action is not supported yet!");
3382 case TargetLowering::Legal: break;
3383 case TargetLowering::Custom:
3384 Tmp1 = TLI.LowerOperation(Result, DAG);
3385 if (Tmp1.Val) Result = Tmp1;
3386 break;
3387 }
3388 break;
3389
3390 case ISD::ROTL:
3391 case ISD::ROTR:
3392 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3393 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3394 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3395 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3396 default:
3397 assert(0 && "ROTL/ROTR legalize operation not supported");
3398 break;
3399 case TargetLowering::Legal:
3400 break;
3401 case TargetLowering::Custom:
3402 Tmp1 = TLI.LowerOperation(Result, DAG);
3403 if (Tmp1.Val) Result = Tmp1;
3404 break;
3405 case TargetLowering::Promote:
3406 assert(0 && "Do not know how to promote ROTL/ROTR");
3407 break;
3408 case TargetLowering::Expand:
3409 assert(0 && "Do not know how to expand ROTL/ROTR");
3410 break;
3411 }
3412 break;
3413
3414 case ISD::BSWAP:
3415 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3416 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3417 case TargetLowering::Custom:
3418 assert(0 && "Cannot custom legalize this yet!");
3419 case TargetLowering::Legal:
3420 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3421 break;
3422 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003423 MVT OVT = Tmp1.getValueType();
3424 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3425 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003426
3427 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3428 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3429 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3430 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3431 break;
3432 }
3433 case TargetLowering::Expand:
3434 Result = ExpandBSWAP(Tmp1);
3435 break;
3436 }
3437 break;
3438
3439 case ISD::CTPOP:
3440 case ISD::CTTZ:
3441 case ISD::CTLZ:
3442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3443 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003444 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003445 case TargetLowering::Legal:
3446 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003447 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003448 TargetLowering::Custom) {
3449 Tmp1 = TLI.LowerOperation(Result, DAG);
3450 if (Tmp1.Val) {
3451 Result = Tmp1;
3452 }
Scott Michel48b63e62007-07-30 21:00:31 +00003453 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 break;
3455 case TargetLowering::Promote: {
Duncan Sands92c43912008-06-06 12:08:01 +00003456 MVT OVT = Tmp1.getValueType();
3457 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458
3459 // Zero extend the argument.
3460 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3461 // Perform the larger operation, then subtract if needed.
3462 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3463 switch (Node->getOpcode()) {
3464 case ISD::CTPOP:
3465 Result = Tmp1;
3466 break;
3467 case ISD::CTTZ:
3468 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00003469 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003470 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471 ISD::SETEQ);
3472 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00003473 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003474 break;
3475 case ISD::CTLZ:
3476 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3477 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00003478 DAG.getConstant(NVT.getSizeInBits() -
3479 OVT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480 break;
3481 }
3482 break;
3483 }
3484 case TargetLowering::Expand:
3485 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3486 break;
3487 }
3488 break;
3489
3490 // Unary operators
3491 case ISD::FABS:
3492 case ISD::FNEG:
3493 case ISD::FSQRT:
3494 case ISD::FSIN:
3495 case ISD::FCOS:
3496 Tmp1 = LegalizeOp(Node->getOperand(0));
3497 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3498 case TargetLowering::Promote:
3499 case TargetLowering::Custom:
3500 isCustom = true;
3501 // FALLTHROUGH
3502 case TargetLowering::Legal:
3503 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3504 if (isCustom) {
3505 Tmp1 = TLI.LowerOperation(Result, DAG);
3506 if (Tmp1.Val) Result = Tmp1;
3507 }
3508 break;
3509 case TargetLowering::Expand:
3510 switch (Node->getOpcode()) {
3511 default: assert(0 && "Unreachable!");
3512 case ISD::FNEG:
3513 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3514 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3515 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3516 break;
3517 case ISD::FABS: {
3518 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands92c43912008-06-06 12:08:01 +00003519 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003521 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003522 ISD::SETUGT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3524 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3525 break;
3526 }
3527 case ISD::FSQRT:
3528 case ISD::FSIN:
3529 case ISD::FCOS: {
Duncan Sands92c43912008-06-06 12:08:01 +00003530 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003531
3532 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003533 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003534 Result = LegalizeOp(UnrollVectorOp(Op));
3535 break;
3536 }
3537
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3539 switch(Node->getOpcode()) {
3540 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003541 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3542 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003543 break;
3544 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003545 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3546 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003547 break;
3548 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003549 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3550 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003551 break;
3552 default: assert(0 && "Unreachable!");
3553 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003554 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003555 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 break;
3557 }
3558 }
3559 break;
3560 }
3561 break;
3562 case ISD::FPOWI: {
Duncan Sands92c43912008-06-06 12:08:01 +00003563 MVT VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003564
3565 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands92c43912008-06-06 12:08:01 +00003566 if (VT.isVector()) {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003567 Result = LegalizeOp(UnrollVectorOp(Op));
3568 break;
3569 }
3570
3571 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003572 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3573 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman8181bd12008-07-27 21:46:04 +00003574 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003575 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003576 break;
3577 }
3578 case ISD::BIT_CONVERT:
3579 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003580 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3581 Node->getValueType(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003582 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583 // The input has to be a vector type, we have to either scalarize it, pack
3584 // it, or convert it based on whether the input vector type is legal.
3585 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003586 int InIx = Node->getOperand(0).ResNo;
Duncan Sands92c43912008-06-06 12:08:01 +00003587 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3588 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589
3590 // Figure out if there is a simple type corresponding to this Vector
3591 // type. If so, convert to the vector type.
Duncan Sands92c43912008-06-06 12:08:01 +00003592 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003593 if (TLI.isTypeLegal(TVT)) {
3594 // Turn this into a bit convert of the vector input.
3595 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3596 LegalizeOp(Node->getOperand(0)));
3597 break;
3598 } else if (NumElems == 1) {
3599 // Turn this into a bit convert of the scalar input.
3600 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3601 ScalarizeVectorOp(Node->getOperand(0)));
3602 break;
3603 } else {
3604 // FIXME: UNIMP! Store then reload
3605 assert(0 && "Cast from unsupported vector type not implemented yet!");
3606 }
3607 } else {
3608 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3609 Node->getOperand(0).getValueType())) {
3610 default: assert(0 && "Unknown operation action!");
3611 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003612 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3613 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614 break;
3615 case TargetLowering::Legal:
3616 Tmp1 = LegalizeOp(Node->getOperand(0));
3617 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3618 break;
3619 }
3620 }
3621 break;
3622
3623 // Conversion operators. The source and destination have different types.
3624 case ISD::SINT_TO_FP:
3625 case ISD::UINT_TO_FP: {
3626 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman29c3cef2008-08-14 20:04:46 +00003627 Result = LegalizeINT_TO_FP(Result, isSigned,
3628 Node->getValueType(0), Node->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003629 break;
3630 }
3631 case ISD::TRUNCATE:
3632 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3633 case Legal:
3634 Tmp1 = LegalizeOp(Node->getOperand(0));
3635 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3636 break;
3637 case Expand:
3638 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3639
3640 // Since the result is legal, we should just be able to truncate the low
3641 // part of the source.
3642 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3643 break;
3644 case Promote:
3645 Result = PromoteOp(Node->getOperand(0));
3646 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3647 break;
3648 }
3649 break;
3650
3651 case ISD::FP_TO_SINT:
3652 case ISD::FP_TO_UINT:
3653 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3654 case Legal:
3655 Tmp1 = LegalizeOp(Node->getOperand(0));
3656
3657 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3658 default: assert(0 && "Unknown operation action!");
3659 case TargetLowering::Custom:
3660 isCustom = true;
3661 // FALLTHROUGH
3662 case TargetLowering::Legal:
3663 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3664 if (isCustom) {
3665 Tmp1 = TLI.LowerOperation(Result, DAG);
3666 if (Tmp1.Val) Result = Tmp1;
3667 }
3668 break;
3669 case TargetLowering::Promote:
3670 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3671 Node->getOpcode() == ISD::FP_TO_SINT);
3672 break;
3673 case TargetLowering::Expand:
3674 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003675 SDValue True, False;
Duncan Sands92c43912008-06-06 12:08:01 +00003676 MVT VT = Node->getOperand(0).getValueType();
3677 MVT NVT = Node->getValueType(0);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003678 const uint64_t zero[] = {0, 0};
Duncan Sands92c43912008-06-06 12:08:01 +00003679 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3680 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman88ae8c52008-02-29 01:44:25 +00003681 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003682 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel502151f2008-03-10 15:42:14 +00003683 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003684 Node->getOperand(0), Tmp2, ISD::SETLT);
3685 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3686 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3687 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3688 Tmp2));
3689 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman88ae8c52008-02-29 01:44:25 +00003690 DAG.getConstant(x, NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003691 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3692 break;
3693 } else {
3694 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3695 }
3696 break;
3697 }
3698 break;
3699 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00003700 MVT VT = Op.getValueType();
3701 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003702 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003703 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner5872a362008-01-17 07:00:52 +00003704 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3705 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3706 Node->getOperand(0), DAG.getValueType(MVT::f64));
3707 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3708 DAG.getIntPtrConstant(1));
3709 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3710 } else {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003711 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3712 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3713 Tmp2 = DAG.getConstantFP(apf, OVT);
3714 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3715 // FIXME: generated code sucks.
3716 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3717 DAG.getNode(ISD::ADD, MVT::i32,
3718 DAG.getNode(ISD::FP_TO_SINT, VT,
3719 DAG.getNode(ISD::FSUB, OVT,
3720 Node->getOperand(0), Tmp2)),
3721 DAG.getConstant(0x80000000, MVT::i32)),
3722 DAG.getNode(ISD::FP_TO_SINT, VT,
3723 Node->getOperand(0)),
3724 DAG.getCondCode(ISD::SETGE));
3725 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003726 break;
3727 }
Dan Gohmanec51f642008-03-10 23:03:31 +00003728 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsf68dffb2008-07-17 02:36:29 +00003729 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3730 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3731 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman8181bd12008-07-27 21:46:04 +00003732 SDValue Dummy;
Duncan Sandsf1db7c82008-04-12 17:14:18 +00003733 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003734 break;
3735 }
3736 case Promote:
3737 Tmp1 = PromoteOp(Node->getOperand(0));
3738 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3739 Result = LegalizeOp(Result);
3740 break;
3741 }
3742 break;
3743
Chris Lattner56ecde32008-01-16 06:57:07 +00003744 case ISD::FP_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00003745 MVT DstVT = Op.getValueType();
3746 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00003747 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3748 // The only other way we can lower this is to turn it into a STORE,
3749 // LOAD pair, targetting a temporary location (a stack slot).
3750 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3751 break;
Chris Lattner56ecde32008-01-16 06:57:07 +00003752 }
3753 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3754 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3755 case Legal:
3756 Tmp1 = LegalizeOp(Node->getOperand(0));
3757 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3758 break;
3759 case Promote:
3760 Tmp1 = PromoteOp(Node->getOperand(0));
3761 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3762 break;
3763 }
3764 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003765 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003766 case ISD::FP_ROUND: {
Duncan Sands92c43912008-06-06 12:08:01 +00003767 MVT DstVT = Op.getValueType();
3768 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner5872a362008-01-17 07:00:52 +00003769 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3770 if (SrcVT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003771 SDValue Lo;
Dale Johannesena0d36082008-01-20 01:18:38 +00003772 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner5872a362008-01-17 07:00:52 +00003773 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesena0d36082008-01-20 01:18:38 +00003774 if (DstVT!=MVT::f64)
3775 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner5872a362008-01-17 07:00:52 +00003776 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003777 }
Chris Lattner5872a362008-01-17 07:00:52 +00003778 // The only other way we can lower this is to turn it into a STORE,
3779 // LOAD pair, targetting a temporary location (a stack slot).
3780 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3781 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003782 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003783 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3784 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3785 case Legal:
3786 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003787 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003788 break;
3789 case Promote:
3790 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner5872a362008-01-17 07:00:52 +00003791 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3792 Node->getOperand(1));
Chris Lattner56ecde32008-01-16 06:57:07 +00003793 break;
3794 }
3795 break;
Chris Lattner5872a362008-01-17 07:00:52 +00003796 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003797 case ISD::ANY_EXTEND:
3798 case ISD::ZERO_EXTEND:
3799 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003800 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3801 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3802 case Legal:
3803 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelac54d002008-04-30 00:26:38 +00003804 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michelac7091c2008-02-15 23:05:48 +00003805 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3806 TargetLowering::Custom) {
Scott Michelac54d002008-04-30 00:26:38 +00003807 Tmp1 = TLI.LowerOperation(Result, DAG);
3808 if (Tmp1.Val) Result = Tmp1;
Scott Michelac7091c2008-02-15 23:05:48 +00003809 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 break;
3811 case Promote:
3812 switch (Node->getOpcode()) {
3813 case ISD::ANY_EXTEND:
3814 Tmp1 = PromoteOp(Node->getOperand(0));
3815 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3816 break;
3817 case ISD::ZERO_EXTEND:
3818 Result = PromoteOp(Node->getOperand(0));
3819 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3820 Result = DAG.getZeroExtendInReg(Result,
3821 Node->getOperand(0).getValueType());
3822 break;
3823 case ISD::SIGN_EXTEND:
3824 Result = PromoteOp(Node->getOperand(0));
3825 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3826 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3827 Result,
3828 DAG.getValueType(Node->getOperand(0).getValueType()));
3829 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 }
3831 }
3832 break;
3833 case ISD::FP_ROUND_INREG:
3834 case ISD::SIGN_EXTEND_INREG: {
3835 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00003836 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837
3838 // If this operation is not supported, convert it to a shl/shr or load/store
3839 // pair.
3840 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3841 default: assert(0 && "This action not supported for this op yet!");
3842 case TargetLowering::Legal:
3843 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3844 break;
3845 case TargetLowering::Expand:
3846 // If this is an integer extend and shifts are supported, do that.
3847 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3848 // NOTE: we could fall back on load/store here too for targets without
3849 // SAR. However, it is doubtful that any exist.
Duncan Sands92c43912008-06-06 12:08:01 +00003850 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3851 ExtraVT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00003852 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3854 Node->getOperand(0), ShiftCst);
3855 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3856 Result, ShiftCst);
3857 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3858 // The only way we can lower this is to turn it into a TRUNCSTORE,
3859 // EXTLOAD pair, targetting a temporary location (a stack slot).
3860
3861 // NOTE: there is a choice here between constantly creating new stack
3862 // slots and always reusing the same one. We currently always create
3863 // new ones, as reuse may inhibit scheduling.
Chris Lattner59370bd2008-01-16 07:51:34 +00003864 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3865 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003866 } else {
3867 assert(0 && "Unknown op");
3868 }
3869 break;
3870 }
3871 break;
3872 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003873 case ISD::TRAMPOLINE: {
Dan Gohman8181bd12008-07-27 21:46:04 +00003874 SDValue Ops[6];
Duncan Sands38947cd2007-07-27 12:58:54 +00003875 for (unsigned i = 0; i != 6; ++i)
3876 Ops[i] = LegalizeOp(Node->getOperand(i));
3877 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3878 // The only option for this node is to custom lower it.
3879 Result = TLI.LowerOperation(Result, DAG);
3880 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003881
3882 // Since trampoline produces two values, make sure to remember that we
3883 // legalized both of them.
3884 Tmp1 = LegalizeOp(Result.getValue(1));
3885 Result = LegalizeOp(Result);
Dan Gohman8181bd12008-07-27 21:46:04 +00003886 AddLegalizedOperand(SDValue(Node, 0), Result);
3887 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Duncan Sands7407a9f2007-09-11 14:10:23 +00003888 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003889 }
Dan Gohmane8e4a412008-05-14 00:43:10 +00003890 case ISD::FLT_ROUNDS_: {
Duncan Sands92c43912008-06-06 12:08:01 +00003891 MVT VT = Node->getValueType(0);
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003892 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3893 default: assert(0 && "This action not supported for this op yet!");
3894 case TargetLowering::Custom:
3895 Result = TLI.LowerOperation(Op, DAG);
3896 if (Result.Val) break;
3897 // Fall Thru
3898 case TargetLowering::Legal:
3899 // If this operation is not supported, lower it to constant 1
3900 Result = DAG.getConstant(1, VT);
3901 break;
3902 }
Dan Gohmane09dc8c2008-05-12 16:07:15 +00003903 break;
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003904 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003905 case ISD::TRAP: {
Duncan Sands92c43912008-06-06 12:08:01 +00003906 MVT VT = Node->getValueType(0);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003907 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3908 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00003909 case TargetLowering::Legal:
3910 Tmp1 = LegalizeOp(Node->getOperand(0));
3911 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3912 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003913 case TargetLowering::Custom:
3914 Result = TLI.LowerOperation(Op, DAG);
3915 if (Result.Val) break;
3916 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00003917 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003918 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00003919 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003920 TargetLowering::ArgListTy Args;
Dan Gohman8181bd12008-07-27 21:46:04 +00003921 std::pair<SDValue,SDValue> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00003922 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3923 false, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00003924 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3925 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003926 Result = CallResult.second;
3927 break;
3928 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003929 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003930 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 }
3932
3933 assert(Result.getValueType() == Op.getValueType() &&
3934 "Bad legalization!");
3935
3936 // Make sure that the generated code is itself legal.
3937 if (Result != Op)
3938 Result = LegalizeOp(Result);
3939
3940 // Note that LegalizeOp may be reentered even from single-use nodes, which
3941 // means that we always must cache transformed nodes.
3942 AddLegalizedOperand(Op, Result);
3943 return Result;
3944}
3945
3946/// PromoteOp - Given an operation that produces a value in an invalid type,
3947/// promote it to compute the value into a larger type. The produced value will
3948/// have the correct bits for the low portion of the register, but no guarantee
3949/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman8181bd12008-07-27 21:46:04 +00003950SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00003951 MVT VT = Op.getValueType();
3952 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953 assert(getTypeAction(VT) == Promote &&
3954 "Caller should expand or legalize operands that are not promotable!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00003955 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 "Cannot promote to smaller type!");
3957
Dan Gohman8181bd12008-07-27 21:46:04 +00003958 SDValue Tmp1, Tmp2, Tmp3;
3959 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 SDNode *Node = Op.Val;
3961
Dan Gohman8181bd12008-07-27 21:46:04 +00003962 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003963 if (I != PromotedNodes.end()) return I->second;
3964
3965 switch (Node->getOpcode()) {
3966 case ISD::CopyFromReg:
3967 assert(0 && "CopyFromReg must be legal!");
3968 default:
3969#ifndef NDEBUG
3970 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
3971#endif
3972 assert(0 && "Do not know how to promote this operator!");
3973 abort();
3974 case ISD::UNDEF:
3975 Result = DAG.getNode(ISD::UNDEF, NVT);
3976 break;
3977 case ISD::Constant:
3978 if (VT != MVT::i1)
3979 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3980 else
3981 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
3982 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3983 break;
3984 case ISD::ConstantFP:
3985 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3986 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3987 break;
3988
3989 case ISD::SETCC:
Scott Michel502151f2008-03-10 15:42:14 +00003990 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003991 && "SetCC type is not legal??");
Scott Michel502151f2008-03-10 15:42:14 +00003992 Result = DAG.getNode(ISD::SETCC,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00003993 TLI.getSetCCResultType(Node->getOperand(0)),
3994 Node->getOperand(0), Node->getOperand(1),
3995 Node->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 break;
3997
3998 case ISD::TRUNCATE:
3999 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4000 case Legal:
4001 Result = LegalizeOp(Node->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00004002 assert(Result.getValueType().bitsGE(NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003 "This truncation doesn't make sense!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00004004 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4006 break;
4007 case Promote:
4008 // The truncation is not required, because we don't guarantee anything
4009 // about high bits anyway.
4010 Result = PromoteOp(Node->getOperand(0));
4011 break;
4012 case Expand:
4013 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4014 // Truncate the low part of the expanded value to the result type
4015 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
4016 }
4017 break;
4018 case ISD::SIGN_EXTEND:
4019 case ISD::ZERO_EXTEND:
4020 case ISD::ANY_EXTEND:
4021 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4022 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4023 case Legal:
4024 // Input is legal? Just do extend all the way to the larger type.
4025 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4026 break;
4027 case Promote:
4028 // Promote the reg if it's smaller.
4029 Result = PromoteOp(Node->getOperand(0));
4030 // The high bits are not guaranteed to be anything. Insert an extend.
4031 if (Node->getOpcode() == ISD::SIGN_EXTEND)
4032 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4033 DAG.getValueType(Node->getOperand(0).getValueType()));
4034 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
4035 Result = DAG.getZeroExtendInReg(Result,
4036 Node->getOperand(0).getValueType());
4037 break;
4038 }
4039 break;
4040 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004041 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4042 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 Result = PromoteOp(Result);
4044 break;
4045
4046 case ISD::FP_EXTEND:
4047 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4048 case ISD::FP_ROUND:
4049 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4050 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4051 case Promote: assert(0 && "Unreachable with 2 FP types!");
4052 case Legal:
Chris Lattner5872a362008-01-17 07:00:52 +00004053 if (Node->getConstantOperandVal(1) == 0) {
4054 // Input is legal? Do an FP_ROUND_INREG.
4055 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4056 DAG.getValueType(VT));
4057 } else {
4058 // Just remove the truncate, it isn't affecting the value.
4059 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4060 Node->getOperand(1));
4061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 break;
4063 }
4064 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 case ISD::SINT_TO_FP:
4066 case ISD::UINT_TO_FP:
4067 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4068 case Legal:
4069 // No extra round required here.
4070 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
4071 break;
4072
4073 case Promote:
4074 Result = PromoteOp(Node->getOperand(0));
4075 if (Node->getOpcode() == ISD::SINT_TO_FP)
4076 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
4077 Result,
4078 DAG.getValueType(Node->getOperand(0).getValueType()));
4079 else
4080 Result = DAG.getZeroExtendInReg(Result,
4081 Node->getOperand(0).getValueType());
4082 // No extra round required here.
4083 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
4084 break;
4085 case Expand:
4086 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4087 Node->getOperand(0));
4088 // Round if we cannot tolerate excess precision.
4089 if (NoExcessFPPrecision)
4090 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4091 DAG.getValueType(VT));
4092 break;
4093 }
4094 break;
4095
4096 case ISD::SIGN_EXTEND_INREG:
4097 Result = PromoteOp(Node->getOperand(0));
4098 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4099 Node->getOperand(1));
4100 break;
4101 case ISD::FP_TO_SINT:
4102 case ISD::FP_TO_UINT:
4103 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4104 case Legal:
4105 case Expand:
4106 Tmp1 = Node->getOperand(0);
4107 break;
4108 case Promote:
4109 // The input result is prerounded, so we don't have to do anything
4110 // special.
4111 Tmp1 = PromoteOp(Node->getOperand(0));
4112 break;
4113 }
4114 // If we're promoting a UINT to a larger size, check to see if the new node
4115 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4116 // we can use that instead. This allows us to generate better code for
4117 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4118 // legal, such as PowerPC.
4119 if (Node->getOpcode() == ISD::FP_TO_UINT &&
4120 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
4121 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4122 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
4123 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4124 } else {
4125 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4126 }
4127 break;
4128
4129 case ISD::FABS:
4130 case ISD::FNEG:
4131 Tmp1 = PromoteOp(Node->getOperand(0));
4132 assert(Tmp1.getValueType() == NVT);
4133 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4134 // NOTE: we do not have to do any extra rounding here for
4135 // NoExcessFPPrecision, because we know the input will have the appropriate
4136 // precision, and these operations don't modify precision at all.
4137 break;
4138
4139 case ISD::FSQRT:
4140 case ISD::FSIN:
4141 case ISD::FCOS:
4142 Tmp1 = PromoteOp(Node->getOperand(0));
4143 assert(Tmp1.getValueType() == NVT);
4144 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4145 if (NoExcessFPPrecision)
4146 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4147 DAG.getValueType(VT));
4148 break;
4149
4150 case ISD::FPOWI: {
4151 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4152 // directly as well, which may be better.
4153 Tmp1 = PromoteOp(Node->getOperand(0));
4154 assert(Tmp1.getValueType() == NVT);
4155 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4156 if (NoExcessFPPrecision)
4157 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4158 DAG.getValueType(VT));
4159 break;
4160 }
4161
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004162 case ISD::ATOMIC_CMP_SWAP: {
4163 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004164 Tmp2 = PromoteOp(Node->getOperand(2));
4165 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004166 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4167 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004168 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004169 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004170 // Remember that we legalized the chain.
4171 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4172 break;
4173 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004174 case ISD::ATOMIC_LOAD_ADD:
4175 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang078a62d2008-05-05 19:05:59 +00004176 case ISD::ATOMIC_LOAD_AND:
4177 case ISD::ATOMIC_LOAD_OR:
4178 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharthaf02d592008-06-14 05:48:15 +00004179 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang078a62d2008-05-05 19:05:59 +00004180 case ISD::ATOMIC_LOAD_MIN:
4181 case ISD::ATOMIC_LOAD_MAX:
4182 case ISD::ATOMIC_LOAD_UMIN:
4183 case ISD::ATOMIC_LOAD_UMAX:
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004184 case ISD::ATOMIC_SWAP: {
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004185 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004186 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004187 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4188 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanc70fa752008-06-25 16:07:49 +00004189 AtomNode->getSrcValue(),
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004190 AtomNode->getAlignment());
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004191 // Remember that we legalized the chain.
4192 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4193 break;
4194 }
4195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 case ISD::AND:
4197 case ISD::OR:
4198 case ISD::XOR:
4199 case ISD::ADD:
4200 case ISD::SUB:
4201 case ISD::MUL:
4202 // The input may have strange things in the top bits of the registers, but
4203 // these operations don't care. They may have weird bits going out, but
4204 // that too is okay if they are integer operations.
4205 Tmp1 = PromoteOp(Node->getOperand(0));
4206 Tmp2 = PromoteOp(Node->getOperand(1));
4207 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4208 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4209 break;
4210 case ISD::FADD:
4211 case ISD::FSUB:
4212 case ISD::FMUL:
4213 Tmp1 = PromoteOp(Node->getOperand(0));
4214 Tmp2 = PromoteOp(Node->getOperand(1));
4215 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4216 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4217
4218 // Floating point operations will give excess precision that we may not be
4219 // able to tolerate. If we DO allow excess precision, just leave it,
4220 // otherwise excise it.
4221 // FIXME: Why would we need to round FP ops more than integer ones?
4222 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4223 if (NoExcessFPPrecision)
4224 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4225 DAG.getValueType(VT));
4226 break;
4227
4228 case ISD::SDIV:
4229 case ISD::SREM:
4230 // These operators require that their input be sign extended.
4231 Tmp1 = PromoteOp(Node->getOperand(0));
4232 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004233 if (NVT.isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4235 DAG.getValueType(VT));
4236 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4237 DAG.getValueType(VT));
4238 }
4239 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4240
4241 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands92c43912008-06-06 12:08:01 +00004242 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4244 DAG.getValueType(VT));
4245 break;
4246 case ISD::FDIV:
4247 case ISD::FREM:
4248 case ISD::FCOPYSIGN:
4249 // These operators require that their input be fp extended.
4250 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004251 case Expand: assert(0 && "not implemented");
4252 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4253 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 }
4255 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner5872a362008-01-17 07:00:52 +00004256 case Expand: assert(0 && "not implemented");
4257 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4258 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 }
4260 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4261
4262 // Perform FP_ROUND: this is probably overly pessimistic.
4263 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4264 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4265 DAG.getValueType(VT));
4266 break;
4267
4268 case ISD::UDIV:
4269 case ISD::UREM:
4270 // These operators require that their input be zero extended.
4271 Tmp1 = PromoteOp(Node->getOperand(0));
4272 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands92c43912008-06-06 12:08:01 +00004273 assert(NVT.isInteger() && "Operators don't apply to FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4275 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4276 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4277 break;
4278
4279 case ISD::SHL:
4280 Tmp1 = PromoteOp(Node->getOperand(0));
4281 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4282 break;
4283 case ISD::SRA:
4284 // The input value must be properly sign extended.
4285 Tmp1 = PromoteOp(Node->getOperand(0));
4286 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4287 DAG.getValueType(VT));
4288 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4289 break;
4290 case ISD::SRL:
4291 // The input value must be properly zero extended.
4292 Tmp1 = PromoteOp(Node->getOperand(0));
4293 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4294 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4295 break;
4296
4297 case ISD::VAARG:
4298 Tmp1 = Node->getOperand(0); // Get the chain.
4299 Tmp2 = Node->getOperand(1); // Get the pointer.
4300 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4301 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sandsac496a12008-07-04 11:47:58 +00004302 Result = TLI.LowerOperation(Tmp3, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 } else {
Dan Gohman12a9c082008-02-06 22:27:42 +00004304 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004305 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 // Increment the pointer, VAList, to the next vaarg
4307 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands92c43912008-06-06 12:08:01 +00004308 DAG.getConstant(VT.getSizeInBits()/8,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004309 TLI.getPointerTy()));
4310 // Store the incremented VAList to the legalized pointer
Dan Gohman12a9c082008-02-06 22:27:42 +00004311 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 // Load the actual argument out of the pointer VAList
4313 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4314 }
4315 // Remember that we legalized the chain.
4316 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4317 break;
4318
4319 case ISD::LOAD: {
4320 LoadSDNode *LD = cast<LoadSDNode>(Node);
4321 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4322 ? ISD::EXTLOAD : LD->getExtensionType();
4323 Result = DAG.getExtLoad(ExtType, NVT,
4324 LD->getChain(), LD->getBasePtr(),
4325 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004326 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004327 LD->isVolatile(),
4328 LD->getAlignment());
4329 // Remember that we legalized the chain.
4330 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4331 break;
4332 }
Scott Michel67224b22008-06-02 22:18:03 +00004333 case ISD::SELECT: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4335 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel67224b22008-06-02 22:18:03 +00004336
Duncan Sands92c43912008-06-06 12:08:01 +00004337 MVT VT2 = Tmp2.getValueType();
Scott Michel67224b22008-06-02 22:18:03 +00004338 assert(VT2 == Tmp3.getValueType()
Scott Michel7b54de02008-06-03 19:13:20 +00004339 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4340 // Ensure that the resulting node is at least the same size as the operands'
4341 // value types, because we cannot assume that TLI.getSetCCValueType() is
4342 // constant.
4343 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 break;
Scott Michel67224b22008-06-02 22:18:03 +00004345 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 case ISD::SELECT_CC:
4347 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4348 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4349 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4350 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4351 break;
4352 case ISD::BSWAP:
4353 Tmp1 = Node->getOperand(0);
4354 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4355 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4356 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004357 DAG.getConstant(NVT.getSizeInBits() -
4358 VT.getSizeInBits(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 TLI.getShiftAmountTy()));
4360 break;
4361 case ISD::CTPOP:
4362 case ISD::CTTZ:
4363 case ISD::CTLZ:
4364 // Zero extend the argument
4365 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4366 // Perform the larger operation, then subtract if needed.
4367 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4368 switch(Node->getOpcode()) {
4369 case ISD::CTPOP:
4370 Result = Tmp1;
4371 break;
4372 case ISD::CTTZ:
4373 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel502151f2008-03-10 15:42:14 +00004374 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004375 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 ISD::SETEQ);
4377 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands92c43912008-06-06 12:08:01 +00004378 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379 break;
4380 case ISD::CTLZ:
4381 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4382 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands92c43912008-06-06 12:08:01 +00004383 DAG.getConstant(NVT.getSizeInBits() -
4384 VT.getSizeInBits(), NVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385 break;
4386 }
4387 break;
4388 case ISD::EXTRACT_SUBVECTOR:
4389 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4390 break;
4391 case ISD::EXTRACT_VECTOR_ELT:
4392 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4393 break;
4394 }
4395
4396 assert(Result.Val && "Didn't set a result!");
4397
4398 // Make sure the result is itself legal.
4399 Result = LegalizeOp(Result);
4400
4401 // Remember that we promoted this!
4402 AddPromotedOperand(Op, Result);
4403 return Result;
4404}
4405
4406/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4407/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4408/// based on the vector type. The return type of this matches the element type
4409/// of the vector, which may not be legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00004410SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411 // We know that operand #0 is the Vec vector. If the index is a constant
4412 // or if the invec is a supported hardware type, we can use it. Otherwise,
4413 // lower to a store then an indexed load.
Dan Gohman8181bd12008-07-27 21:46:04 +00004414 SDValue Vec = Op.getOperand(0);
4415 SDValue Idx = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416
Duncan Sands92c43912008-06-06 12:08:01 +00004417 MVT TVT = Vec.getValueType();
4418 unsigned NumElems = TVT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004419
4420 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4421 default: assert(0 && "This action is not supported yet!");
4422 case TargetLowering::Custom: {
4423 Vec = LegalizeOp(Vec);
4424 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004425 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004426 if (Tmp3.Val)
4427 return Tmp3;
4428 break;
4429 }
4430 case TargetLowering::Legal:
4431 if (isTypeLegal(TVT)) {
4432 Vec = LegalizeOp(Vec);
4433 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004434 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435 }
4436 break;
4437 case TargetLowering::Expand:
4438 break;
4439 }
4440
4441 if (NumElems == 1) {
4442 // This must be an access of the only element. Return it.
4443 Op = ScalarizeVectorOp(Vec);
4444 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman2b10fde2008-01-29 02:24:00 +00004445 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004446 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004447 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman2b10fde2008-01-29 02:24:00 +00004449 if (CIdx->getValue() < NumLoElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450 Vec = Lo;
4451 } else {
4452 Vec = Hi;
Nate Begeman2b10fde2008-01-29 02:24:00 +00004453 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004454 Idx.getValueType());
4455 }
4456
4457 // It's now an extract from the appropriate high or low part. Recurse.
4458 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4459 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4460 } else {
4461 // Store the value to a temporary stack slot, then LOAD the scalar
4462 // element back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4464 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465
4466 // Add the offset to the index.
Duncan Sands92c43912008-06-06 12:08:01 +00004467 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4469 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004470
Duncan Sandsec142ee2008-06-08 20:54:56 +00004471 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004472 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004473 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004474 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004475
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004476 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4477
4478 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4479 }
4480 return Op;
4481}
4482
4483/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4484/// we assume the operation can be split if it is not already legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00004485SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004486 // We know that operand #0 is the Vec vector. For now we assume the index
4487 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004488 SDValue Vec = Op.getOperand(0);
4489 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490
Duncan Sands92c43912008-06-06 12:08:01 +00004491 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492
Duncan Sands92c43912008-06-06 12:08:01 +00004493 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004494 // This must be an access of the desired vector length. Return it.
4495 return Vec;
4496 }
4497
4498 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman8181bd12008-07-27 21:46:04 +00004499 SDValue Lo, Hi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500 SplitVectorOp(Vec, Lo, Hi);
4501 if (CIdx->getValue() < NumElems/2) {
4502 Vec = Lo;
4503 } else {
4504 Vec = Hi;
4505 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4506 }
4507
4508 // It's now an extract from the appropriate high or low part. Recurse.
4509 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4510 return ExpandEXTRACT_SUBVECTOR(Op);
4511}
4512
4513/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4514/// with condition CC on the current target. This usually involves legalizing
4515/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4516/// there may be no choice but to create a new SetCC node to represent the
4517/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman8181bd12008-07-27 21:46:04 +00004518/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4519void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4520 SDValue &RHS,
4521 SDValue &CC) {
4522 SDValue Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523
4524 switch (getTypeAction(LHS.getValueType())) {
4525 case Legal:
4526 Tmp1 = LegalizeOp(LHS); // LHS
4527 Tmp2 = LegalizeOp(RHS); // RHS
4528 break;
4529 case Promote:
4530 Tmp1 = PromoteOp(LHS); // LHS
4531 Tmp2 = PromoteOp(RHS); // RHS
4532
4533 // If this is an FP compare, the operands have already been extended.
Duncan Sands92c43912008-06-06 12:08:01 +00004534 if (LHS.getValueType().isInteger()) {
4535 MVT VT = LHS.getValueType();
4536 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537
4538 // Otherwise, we have to insert explicit sign or zero extends. Note
4539 // that we could insert sign extends for ALL conditions, but zero extend
4540 // is cheaper on many machines (an AND instead of two shifts), so prefer
4541 // it.
4542 switch (cast<CondCodeSDNode>(CC)->get()) {
4543 default: assert(0 && "Unknown integer comparison!");
4544 case ISD::SETEQ:
4545 case ISD::SETNE:
4546 case ISD::SETUGE:
4547 case ISD::SETUGT:
4548 case ISD::SETULE:
4549 case ISD::SETULT:
4550 // ALL of these operations will work if we either sign or zero extend
4551 // the operands (including the unsigned comparisons!). Zero extend is
4552 // usually a simpler/cheaper operation, so prefer it.
4553 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4554 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4555 break;
4556 case ISD::SETGE:
4557 case ISD::SETGT:
4558 case ISD::SETLT:
4559 case ISD::SETLE:
4560 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4561 DAG.getValueType(VT));
4562 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4563 DAG.getValueType(VT));
4564 break;
4565 }
4566 }
4567 break;
4568 case Expand: {
Duncan Sands92c43912008-06-06 12:08:01 +00004569 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004570 if (VT == MVT::f32 || VT == MVT::f64) {
4571 // Expand into one or more soft-fp libcall(s).
Evan Cheng24108632008-07-01 21:35:46 +00004572 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 switch (cast<CondCodeSDNode>(CC)->get()) {
4574 case ISD::SETEQ:
4575 case ISD::SETOEQ:
4576 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4577 break;
4578 case ISD::SETNE:
4579 case ISD::SETUNE:
4580 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4581 break;
4582 case ISD::SETGE:
4583 case ISD::SETOGE:
4584 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4585 break;
4586 case ISD::SETLT:
4587 case ISD::SETOLT:
4588 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4589 break;
4590 case ISD::SETLE:
4591 case ISD::SETOLE:
4592 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4593 break;
4594 case ISD::SETGT:
4595 case ISD::SETOGT:
4596 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4597 break;
4598 case ISD::SETUO:
4599 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4600 break;
4601 case ISD::SETO:
4602 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4603 break;
4604 default:
4605 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4606 switch (cast<CondCodeSDNode>(CC)->get()) {
4607 case ISD::SETONE:
4608 // SETONE = SETOLT | SETOGT
4609 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4610 // Fallthrough
4611 case ISD::SETUGT:
4612 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4613 break;
4614 case ISD::SETUGE:
4615 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4616 break;
4617 case ISD::SETULT:
4618 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4619 break;
4620 case ISD::SETULE:
4621 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4622 break;
4623 case ISD::SETUEQ:
4624 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4625 break;
4626 default: assert(0 && "Unsupported FP setcc!");
4627 }
4628 }
Duncan Sandsf19591c2008-06-30 10:19:09 +00004629
Dan Gohman8181bd12008-07-27 21:46:04 +00004630 SDValue Dummy;
4631 SDValue Ops[2] = { LHS, RHS };
Duncan Sands698842f2008-07-02 17:40:58 +00004632 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).Val,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004633 false /*sign irrelevant*/, Dummy);
4634 Tmp2 = DAG.getConstant(0, MVT::i32);
4635 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4636 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel502151f2008-03-10 15:42:14 +00004637 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004638 CC);
Duncan Sands698842f2008-07-02 17:40:58 +00004639 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).Val,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640 false /*sign irrelevant*/, Dummy);
Scott Michel502151f2008-03-10 15:42:14 +00004641 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004642 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4643 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004644 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004645 }
Evan Cheng18a1ab12008-07-07 07:18:09 +00004646 LHS = LegalizeOp(Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004647 RHS = Tmp2;
4648 return;
4649 }
4650
Dan Gohman8181bd12008-07-27 21:46:04 +00004651 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004652 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004653 ExpandOp(RHS, RHSLo, RHSHi);
4654 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4655
4656 if (VT==MVT::ppcf128) {
4657 // FIXME: This generated code sucks. We want to generate
4658 // FCMP crN, hi1, hi2
4659 // BNE crN, L:
4660 // FCMP crN, lo1, lo2
4661 // The following can be improved, but not that much.
Scott Michel502151f2008-03-10 15:42:14 +00004662 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4663 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004664 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michel502151f2008-03-10 15:42:14 +00004665 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4666 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004667 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4668 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman8181bd12008-07-27 21:46:04 +00004669 Tmp2 = SDValue();
Dale Johannesen472d15d2007-10-06 01:24:11 +00004670 break;
4671 }
4672
4673 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 case ISD::SETEQ:
4675 case ISD::SETNE:
4676 if (RHSLo == RHSHi)
4677 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4678 if (RHSCST->isAllOnesValue()) {
4679 // Comparison to -1.
4680 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4681 Tmp2 = RHSLo;
4682 break;
4683 }
4684
4685 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4686 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4687 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4688 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4689 break;
4690 default:
4691 // If this is a comparison of the sign bit, just look at the top part.
4692 // X > -1, x < 0
4693 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4694 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004695 CST->isNullValue()) || // X < 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004696 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4697 CST->isAllOnesValue())) { // X > -1
4698 Tmp1 = LHSHi;
4699 Tmp2 = RHSHi;
4700 break;
4701 }
4702
4703 // FIXME: This generated code sucks.
4704 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004705 switch (CCCode) {
4706 default: assert(0 && "Unknown integer setcc!");
4707 case ISD::SETLT:
4708 case ISD::SETULT: LowCC = ISD::SETULT; break;
4709 case ISD::SETGT:
4710 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4711 case ISD::SETLE:
4712 case ISD::SETULE: LowCC = ISD::SETULE; break;
4713 case ISD::SETGE:
4714 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4715 }
4716
4717 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4718 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4719 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4720
4721 // NOTE: on targets without efficient SELECT of bools, we can always use
4722 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4723 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel502151f2008-03-10 15:42:14 +00004724 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004725 LowCC, false, DagCombineInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726 if (!Tmp1.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004727 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4728 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 CCCode, false, DagCombineInfo);
4730 if (!Tmp2.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004731 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004732 RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004733
4734 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4735 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohman9d24dc72008-03-13 22:13:53 +00004736 if ((Tmp1C && Tmp1C->isNullValue()) ||
4737 (Tmp2C && Tmp2C->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4739 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman9d24dc72008-03-13 22:13:53 +00004740 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4742 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4743 // low part is known false, returns high part.
4744 // For LE / GE, if high part is known false, ignore the low part.
4745 // For LT / GT, if high part is known true, ignore the low part.
4746 Tmp1 = Tmp2;
Dan Gohman8181bd12008-07-27 21:46:04 +00004747 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 } else {
Scott Michel502151f2008-03-10 15:42:14 +00004749 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 ISD::SETEQ, false, DagCombineInfo);
4751 if (!Result.Val)
Scott Michel502151f2008-03-10 15:42:14 +00004752 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman8bb3cb32008-03-14 00:53:31 +00004753 ISD::SETEQ);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4755 Result, Tmp1, Tmp2));
4756 Tmp1 = Result;
Dan Gohman8181bd12008-07-27 21:46:04 +00004757 Tmp2 = SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758 }
4759 }
4760 }
4761 }
4762 LHS = Tmp1;
4763 RHS = Tmp2;
4764}
4765
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004766/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4767/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4768/// a load from the stack slot to DestVT, extending it if needed.
4769/// The resultant code need not be legal.
Dan Gohman8181bd12008-07-27 21:46:04 +00004770SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
4771 MVT SlotVT,
4772 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 // Create the stack frame object.
Mon P Wang55854cc2008-07-05 20:40:31 +00004774 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
4775 SrcOp.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00004776 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang55854cc2008-07-05 20:40:31 +00004777
Dan Gohman20e37962008-02-11 18:58:42 +00004778 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004779 int SPFI = StackPtrFI->getIndex();
Mon P Wang55854cc2008-07-05 20:40:31 +00004780
Duncan Sands92c43912008-06-06 12:08:01 +00004781 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
4782 unsigned SlotSize = SlotVT.getSizeInBits();
4783 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang55854cc2008-07-05 20:40:31 +00004784 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
4785 DestVT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004786
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004787 // Emit a store to the stack slot. Use a truncstore if the input value is
4788 // later than DestVT.
Dan Gohman8181bd12008-07-27 21:46:04 +00004789 SDValue Store;
Mon P Wang55854cc2008-07-05 20:40:31 +00004790
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004791 if (SrcSize > SlotSize)
Dan Gohman12a9c082008-02-06 22:27:42 +00004792 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004793 PseudoSourceValue::getFixedStack(SPFI), 0,
4794 SlotVT, false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004795 else {
4796 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman12a9c082008-02-06 22:27:42 +00004797 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004798 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang55854cc2008-07-05 20:40:31 +00004799 false, SrcAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004800 }
4801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004803 if (SlotSize == DestSize)
Mon P Wang55854cc2008-07-05 20:40:31 +00004804 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004805
4806 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang55854cc2008-07-05 20:40:31 +00004807 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
4808 false, DestAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809}
4810
Dan Gohman8181bd12008-07-27 21:46:04 +00004811SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 // Create a vector sized/aligned stack slot, store the value to element #0,
4813 // then load the whole vector back out.
Dan Gohman8181bd12008-07-27 21:46:04 +00004814 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman12a9c082008-02-06 22:27:42 +00004815
Dan Gohman20e37962008-02-11 18:58:42 +00004816 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman12a9c082008-02-06 22:27:42 +00004817 int SPFI = StackPtrFI->getIndex();
4818
Dan Gohman8181bd12008-07-27 21:46:04 +00004819 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004820 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00004821 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004822 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004823}
4824
4825
4826/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4827/// support the operation, but do support the resultant vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004828SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004829
4830 // If the only non-undef value is the low element, turn this into a
4831 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4832 unsigned NumElems = Node->getNumOperands();
4833 bool isOnlyLowElement = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004834 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerd8cee732008-03-09 00:29:42 +00004835
Dan Gohman8181bd12008-07-27 21:46:04 +00004836 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerd8cee732008-03-09 00:29:42 +00004837 // and use a bitmask instead of a list of elements.
Dan Gohman8181bd12008-07-27 21:46:04 +00004838 std::map<SDValue, std::vector<unsigned> > Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004839 Values[SplatValue].push_back(0);
4840 bool isConstant = true;
4841 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4842 SplatValue.getOpcode() != ISD::UNDEF)
4843 isConstant = false;
4844
4845 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004846 SDValue V = Node->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847 Values[V].push_back(i);
4848 if (V.getOpcode() != ISD::UNDEF)
4849 isOnlyLowElement = false;
4850 if (SplatValue != V)
Dan Gohman8181bd12008-07-27 21:46:04 +00004851 SplatValue = SDValue(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852
4853 // If this isn't a constant element or an undef, we can't use a constant
4854 // pool load.
4855 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4856 V.getOpcode() != ISD::UNDEF)
4857 isConstant = false;
4858 }
4859
4860 if (isOnlyLowElement) {
4861 // If the low element is an undef too, then this whole things is an undef.
4862 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4863 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4864 // Otherwise, turn this into a scalar_to_vector node.
4865 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4866 Node->getOperand(0));
4867 }
4868
4869 // If all elements are constants, create a load from the constant pool.
4870 if (isConstant) {
Duncan Sands92c43912008-06-06 12:08:01 +00004871 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872 std::vector<Constant*> CV;
4873 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4874 if (ConstantFPSDNode *V =
4875 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004876 CV.push_back(ConstantFP::get(V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 } else if (ConstantSDNode *V =
Chris Lattner5e0610f2008-04-20 00:41:09 +00004878 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4879 CV.push_back(ConstantInt::get(V->getAPIntValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 } else {
4881 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner5e0610f2008-04-20 00:41:09 +00004882 const Type *OpNTy =
Duncan Sands92c43912008-06-06 12:08:01 +00004883 Node->getOperand(0).getValueType().getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 CV.push_back(UndefValue::get(OpNTy));
4885 }
4886 }
4887 Constant *CP = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004888 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004889 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004890 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891 }
4892
4893 if (SplatValue.Val) { // Splat of one value?
4894 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands92c43912008-06-06 12:08:01 +00004895 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman8181bd12008-07-27 21:46:04 +00004896 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
4897 std::vector<SDValue> ZeroVec(NumElems, Zero);
4898 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004899 &ZeroVec[0], ZeroVec.size());
4900
4901 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4902 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4903 // Get the splatted value into the low element of a vector register.
Dan Gohman8181bd12008-07-27 21:46:04 +00004904 SDValue LowValVec =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4906
4907 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4908 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4909 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4910 SplatMask);
4911 }
4912 }
4913
4914 // If there are only two unique elements, we may be able to turn this into a
4915 // vector shuffle.
4916 if (Values.size() == 2) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00004917 // Get the two values in deterministic order.
Dan Gohman8181bd12008-07-27 21:46:04 +00004918 SDValue Val1 = Node->getOperand(1);
4919 SDValue Val2;
4920 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerd8cee732008-03-09 00:29:42 +00004921 if (MI->first != Val1)
4922 Val2 = MI->first;
4923 else
4924 Val2 = (++MI)->first;
4925
4926 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4927 // vector shuffle has the undef vector on the RHS.
4928 if (Val1.getOpcode() == ISD::UNDEF)
4929 std::swap(Val1, Val2);
4930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands92c43912008-06-06 12:08:01 +00004932 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4933 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004934 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerd8cee732008-03-09 00:29:42 +00004935
4936 // Set elements of the shuffle mask for Val1.
4937 std::vector<unsigned> &Val1Elts = Values[Val1];
4938 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4939 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4940
4941 // Set elements of the shuffle mask for Val2.
4942 std::vector<unsigned> &Val2Elts = Values[Val2];
4943 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4944 if (Val2.getOpcode() != ISD::UNDEF)
4945 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4946 else
4947 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4948
Dan Gohman8181bd12008-07-27 21:46:04 +00004949 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 &MaskVec[0], MaskVec.size());
4951
Chris Lattnerd8cee732008-03-09 00:29:42 +00004952 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4954 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerd8cee732008-03-09 00:29:42 +00004955 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
4956 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004957 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958
4959 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerd8cee732008-03-09 00:29:42 +00004960 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 }
4962 }
4963
4964 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4965 // aligned object on the stack, store each element into it, then load
4966 // the result as a vector.
Duncan Sands92c43912008-06-06 12:08:01 +00004967 MVT VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 // Create the stack frame object.
Dan Gohman8181bd12008-07-27 21:46:04 +00004969 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004970
4971 // Emit a store of each element to the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004972 SmallVector<SDValue, 8> Stores;
Duncan Sands92c43912008-06-06 12:08:01 +00004973 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 // Store (in the right endianness) the elements to memory.
4975 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4976 // Ignore undef elements.
4977 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4978
4979 unsigned Offset = TypeByteSize*i;
4980
Dan Gohman8181bd12008-07-27 21:46:04 +00004981 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004982 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4983
4984 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
4985 NULL, 0));
4986 }
4987
Dan Gohman8181bd12008-07-27 21:46:04 +00004988 SDValue StoreChain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989 if (!Stores.empty()) // Not all undef elements?
4990 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4991 &Stores[0], Stores.size());
4992 else
4993 StoreChain = DAG.getEntryNode();
4994
4995 // Result is a load from the stack slot.
4996 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
4997}
4998
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00005000 SDValue Op, SDValue Amt,
5001 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00005003 SDValue LHSL, LHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005004 ExpandOp(Op, LHSL, LHSH);
5005
Dan Gohman8181bd12008-07-27 21:46:04 +00005006 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands92c43912008-06-06 12:08:01 +00005007 MVT VT = LHSL.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005008 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
5009 Hi = Lo.getValue(1);
5010}
5011
5012
5013/// ExpandShift - Try to find a clever way to expand this shift operation out to
5014/// smaller elements. If we can't find a way that is more efficient than a
5015/// libcall on this target, return false. Otherwise, return true with the
5016/// low-parts expanded into Lo and Hi.
Dan Gohman8181bd12008-07-27 21:46:04 +00005017bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5018 SDValue &Lo, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5020 "This is not a shift!");
5021
Duncan Sands92c43912008-06-06 12:08:01 +00005022 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman8181bd12008-07-27 21:46:04 +00005023 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands92c43912008-06-06 12:08:01 +00005024 MVT ShTy = ShAmt.getValueType();
5025 unsigned ShBits = ShTy.getSizeInBits();
5026 unsigned VTBits = Op.getValueType().getSizeInBits();
5027 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
Chris Lattner8c931452007-10-14 20:35:12 +00005029 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5031 unsigned Cst = CN->getValue();
5032 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005033 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 ExpandOp(Op, InL, InH);
5035 switch(Opc) {
5036 case ISD::SHL:
5037 if (Cst > VTBits) {
5038 Lo = DAG.getConstant(0, NVT);
5039 Hi = DAG.getConstant(0, NVT);
5040 } else if (Cst > NVTBits) {
5041 Lo = DAG.getConstant(0, NVT);
5042 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
5043 } else if (Cst == NVTBits) {
5044 Lo = DAG.getConstant(0, NVT);
5045 Hi = InL;
5046 } else {
5047 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5048 Hi = DAG.getNode(ISD::OR, NVT,
5049 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5050 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5051 }
5052 return true;
5053 case ISD::SRL:
5054 if (Cst > VTBits) {
5055 Lo = DAG.getConstant(0, NVT);
5056 Hi = DAG.getConstant(0, NVT);
5057 } else if (Cst > NVTBits) {
5058 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5059 Hi = DAG.getConstant(0, NVT);
5060 } else if (Cst == NVTBits) {
5061 Lo = InH;
5062 Hi = DAG.getConstant(0, NVT);
5063 } else {
5064 Lo = DAG.getNode(ISD::OR, NVT,
5065 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5066 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5067 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5068 }
5069 return true;
5070 case ISD::SRA:
5071 if (Cst > VTBits) {
5072 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
5073 DAG.getConstant(NVTBits-1, ShTy));
5074 } else if (Cst > NVTBits) {
5075 Lo = DAG.getNode(ISD::SRA, NVT, InH,
5076 DAG.getConstant(Cst-NVTBits, ShTy));
5077 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5078 DAG.getConstant(NVTBits-1, ShTy));
5079 } else if (Cst == NVTBits) {
5080 Lo = InH;
5081 Hi = DAG.getNode(ISD::SRA, NVT, InH,
5082 DAG.getConstant(NVTBits-1, ShTy));
5083 } else {
5084 Lo = DAG.getNode(ISD::OR, NVT,
5085 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5086 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5087 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5088 }
5089 return true;
5090 }
5091 }
5092
5093 // Okay, the shift amount isn't constant. However, if we can tell that it is
5094 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohmanece0a882008-02-20 16:57:27 +00005095 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5096 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
5098
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005099 // If we know that if any of the high bits of the shift amount are one, then
5100 // we can do this as a couple of simple shifts.
Dan Gohmanece0a882008-02-20 16:57:27 +00005101 if (KnownOne.intersects(Mask)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005102 // Mask out the high bit, which we know is set.
5103 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohmanece0a882008-02-20 16:57:27 +00005104 DAG.getConstant(~Mask, Amt.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105
5106 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005107 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 ExpandOp(Op, InL, InH);
5109 switch(Opc) {
5110 case ISD::SHL:
5111 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5112 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5113 return true;
5114 case ISD::SRL:
5115 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5116 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5117 return true;
5118 case ISD::SRA:
5119 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5120 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5121 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5122 return true;
5123 }
5124 }
5125
Dan Gohmaneb3f1172008-02-22 01:12:31 +00005126 // If we know that the high bits of the shift amount are all zero, then we can
5127 // do this as a couple of simple shifts.
5128 if ((KnownZero & Mask) == Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005129 // Compute 32-amt.
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131 DAG.getConstant(NVTBits, Amt.getValueType()),
5132 Amt);
5133
5134 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman8181bd12008-07-27 21:46:04 +00005135 SDValue InL, InH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136 ExpandOp(Op, InL, InH);
5137 switch(Opc) {
5138 case ISD::SHL:
5139 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5140 Hi = DAG.getNode(ISD::OR, NVT,
5141 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5142 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5143 return true;
5144 case ISD::SRL:
5145 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5146 Lo = DAG.getNode(ISD::OR, NVT,
5147 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5148 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5149 return true;
5150 case ISD::SRA:
5151 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5152 Lo = DAG.getNode(ISD::OR, NVT,
5153 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5154 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5155 return true;
5156 }
5157 }
5158
5159 return false;
5160}
5161
5162
5163// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5164// does not fit into a register, return the lo part and set the hi part to the
5165// by-reg argument. If it does fit into a single register, return the result
5166// and leave the Hi part unset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005167SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5168 bool isSigned, SDValue &Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005169 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5170 // The input chain to this libcall is the entry node of the function.
5171 // Legalizing the call will automatically add the previous call to the
5172 // dependence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005173 SDValue InChain = DAG.getEntryNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174
5175 TargetLowering::ArgListTy Args;
5176 TargetLowering::ArgListEntry Entry;
5177 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00005178 MVT ArgVT = Node->getOperand(i).getValueType();
5179 const Type *ArgTy = ArgVT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005180 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
5181 Entry.isSExt = isSigned;
Duncan Sandsead972e2008-02-14 17:28:50 +00005182 Entry.isZExt = !isSigned;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005183 Args.push_back(Entry);
5184 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005185 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Duncan Sandsf1db7c82008-04-12 17:14:18 +00005186 TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187
5188 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands92c43912008-06-06 12:08:01 +00005189 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00005190 std::pair<SDValue,SDValue> CallInfo =
Duncan Sandsead972e2008-02-14 17:28:50 +00005191 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5192 false, Callee, Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193
5194 // Legalize the call sequence, starting with the chain. This will advance
5195 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5196 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5197 LegalizeOp(CallInfo.second);
Dan Gohman8181bd12008-07-27 21:46:04 +00005198 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199 switch (getTypeAction(CallInfo.first.getValueType())) {
5200 default: assert(0 && "Unknown thing");
5201 case Legal:
5202 Result = CallInfo.first;
5203 break;
5204 case Expand:
5205 ExpandOp(CallInfo.first, Result, Hi);
5206 break;
5207 }
5208 return Result;
5209}
5210
Dan Gohman29c3cef2008-08-14 20:04:46 +00005211/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5212///
5213SDValue SelectionDAGLegalize::
5214LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5215 bool isCustom = false;
5216 SDValue Tmp1;
5217 switch (getTypeAction(Op.getValueType())) {
5218 case Legal:
5219 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5220 Op.getValueType())) {
5221 default: assert(0 && "Unknown operation action!");
5222 case TargetLowering::Custom:
5223 isCustom = true;
5224 // FALLTHROUGH
5225 case TargetLowering::Legal:
5226 Tmp1 = LegalizeOp(Op);
5227 if (Result.Val)
5228 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5229 else
5230 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5231 DestTy, Tmp1);
5232 if (isCustom) {
5233 Tmp1 = TLI.LowerOperation(Result, DAG);
5234 if (Tmp1.Val) Result = Tmp1;
5235 }
5236 break;
5237 case TargetLowering::Expand:
5238 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5239 break;
5240 case TargetLowering::Promote:
5241 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5242 break;
5243 }
5244 break;
5245 case Expand:
5246 Result = ExpandIntToFP(isSigned, DestTy, Op);
5247 break;
5248 case Promote:
5249 Tmp1 = PromoteOp(Op);
5250 if (isSigned) {
5251 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5252 Tmp1, DAG.getValueType(Op.getValueType()));
5253 } else {
5254 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5255 Op.getValueType());
5256 }
5257 if (Result.Val)
5258 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5259 else
5260 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5261 DestTy, Tmp1);
5262 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5263 break;
5264 }
5265 return Result;
5266}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005267
5268/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5269///
Dan Gohman8181bd12008-07-27 21:46:04 +00005270SDValue SelectionDAGLegalize::
5271ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands92c43912008-06-06 12:08:01 +00005272 MVT SourceVT = Source.getValueType();
Dan Gohman8b232ff2008-03-11 01:59:03 +00005273 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274
Dan Gohman29c3cef2008-08-14 20:04:46 +00005275 // Expand unsupported int-to-fp vector casts by unrolling them.
5276 if (DestTy.isVector()) {
5277 if (!ExpandSource)
5278 return LegalizeOp(UnrollVectorOp(Source));
5279 MVT DestEltTy = DestTy.getVectorElementType();
5280 if (DestTy.getVectorNumElements() == 1) {
5281 SDValue Scalar = ScalarizeVectorOp(Source);
5282 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5283 DestEltTy, Scalar);
5284 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5285 }
5286 SDValue Lo, Hi;
5287 SplitVectorOp(Source, Lo, Hi);
5288 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5289 DestTy.getVectorNumElements() / 2);
5290 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5291 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
5292 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult, HiResult));
5293 }
5294
Evan Chengf99a7752008-04-01 02:18:22 +00005295 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5296 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmana193dba2008-03-05 02:07:31 +00005297 // The integer value loaded will be incorrectly if the 'sign bit' of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005298 // incoming integer is set. To handle this, we dynamically test to see if
5299 // it is set, and, if so, add a fudge factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005300 SDValue Hi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005301 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005302 SDValue Lo;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005303 ExpandOp(Source, Lo, Hi);
5304 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5305 } else {
5306 // The comparison for the sign bit will use the entire operand.
5307 Hi = Source;
5308 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309
5310 // If this is unsigned, and not supported, first perform the conversion to
5311 // signed, then adjust the result if the sign bit is set.
Dan Gohman8181bd12008-07-27 21:46:04 +00005312 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005313
Dan Gohman8181bd12008-07-27 21:46:04 +00005314 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315 DAG.getConstant(0, Hi.getValueType()),
5316 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005317 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5318 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319 SignSet, Four, Zero);
5320 uint64_t FF = 0x5f800000ULL;
5321 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmana193dba2008-03-05 02:07:31 +00005322 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman8181bd12008-07-27 21:46:04 +00005326 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 if (DestTy == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005328 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005329 PseudoSourceValue::getConstantPool(), 0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005330 else if (DestTy.bitsGT(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005332 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00005333 CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005334 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005335 MVT::f32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00005336 else
5337 assert(0 && "Unexpected conversion");
5338
Duncan Sands92c43912008-06-06 12:08:01 +00005339 MVT SCVT = SignedConv.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005340 if (SCVT != DestTy) {
5341 // Destination type needs to be expanded as well. The FADD now we are
5342 // constructing will be expanded into a libcall.
Duncan Sands92c43912008-06-06 12:08:01 +00005343 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5344 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmanc98645c2008-03-05 01:08:17 +00005345 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005346 SignedConv, SignedConv.getValue(1));
5347 }
5348 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5349 }
5350 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5351 }
5352
5353 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmanc98645c2008-03-05 01:08:17 +00005354 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005355 default: assert(0 && "This action not implemented for this operation!");
5356 case TargetLowering::Legal:
5357 case TargetLowering::Expand:
5358 break; // This case is handled below.
5359 case TargetLowering::Custom: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005360 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361 Source), DAG);
5362 if (NV.Val)
5363 return LegalizeOp(NV);
5364 break; // The target decided this was legal after all
5365 }
5366 }
5367
5368 // Expand the source, then glue it back together for the call. We must expand
5369 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman8b232ff2008-03-11 01:59:03 +00005370 if (ExpandSource) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005371 SDValue SrcLo, SrcHi;
Dan Gohman8b232ff2008-03-11 01:59:03 +00005372 ExpandOp(Source, SrcLo, SrcHi);
5373 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5374 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375
Duncan Sandsf68dffb2008-07-17 02:36:29 +00005376 RTLIB::Libcall LC = isSigned ?
5377 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5378 RTLIB::getUINTTOFP(SourceVT, DestTy);
5379 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5380
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005381 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman8181bd12008-07-27 21:46:04 +00005382 SDValue HiPart;
5383 SDValue Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart);
Evan Chenga8740032008-04-01 01:50:16 +00005384 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmanec51f642008-03-10 23:03:31 +00005385 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5386 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005387}
5388
5389/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5390/// INT_TO_FP operation of the specified operand when the target requests that
5391/// we expand it. At this point, we know that the result and operand types are
5392/// legal for the target.
Dan Gohman8181bd12008-07-27 21:46:04 +00005393SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5394 SDValue Op0,
5395 MVT DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005396 if (Op0.getValueType() == MVT::i32) {
5397 // simple 32-bit [signed|unsigned] integer to float/double expansion
5398
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005399 // Get the stack frame index of a 8 byte buffer.
Dan Gohman8181bd12008-07-27 21:46:04 +00005400 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005401
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005402 // word offset constant for Hi/Lo address computation
Dan Gohman8181bd12008-07-27 21:46:04 +00005403 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005404 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman8181bd12008-07-27 21:46:04 +00005405 SDValue Hi = StackSlot;
5406 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005407 if (TLI.isLittleEndian())
5408 std::swap(Hi, Lo);
5409
5410 // if signed map to unsigned space
Dan Gohman8181bd12008-07-27 21:46:04 +00005411 SDValue Op0Mapped;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412 if (isSigned) {
5413 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman8181bd12008-07-27 21:46:04 +00005414 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005415 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5416 } else {
5417 Op0Mapped = Op0;
5418 }
5419 // store the lo of the constructed double - based on integer input
Dan Gohman8181bd12008-07-27 21:46:04 +00005420 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421 Op0Mapped, Lo, NULL, 0);
5422 // initial hi portion of constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005423 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005424 // store the hi of the constructed double - biased exponent
Dan Gohman8181bd12008-07-27 21:46:04 +00005425 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 // load the constructed double
Dan Gohman8181bd12008-07-27 21:46:04 +00005427 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428 // FP constant to bias correct the final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005429 SDValue Bias = DAG.getConstantFP(isSigned ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005430 BitsToDouble(0x4330000080000000ULL)
5431 : BitsToDouble(0x4330000000000000ULL),
5432 MVT::f64);
5433 // subtract the bias
Dan Gohman8181bd12008-07-27 21:46:04 +00005434 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435 // final result
Dan Gohman8181bd12008-07-27 21:46:04 +00005436 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005437 // handle final rounding
5438 if (DestVT == MVT::f64) {
5439 // do nothing
5440 Result = Sub;
Duncan Sandsec142ee2008-06-08 20:54:56 +00005441 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner5872a362008-01-17 07:00:52 +00005442 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5443 DAG.getIntPtrConstant(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00005444 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005445 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446 }
5447 return Result;
5448 }
5449 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman8181bd12008-07-27 21:46:04 +00005450 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451
Dan Gohman8181bd12008-07-27 21:46:04 +00005452 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005453 DAG.getConstant(0, Op0.getValueType()),
5454 ISD::SETLT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005455 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5456 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005457 SignSet, Four, Zero);
5458
5459 // If the sign bit of the integer is set, the large number will be treated
5460 // as a negative number. To counteract this, the dynamic code adds an
5461 // offset depending on the data type.
5462 uint64_t FF;
Duncan Sands92c43912008-06-06 12:08:01 +00005463 switch (Op0.getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464 default: assert(0 && "Unsupported integer type!");
5465 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5466 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5467 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5468 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5469 }
5470 if (TLI.isLittleEndian()) FF <<= 32;
5471 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5472
Dan Gohman8181bd12008-07-27 21:46:04 +00005473 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman8181bd12008-07-27 21:46:04 +00005475 SDValue FudgeInReg;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 if (DestVT == MVT::f32)
Dan Gohman12a9c082008-02-06 22:27:42 +00005477 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005478 PseudoSourceValue::getConstantPool(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005479 else {
Dan Gohman12a9c082008-02-06 22:27:42 +00005480 FudgeInReg =
5481 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5482 DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005483 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman12a9c082008-02-06 22:27:42 +00005484 MVT::f32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 }
5486
5487 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5488}
5489
5490/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5491/// *INT_TO_FP operation of the specified operand when the target requests that
5492/// we promote it. At this point, we know that the result and operand types are
5493/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5494/// operation that takes a larger input.
Dan Gohman8181bd12008-07-27 21:46:04 +00005495SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5496 MVT DestVT,
5497 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005499 MVT NewInTy = LegalOp.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005500
5501 unsigned OpToUse = 0;
5502
5503 // Scan for the appropriate larger type to use.
5504 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005505 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5506 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507
5508 // If the target supports SINT_TO_FP of this type, use it.
5509 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5510 default: break;
5511 case TargetLowering::Legal:
5512 if (!TLI.isTypeLegal(NewInTy))
5513 break; // Can't use this datatype.
5514 // FALL THROUGH.
5515 case TargetLowering::Custom:
5516 OpToUse = ISD::SINT_TO_FP;
5517 break;
5518 }
5519 if (OpToUse) break;
5520 if (isSigned) continue;
5521
5522 // If the target supports UINT_TO_FP of this type, use it.
5523 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5524 default: break;
5525 case TargetLowering::Legal:
5526 if (!TLI.isTypeLegal(NewInTy))
5527 break; // Can't use this datatype.
5528 // FALL THROUGH.
5529 case TargetLowering::Custom:
5530 OpToUse = ISD::UINT_TO_FP;
5531 break;
5532 }
5533 if (OpToUse) break;
5534
5535 // Otherwise, try a larger type.
5536 }
5537
5538 // Okay, we found the operation and type to use. Zero extend our input to the
5539 // desired type then run the operation on it.
5540 return DAG.getNode(OpToUse, DestVT,
5541 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5542 NewInTy, LegalOp));
5543}
5544
5545/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5546/// FP_TO_*INT operation of the specified operand when the target requests that
5547/// we promote it. At this point, we know that the result and operand types are
5548/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5549/// operation that returns a larger result.
Dan Gohman8181bd12008-07-27 21:46:04 +00005550SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5551 MVT DestVT,
5552 bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005553 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands92c43912008-06-06 12:08:01 +00005554 MVT NewOutTy = DestVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555
5556 unsigned OpToUse = 0;
5557
5558 // Scan for the appropriate larger type to use.
5559 while (1) {
Duncan Sands92c43912008-06-06 12:08:01 +00005560 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5561 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562
5563 // If the target supports FP_TO_SINT returning this type, use it.
5564 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5565 default: break;
5566 case TargetLowering::Legal:
5567 if (!TLI.isTypeLegal(NewOutTy))
5568 break; // Can't use this datatype.
5569 // FALL THROUGH.
5570 case TargetLowering::Custom:
5571 OpToUse = ISD::FP_TO_SINT;
5572 break;
5573 }
5574 if (OpToUse) break;
5575
5576 // If the target supports FP_TO_UINT of this type, use it.
5577 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5578 default: break;
5579 case TargetLowering::Legal:
5580 if (!TLI.isTypeLegal(NewOutTy))
5581 break; // Can't use this datatype.
5582 // FALL THROUGH.
5583 case TargetLowering::Custom:
5584 OpToUse = ISD::FP_TO_UINT;
5585 break;
5586 }
5587 if (OpToUse) break;
5588
5589 // Otherwise, try a larger type.
5590 }
5591
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005592
5593 // Okay, we found the operation and type to use.
Dan Gohman8181bd12008-07-27 21:46:04 +00005594 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sandsac496a12008-07-04 11:47:58 +00005595
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005596 // If the operation produces an invalid type, it must be custom lowered. Use
5597 // the target lowering hooks to expand it. Just keep the low part of the
5598 // expanded operation, we know that we're truncating anyway.
5599 if (getTypeAction(NewOutTy) == Expand) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005600 Operation = SDValue(TLI.ReplaceNodeResults(Operation.Val, DAG), 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005601 assert(Operation.Val && "Didn't return anything");
5602 }
Duncan Sandsac496a12008-07-04 11:47:58 +00005603
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005604 // Truncate the result of the extended FP_TO_*INT operation to the desired
5605 // size.
5606 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607}
5608
5609/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5610///
Dan Gohman8181bd12008-07-27 21:46:04 +00005611SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00005612 MVT VT = Op.getValueType();
5613 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00005614 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands92c43912008-06-06 12:08:01 +00005615 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5617 case MVT::i16:
5618 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5619 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5620 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5621 case MVT::i32:
5622 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5623 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5624 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5625 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5626 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5627 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5628 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5629 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5630 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5631 case MVT::i64:
5632 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5633 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5634 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5635 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5636 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5637 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5638 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5639 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5640 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5641 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5642 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5643 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5644 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5645 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5646 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5647 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5648 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5649 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5650 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5651 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5652 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5653 }
5654}
5655
5656/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5657///
Dan Gohman8181bd12008-07-27 21:46:04 +00005658SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005659 switch (Opc) {
5660 default: assert(0 && "Cannot expand this yet!");
5661 case ISD::CTPOP: {
5662 static const uint64_t mask[6] = {
5663 0x5555555555555555ULL, 0x3333333333333333ULL,
5664 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5665 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5666 };
Duncan Sands92c43912008-06-06 12:08:01 +00005667 MVT VT = Op.getValueType();
5668 MVT ShVT = TLI.getShiftAmountTy();
5669 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005670 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5671 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman8181bd12008-07-27 21:46:04 +00005672 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
5673 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005674 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5675 DAG.getNode(ISD::AND, VT,
5676 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5677 }
5678 return Op;
5679 }
5680 case ISD::CTLZ: {
5681 // for now, we do this:
5682 // x = x | (x >> 1);
5683 // x = x | (x >> 2);
5684 // ...
5685 // x = x | (x >>16);
5686 // x = x | (x >>32); // for 64-bit input
5687 // return popcount(~x);
5688 //
5689 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00005690 MVT VT = Op.getValueType();
5691 MVT ShVT = TLI.getShiftAmountTy();
5692 unsigned len = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005693 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005694 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005695 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5696 }
5697 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5698 return DAG.getNode(ISD::CTPOP, VT, Op);
5699 }
5700 case ISD::CTTZ: {
5701 // for now, we use: { return popcount(~x & (x - 1)); }
5702 // unless the target has ctlz but not ctpop, in which case we use:
5703 // { return 32 - nlz(~x & (x-1)); }
5704 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands92c43912008-06-06 12:08:01 +00005705 MVT VT = Op.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005706 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
5707 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005708 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5709 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5710 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5711 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5712 TLI.isOperationLegal(ISD::CTLZ, VT))
5713 return DAG.getNode(ISD::SUB, VT,
Duncan Sands92c43912008-06-06 12:08:01 +00005714 DAG.getConstant(VT.getSizeInBits(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005715 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5716 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5717 }
5718 }
5719}
5720
Dan Gohman8181bd12008-07-27 21:46:04 +00005721/// ExpandOp - Expand the specified SDValue into its two component pieces
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5723/// LegalizeNodes map is filled in for any results that are not expanded, the
5724/// ExpandedNodes map is filled in for any results that are expanded, and the
5725/// Lo/Hi values are returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00005726void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands92c43912008-06-06 12:08:01 +00005727 MVT VT = Op.getValueType();
5728 MVT NVT = TLI.getTypeToTransformTo(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005729 SDNode *Node = Op.Val;
5730 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00005731 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands92c43912008-06-06 12:08:01 +00005732 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733
5734 // See if we already expanded it.
Dan Gohman8181bd12008-07-27 21:46:04 +00005735 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005736 = ExpandedNodes.find(Op);
5737 if (I != ExpandedNodes.end()) {
5738 Lo = I->second.first;
5739 Hi = I->second.second;
5740 return;
5741 }
5742
5743 switch (Node->getOpcode()) {
5744 case ISD::CopyFromReg:
5745 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005746 case ISD::FP_ROUND_INREG:
5747 if (VT == MVT::ppcf128 &&
5748 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5749 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005750 SDValue SrcLo, SrcHi, Src;
Dale Johannesend3b6af32007-10-11 23:32:15 +00005751 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5752 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman8181bd12008-07-27 21:46:04 +00005753 SDValue Result = TLI.LowerOperation(
Dale Johannesend3b6af32007-10-11 23:32:15 +00005754 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005755 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5756 Lo = Result.Val->getOperand(0);
5757 Hi = Result.Val->getOperand(1);
5758 break;
5759 }
5760 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005761 default:
5762#ifndef NDEBUG
5763 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5764#endif
5765 assert(0 && "Do not know how to expand this operator!");
5766 abort();
Dan Gohman550c8462008-02-27 01:52:30 +00005767 case ISD::EXTRACT_ELEMENT:
5768 ExpandOp(Node->getOperand(0), Lo, Hi);
5769 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5770 return ExpandOp(Hi, Lo, Hi);
Dan Gohman7e7aa2c2008-02-27 19:44:57 +00005771 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005772 case ISD::EXTRACT_VECTOR_ELT:
5773 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5774 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5775 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5776 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 case ISD::UNDEF:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 Lo = DAG.getNode(ISD::UNDEF, NVT);
5779 Hi = DAG.getNode(ISD::UNDEF, NVT);
5780 break;
5781 case ISD::Constant: {
Duncan Sands92c43912008-06-06 12:08:01 +00005782 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman97f1f8e2008-03-03 22:20:46 +00005783 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5784 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5785 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 break;
5787 }
5788 case ISD::ConstantFP: {
5789 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005790 if (CFP->getValueType(0) == MVT::ppcf128) {
5791 APInt api = CFP->getValueAPF().convertToAPInt();
5792 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5793 MVT::f64);
5794 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5795 MVT::f64);
5796 break;
5797 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005798 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5799 if (getTypeAction(Lo.getValueType()) == Expand)
5800 ExpandOp(Lo, Lo, Hi);
5801 break;
5802 }
5803 case ISD::BUILD_PAIR:
5804 // Return the operands.
5805 Lo = Node->getOperand(0);
5806 Hi = Node->getOperand(1);
5807 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005808
5809 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005810 if (Node->getNumValues() == 1) {
5811 ExpandOp(Op.getOperand(0), Lo, Hi);
5812 break;
5813 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005814 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5815 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5816 Op.getValue(1).getValueType() == MVT::Other &&
5817 "unhandled MERGE_VALUES");
5818 ExpandOp(Op.getOperand(0), Lo, Hi);
5819 // Remember that we legalized the chain.
5820 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5821 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822
5823 case ISD::SIGN_EXTEND_INREG:
5824 ExpandOp(Node->getOperand(0), Lo, Hi);
5825 // sext_inreg the low part if needed.
5826 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5827
5828 // The high part gets the sign extension from the lo-part. This handles
5829 // things like sextinreg V:i64 from i8.
5830 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands92c43912008-06-06 12:08:01 +00005831 DAG.getConstant(NVT.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 TLI.getShiftAmountTy()));
5833 break;
5834
5835 case ISD::BSWAP: {
5836 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00005837 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5839 Lo = TempLo;
5840 break;
5841 }
5842
5843 case ISD::CTPOP:
5844 ExpandOp(Node->getOperand(0), Lo, Hi);
5845 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5846 DAG.getNode(ISD::CTPOP, NVT, Lo),
5847 DAG.getNode(ISD::CTPOP, NVT, Hi));
5848 Hi = DAG.getConstant(0, NVT);
5849 break;
5850
5851 case ISD::CTLZ: {
5852 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5853 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00005854 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
5855 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5856 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005857 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00005858 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5860
5861 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5862 Hi = DAG.getConstant(0, NVT);
5863 break;
5864 }
5865
5866 case ISD::CTTZ: {
5867 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5868 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00005869 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
5870 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5871 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872 ISD::SETNE);
Dan Gohman8181bd12008-07-27 21:46:04 +00005873 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005874 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5875
5876 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5877 Hi = DAG.getConstant(0, NVT);
5878 break;
5879 }
5880
5881 case ISD::VAARG: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005882 SDValue Ch = Node->getOperand(0); // Legalize the chain.
5883 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005884 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5885 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5886
5887 // Remember that we legalized the chain.
5888 Hi = LegalizeOp(Hi);
5889 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005890 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005891 std::swap(Lo, Hi);
5892 break;
5893 }
5894
5895 case ISD::LOAD: {
5896 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00005897 SDValue Ch = LD->getChain(); // Legalize the chain.
5898 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005899 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman29c3cef2008-08-14 20:04:46 +00005900 const Value *SV = LD->getSrcValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005901 int SVOffset = LD->getSrcValueOffset();
5902 unsigned Alignment = LD->getAlignment();
5903 bool isVolatile = LD->isVolatile();
5904
5905 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00005906 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907 isVolatile, Alignment);
5908 if (VT == MVT::f32 || VT == MVT::f64) {
5909 // f32->i32 or f64->i64 one to one expansion.
5910 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00005911 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005912 // Recursively expand the new load.
5913 if (getTypeAction(NVT) == Expand)
5914 ExpandOp(Lo, Lo, Hi);
5915 break;
5916 }
5917
5918 // Increment the pointer to the other half.
Duncan Sands92c43912008-06-06 12:08:01 +00005919 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005920 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00005921 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005922 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005923 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00005924 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005925 isVolatile, Alignment);
5926
5927 // Build a factor node to remember that this load is independent of the
5928 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005929 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005930 Hi.getValue(1));
5931
5932 // Remember that we legalized the chain.
5933 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005934 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935 std::swap(Lo, Hi);
5936 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00005937 MVT EVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005938
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005939 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5940 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005941 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman29c3cef2008-08-14 20:04:46 +00005942 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005943 SVOffset, isVolatile, Alignment);
5944 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00005945 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005946 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5947 break;
5948 }
5949
5950 if (EVT == NVT)
Dan Gohman29c3cef2008-08-14 20:04:46 +00005951 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005952 SVOffset, isVolatile, Alignment);
5953 else
Dan Gohman29c3cef2008-08-14 20:04:46 +00005954 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955 SVOffset, EVT, isVolatile,
5956 Alignment);
5957
5958 // Remember that we legalized the chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00005959 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005960
5961 if (ExtType == ISD::SEXTLOAD) {
5962 // The high part is obtained by SRA'ing all but one of the bits of the
5963 // lo part.
Duncan Sands92c43912008-06-06 12:08:01 +00005964 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005965 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5966 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5967 } else if (ExtType == ISD::ZEXTLOAD) {
5968 // The high part is just a zero.
5969 Hi = DAG.getConstant(0, NVT);
5970 } else /* if (ExtType == ISD::EXTLOAD) */ {
5971 // The high part is undefined.
5972 Hi = DAG.getNode(ISD::UNDEF, NVT);
5973 }
5974 }
5975 break;
5976 }
5977 case ISD::AND:
5978 case ISD::OR:
5979 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman8181bd12008-07-27 21:46:04 +00005980 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005981 ExpandOp(Node->getOperand(0), LL, LH);
5982 ExpandOp(Node->getOperand(1), RL, RH);
5983 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5984 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5985 break;
5986 }
5987 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005988 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005989 ExpandOp(Node->getOperand(1), LL, LH);
5990 ExpandOp(Node->getOperand(2), RL, RH);
5991 if (getTypeAction(NVT) == Expand)
5992 NVT = TLI.getTypeToExpandTo(NVT);
5993 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5994 if (VT != MVT::f32)
5995 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5996 break;
5997 }
5998 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005999 SDValue TL, TH, FL, FH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 ExpandOp(Node->getOperand(2), TL, TH);
6001 ExpandOp(Node->getOperand(3), FL, FH);
6002 if (getTypeAction(NVT) == Expand)
6003 NVT = TLI.getTypeToExpandTo(NVT);
6004 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6005 Node->getOperand(1), TL, FL, Node->getOperand(4));
6006 if (VT != MVT::f32)
6007 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6008 Node->getOperand(1), TH, FH, Node->getOperand(4));
6009 break;
6010 }
6011 case ISD::ANY_EXTEND:
6012 // The low part is any extension of the input (which degenerates to a copy).
6013 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6014 // The high part is undefined.
6015 Hi = DAG.getNode(ISD::UNDEF, NVT);
6016 break;
6017 case ISD::SIGN_EXTEND: {
6018 // The low part is just a sign extension of the input (which degenerates to
6019 // a copy).
6020 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
6021
6022 // The high part is obtained by SRA'ing all but one of the bits of the lo
6023 // part.
Duncan Sands92c43912008-06-06 12:08:01 +00006024 unsigned LoSize = Lo.getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006025 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6026 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6027 break;
6028 }
6029 case ISD::ZERO_EXTEND:
6030 // The low part is just a zero extension of the input (which degenerates to
6031 // a copy).
6032 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
6033
6034 // The high part is just a zero.
6035 Hi = DAG.getConstant(0, NVT);
6036 break;
6037
6038 case ISD::TRUNCATE: {
6039 // The input value must be larger than this value. Expand *it*.
Dan Gohman8181bd12008-07-27 21:46:04 +00006040 SDValue NewLo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006041 ExpandOp(Node->getOperand(0), NewLo, Hi);
6042
6043 // The low part is now either the right size, or it is closer. If not the
6044 // right size, make an illegal truncate so we recursively expand it.
6045 if (NewLo.getValueType() != Node->getValueType(0))
6046 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6047 ExpandOp(NewLo, Lo, Hi);
6048 break;
6049 }
6050
6051 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006052 SDValue Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006053 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6054 // If the target wants to, allow it to lower this itself.
6055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6056 case Expand: assert(0 && "cannot expand FP!");
6057 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6058 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6059 }
6060 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6061 }
6062
6063 // f32 / f64 must be expanded to i32 / i64.
6064 if (VT == MVT::f32 || VT == MVT::f64) {
6065 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6066 if (getTypeAction(NVT) == Expand)
6067 ExpandOp(Lo, Lo, Hi);
6068 break;
6069 }
6070
6071 // If source operand will be expanded to the same type as VT, i.e.
6072 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands92c43912008-06-06 12:08:01 +00006073 MVT VT0 = Node->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006074 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6075 ExpandOp(Node->getOperand(0), Lo, Hi);
6076 break;
6077 }
6078
6079 // Turn this into a load/store pair by default.
6080 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00006081 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006082
6083 ExpandOp(Tmp, Lo, Hi);
6084 break;
6085 }
6086
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006087 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006088 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6089 TargetLowering::Custom &&
6090 "Must custom expand ReadCycleCounter");
Dan Gohman8181bd12008-07-27 21:46:04 +00006091 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006092 assert(Tmp.Val && "Node must be custom expanded!");
6093 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006094 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006095 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006096 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006097 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006098
Mon P Wang6bde9ec2008-06-25 08:15:39 +00006099 case ISD::ATOMIC_CMP_SWAP: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006100 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00006101 assert(Tmp.Val && "Node must be custom expanded!");
6102 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman8181bd12008-07-27 21:46:04 +00006103 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Andrew Lenharth81580822008-03-05 01:15:49 +00006104 LegalizeOp(Tmp.getValue(1)));
6105 break;
6106 }
6107
6108
6109
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 // These operators cannot be expanded directly, emit them as calls to
6111 // library functions.
6112 case ISD::FP_TO_SINT: {
6113 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006114 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6116 case Expand: assert(0 && "cannot expand FP!");
6117 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6118 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6119 }
6120
6121 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6122
6123 // Now that the custom expander is done, expand the result, which is still
6124 // VT.
6125 if (Op.Val) {
6126 ExpandOp(Op, Lo, Hi);
6127 break;
6128 }
6129 }
6130
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006131 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6132 VT);
6133 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6134 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006135 break;
6136 }
6137
6138 case ISD::FP_TO_UINT: {
6139 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006140 SDValue Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006141 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6142 case Expand: assert(0 && "cannot expand FP!");
6143 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6144 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6145 }
6146
6147 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6148
6149 // Now that the custom expander is done, expand the result.
6150 if (Op.Val) {
6151 ExpandOp(Op, Lo, Hi);
6152 break;
6153 }
6154 }
6155
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006156 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6157 VT);
6158 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6159 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006160 break;
6161 }
6162
6163 case ISD::SHL: {
6164 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006165 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006166 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006167 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006168 Op = TLI.LowerOperation(Op, DAG);
6169 if (Op.Val) {
6170 // Now that the custom expander is done, expand the result, which is
6171 // still VT.
6172 ExpandOp(Op, Lo, Hi);
6173 break;
6174 }
6175 }
6176
6177 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6178 // this X << 1 as X+X.
6179 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00006180 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006182 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006183 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6184 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6185 LoOps[1] = LoOps[0];
6186 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6187
6188 HiOps[1] = HiOps[0];
6189 HiOps[2] = Lo.getValue(1);
6190 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6191 break;
6192 }
6193 }
6194
6195 // If we can emit an efficient shift operation, do so now.
6196 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6197 break;
6198
6199 // If this target supports SHL_PARTS, use it.
6200 TargetLowering::LegalizeAction Action =
6201 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6202 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6203 Action == TargetLowering::Custom) {
6204 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6205 break;
6206 }
6207
6208 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006209 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 break;
6211 }
6212
6213 case ISD::SRA: {
6214 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006215 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006216 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006217 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006218 Op = TLI.LowerOperation(Op, DAG);
6219 if (Op.Val) {
6220 // Now that the custom expander is done, expand the result, which is
6221 // still VT.
6222 ExpandOp(Op, Lo, Hi);
6223 break;
6224 }
6225 }
6226
6227 // If we can emit an efficient shift operation, do so now.
6228 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
6229 break;
6230
6231 // If this target supports SRA_PARTS, use it.
6232 TargetLowering::LegalizeAction Action =
6233 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6234 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6235 Action == TargetLowering::Custom) {
6236 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6237 break;
6238 }
6239
6240 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006241 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006242 break;
6243 }
6244
6245 case ISD::SRL: {
6246 // If the target wants custom lowering, do so.
Dan Gohman8181bd12008-07-27 21:46:04 +00006247 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006248 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006249 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250 Op = TLI.LowerOperation(Op, DAG);
6251 if (Op.Val) {
6252 // Now that the custom expander is done, expand the result, which is
6253 // still VT.
6254 ExpandOp(Op, Lo, Hi);
6255 break;
6256 }
6257 }
6258
6259 // If we can emit an efficient shift operation, do so now.
6260 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
6261 break;
6262
6263 // If this target supports SRL_PARTS, use it.
6264 TargetLowering::LegalizeAction Action =
6265 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6266 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6267 Action == TargetLowering::Custom) {
6268 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
6269 break;
6270 }
6271
6272 // Otherwise, emit a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006273 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006274 break;
6275 }
6276
6277 case ISD::ADD:
6278 case ISD::SUB: {
6279 // If the target wants to custom expand this, let them.
6280 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6281 TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006282 SDValue Result = TLI.LowerOperation(Op, DAG);
Duncan Sands4c3885b2008-06-22 09:42:16 +00006283 if (Result.Val) {
6284 ExpandOp(Result, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006285 break;
6286 }
6287 }
6288
6289 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006290 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006291 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6292 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6293 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006294 SDValue LoOps[2], HiOps[3];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006295 LoOps[0] = LHSL;
6296 LoOps[1] = RHSL;
6297 HiOps[0] = LHSH;
6298 HiOps[1] = RHSH;
6299 if (Node->getOpcode() == ISD::ADD) {
6300 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6301 HiOps[2] = Lo.getValue(1);
6302 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6303 } else {
6304 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6305 HiOps[2] = Lo.getValue(1);
6306 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6307 }
6308 break;
6309 }
6310
6311 case ISD::ADDC:
6312 case ISD::SUBC: {
6313 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006314 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006315 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6316 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6317 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006318 SDValue LoOps[2] = { LHSL, RHSL };
6319 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006320
6321 if (Node->getOpcode() == ISD::ADDC) {
6322 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6323 HiOps[2] = Lo.getValue(1);
6324 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6325 } else {
6326 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6327 HiOps[2] = Lo.getValue(1);
6328 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6329 }
6330 // Remember that we legalized the flag.
6331 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6332 break;
6333 }
6334 case ISD::ADDE:
6335 case ISD::SUBE: {
6336 // Expand the subcomponents.
Dan Gohman8181bd12008-07-27 21:46:04 +00006337 SDValue LHSL, LHSH, RHSL, RHSH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006338 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6339 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6340 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006341 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6342 SDValue HiOps[3] = { LHSH, RHSH };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343
6344 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6345 HiOps[2] = Lo.getValue(1);
6346 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6347
6348 // Remember that we legalized the flag.
6349 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6350 break;
6351 }
6352 case ISD::MUL: {
6353 // If the target wants to custom expand this, let them.
6354 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006355 SDValue New = TLI.LowerOperation(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006356 if (New.Val) {
6357 ExpandOp(New, Lo, Hi);
6358 break;
6359 }
6360 }
6361
6362 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6363 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006364 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6365 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6366 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006367 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006368 ExpandOp(Node->getOperand(0), LL, LH);
6369 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman07961cd2008-02-25 21:11:39 +00006370 unsigned OuterBitSize = Op.getValueSizeInBits();
6371 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman5a199552007-10-08 18:33:35 +00006372 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6373 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2594d942008-03-10 20:42:19 +00006374 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6375 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6376 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman5a199552007-10-08 18:33:35 +00006377 // The inputs are both zero-extended.
6378 if (HasUMUL_LOHI) {
6379 // We can emit a umul_lohi.
6380 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Dan Gohman8181bd12008-07-27 21:46:04 +00006381 Hi = SDValue(Lo.Val, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006382 break;
6383 }
6384 if (HasMULHU) {
6385 // We can emit a mulhu+mul.
6386 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6387 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6388 break;
6389 }
Dan Gohman5a199552007-10-08 18:33:35 +00006390 }
Dan Gohman07961cd2008-02-25 21:11:39 +00006391 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman5a199552007-10-08 18:33:35 +00006392 // The input values are both sign-extended.
6393 if (HasSMUL_LOHI) {
6394 // We can emit a smul_lohi.
6395 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Dan Gohman8181bd12008-07-27 21:46:04 +00006396 Hi = SDValue(Lo.Val, 1);
Dan Gohman5a199552007-10-08 18:33:35 +00006397 break;
6398 }
6399 if (HasMULHS) {
6400 // We can emit a mulhs+mul.
6401 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6402 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6403 break;
6404 }
6405 }
6406 if (HasUMUL_LOHI) {
6407 // Lo,Hi = umul LHS, RHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00006408 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman5a199552007-10-08 18:33:35 +00006409 DAG.getVTList(NVT, NVT), LL, RL);
6410 Lo = UMulLOHI;
6411 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006412 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6413 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6414 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6415 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6416 break;
6417 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006418 if (HasMULHU) {
6419 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6420 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6421 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6422 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6423 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6424 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6425 break;
6426 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006427 }
6428
Dan Gohman5a199552007-10-08 18:33:35 +00006429 // If nothing else, we can make a libcall.
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006430 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 break;
6432 }
6433 case ISD::SDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006434 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435 break;
6436 case ISD::UDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006437 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006438 break;
6439 case ISD::SREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006440 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006441 break;
6442 case ISD::UREM:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006443 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 break;
6445
6446 case ISD::FADD:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006447 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6448 RTLIB::ADD_F64,
6449 RTLIB::ADD_F80,
6450 RTLIB::ADD_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 Node, false, Hi);
6452 break;
6453 case ISD::FSUB:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006454 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6455 RTLIB::SUB_F64,
6456 RTLIB::SUB_F80,
6457 RTLIB::SUB_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 Node, false, Hi);
6459 break;
6460 case ISD::FMUL:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006461 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6462 RTLIB::MUL_F64,
6463 RTLIB::MUL_F80,
6464 RTLIB::MUL_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006465 Node, false, Hi);
6466 break;
6467 case ISD::FDIV:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006468 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6469 RTLIB::DIV_F64,
6470 RTLIB::DIV_F80,
6471 RTLIB::DIV_PPCF128),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 Node, false, Hi);
6473 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006474 case ISD::FP_EXTEND: {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006475 if (VT == MVT::ppcf128) {
6476 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6477 Node->getOperand(0).getValueType()==MVT::f64);
6478 const uint64_t zero = 0;
6479 if (Node->getOperand(0).getValueType()==MVT::f32)
6480 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6481 else
6482 Hi = Node->getOperand(0);
6483 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6484 break;
6485 }
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006486 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6487 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6488 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006489 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006490 }
6491 case ISD::FP_ROUND: {
6492 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6493 VT);
6494 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6495 Lo = ExpandLibCall(LC, Node, true, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006496 break;
Duncan Sandsf68dffb2008-07-17 02:36:29 +00006497 }
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006498 case ISD::FPOWI:
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006499 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32,
6500 RTLIB::POWI_F64,
6501 RTLIB::POWI_F80,
6502 RTLIB::POWI_PPCF128),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006503 Node, false, Hi);
6504 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 case ISD::FSQRT:
6506 case ISD::FSIN:
6507 case ISD::FCOS: {
6508 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6509 switch(Node->getOpcode()) {
6510 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006511 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6512 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006513 break;
6514 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006515 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6516 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517 break;
6518 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006519 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6520 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006521 break;
6522 default: assert(0 && "Unreachable!");
6523 }
Duncan Sandsf1db7c82008-04-12 17:14:18 +00006524 Lo = ExpandLibCall(LC, Node, false, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006525 break;
6526 }
6527 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006528 if (VT == MVT::ppcf128) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006529 SDValue Tmp;
Dale Johannesen5707ef82007-10-12 19:02:17 +00006530 ExpandOp(Node->getOperand(0), Lo, Tmp);
6531 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6532 // lo = hi==fabs(hi) ? lo : -lo;
6533 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6534 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6535 DAG.getCondCode(ISD::SETEQ));
6536 break;
6537 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006538 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006539 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6540 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6541 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6542 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6543 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6544 if (getTypeAction(NVT) == Expand)
6545 ExpandOp(Lo, Lo, Hi);
6546 break;
6547 }
6548 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006549 if (VT == MVT::ppcf128) {
6550 ExpandOp(Node->getOperand(0), Lo, Hi);
6551 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6552 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6553 break;
6554 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006555 SDValue Mask = (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006556 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6557 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6558 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6559 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6560 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6561 if (getTypeAction(NVT) == Expand)
6562 ExpandOp(Lo, Lo, Hi);
6563 break;
6564 }
6565 case ISD::FCOPYSIGN: {
6566 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6567 if (getTypeAction(NVT) == Expand)
6568 ExpandOp(Lo, Lo, Hi);
6569 break;
6570 }
6571 case ISD::SINT_TO_FP:
6572 case ISD::UINT_TO_FP: {
6573 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands92c43912008-06-06 12:08:01 +00006574 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6a779c82008-03-18 17:28:38 +00006575
6576 // Promote the operand if needed. Do this before checking for
6577 // ppcf128 so conversions of i16 and i8 work.
6578 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006579 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen6a779c82008-03-18 17:28:38 +00006580 Tmp = isSigned
6581 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6582 DAG.getValueType(SrcVT))
6583 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6584 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6585 SrcVT = Node->getOperand(0).getValueType();
6586 }
6587
Dan Gohmanec51f642008-03-10 23:03:31 +00006588 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman84d00962008-02-25 21:39:34 +00006589 static const uint64_t zero = 0;
Dale Johannesen4c14d512007-10-12 01:37:08 +00006590 if (isSigned) {
6591 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6592 Node->getOperand(0)));
6593 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6594 } else {
Dan Gohman84d00962008-02-25 21:39:34 +00006595 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen4c14d512007-10-12 01:37:08 +00006596 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6597 Node->getOperand(0)));
6598 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6599 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006600 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006601 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6602 DAG.getConstant(0, MVT::i32),
6603 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6604 DAG.getConstantFP(
6605 APFloat(APInt(128, 2, TwoE32)),
6606 MVT::ppcf128)),
6607 Hi,
6608 DAG.getCondCode(ISD::SETLT)),
6609 Lo, Hi);
6610 }
6611 break;
6612 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006613 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6614 // si64->ppcf128 done by libcall, below
Dan Gohman84d00962008-02-25 21:39:34 +00006615 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006616 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6617 Lo, Hi);
6618 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6619 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6620 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6621 DAG.getConstant(0, MVT::i64),
6622 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6623 DAG.getConstantFP(
6624 APFloat(APInt(128, 2, TwoE64)),
6625 MVT::ppcf128)),
6626 Hi,
6627 DAG.getCondCode(ISD::SETLT)),
6628 Lo, Hi);
6629 break;
6630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006631
Dan Gohmanec51f642008-03-10 23:03:31 +00006632 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6633 Node->getOperand(0));
Evan Chenga8740032008-04-01 01:50:16 +00006634 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng4a2f6df2008-04-01 01:51:26 +00006635 // float to i32 etc. can be 'expanded' to a single node.
Evan Chenga8740032008-04-01 01:50:16 +00006636 ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006637 break;
6638 }
6639 }
6640
6641 // Make sure the resultant values have been legalized themselves, unless this
6642 // is a type that requires multi-step expansion.
6643 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6644 Lo = LegalizeOp(Lo);
6645 if (Hi.Val)
6646 // Don't legalize the high part if it is expanded to a single node.
6647 Hi = LegalizeOp(Hi);
6648 }
6649
6650 // Remember in a map if the values will be reused later.
Dan Gohman55d19662008-07-07 17:46:23 +00006651 bool isNew =
6652 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006653 assert(isNew && "Value already expanded?!?");
6654}
6655
6656/// SplitVectorOp - Given an operand of vector type, break it down into
6657/// two smaller values, still of vector type.
Dan Gohman8181bd12008-07-27 21:46:04 +00006658void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
6659 SDValue &Hi) {
Duncan Sands92c43912008-06-06 12:08:01 +00006660 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006661 SDNode *Node = Op.Val;
Duncan Sands92c43912008-06-06 12:08:01 +00006662 unsigned NumElements = Op.getValueType().getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006663 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006664
Duncan Sands92c43912008-06-06 12:08:01 +00006665 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman4a365ad2007-11-15 21:15:26 +00006666
6667 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6668 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6669
Duncan Sands92c43912008-06-06 12:08:01 +00006670 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
6671 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006672
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006673 // See if we already split it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006674 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006675 = SplitNodes.find(Op);
6676 if (I != SplitNodes.end()) {
6677 Lo = I->second.first;
6678 Hi = I->second.second;
6679 return;
6680 }
6681
6682 switch (Node->getOpcode()) {
6683 default:
6684#ifndef NDEBUG
6685 Node->dump(&DAG);
6686#endif
6687 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006688 case ISD::UNDEF:
6689 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6690 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6691 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006692 case ISD::BUILD_PAIR:
6693 Lo = Node->getOperand(0);
6694 Hi = Node->getOperand(1);
6695 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006696 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman7c9e4b72008-04-25 18:07:40 +00006697 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
6698 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6699 unsigned Index = Idx->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00006700 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman7c9e4b72008-04-25 18:07:40 +00006701 if (Index < NewNumElts_Lo)
6702 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
6703 DAG.getIntPtrConstant(Index));
6704 else
6705 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6706 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
6707 break;
6708 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006709 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman7c9e4b72008-04-25 18:07:40 +00006710 Node->getOperand(1),
6711 Node->getOperand(2));
6712 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006713 break;
6714 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006715 case ISD::VECTOR_SHUFFLE: {
6716 // Build the low part.
Dan Gohman8181bd12008-07-27 21:46:04 +00006717 SDValue Mask = Node->getOperand(2);
6718 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +00006719 MVT PtrVT = TLI.getPointerTy();
Chris Lattner587c46d2007-11-19 21:16:54 +00006720
6721 // Insert all of the elements from the input that are needed. We use
6722 // buildvector of extractelement here because the input vectors will have
6723 // to be legalized, so this makes the code simpler.
6724 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006725 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006726 if (IdxNode.getOpcode() == ISD::UNDEF) {
6727 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6728 continue;
6729 }
6730 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00006731 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00006732 if (Idx >= NumElements) {
6733 InVec = Node->getOperand(1);
6734 Idx -= NumElements;
6735 }
6736 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6737 DAG.getConstant(Idx, PtrVT)));
6738 }
6739 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6740 Ops.clear();
6741
6742 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006743 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman8bb3cb32008-03-14 00:53:31 +00006744 if (IdxNode.getOpcode() == ISD::UNDEF) {
6745 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6746 continue;
6747 }
6748 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00006749 SDValue InVec = Node->getOperand(0);
Chris Lattner587c46d2007-11-19 21:16:54 +00006750 if (Idx >= NumElements) {
6751 InVec = Node->getOperand(1);
6752 Idx -= NumElements;
6753 }
6754 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6755 DAG.getConstant(Idx, PtrVT)));
6756 }
Mon P Wang2e89b112008-07-25 01:30:26 +00006757 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner587c46d2007-11-19 21:16:54 +00006758 break;
6759 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760 case ISD::BUILD_VECTOR: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006761 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006762 Node->op_begin()+NewNumElts_Lo);
6763 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006764
Dan Gohman8181bd12008-07-27 21:46:04 +00006765 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006767 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768 break;
6769 }
6770 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006771 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006772 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6773 if (NewNumSubvectors == 1) {
6774 Lo = Node->getOperand(0);
6775 Hi = Node->getOperand(1);
6776 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00006777 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006778 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006779 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006780
Dan Gohman8181bd12008-07-27 21:46:04 +00006781 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006782 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006783 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006784 }
6785 break;
6786 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006787 case ISD::SELECT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006788 SDValue Cond = Node->getOperand(0);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006789
Dan Gohman8181bd12008-07-27 21:46:04 +00006790 SDValue LL, LH, RL, RH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00006791 SplitVectorOp(Node->getOperand(1), LL, LH);
6792 SplitVectorOp(Node->getOperand(2), RL, RH);
6793
Duncan Sands92c43912008-06-06 12:08:01 +00006794 if (Cond.getValueType().isVector()) {
Dan Gohmand5d4c872007-10-17 14:48:28 +00006795 // Handle a vector merge.
Dan Gohman8181bd12008-07-27 21:46:04 +00006796 SDValue CL, CH;
Dan Gohmand5d4c872007-10-17 14:48:28 +00006797 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006798 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6799 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006800 } else {
6801 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006802 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6803 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006804 }
6805 break;
6806 }
Chris Lattnerc7471452008-06-30 02:43:01 +00006807 case ISD::SELECT_CC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006808 SDValue CondLHS = Node->getOperand(0);
6809 SDValue CondRHS = Node->getOperand(1);
6810 SDValue CondCode = Node->getOperand(4);
Chris Lattnerc7471452008-06-30 02:43:01 +00006811
Dan Gohman8181bd12008-07-27 21:46:04 +00006812 SDValue LL, LH, RL, RH;
Chris Lattnerc7471452008-06-30 02:43:01 +00006813 SplitVectorOp(Node->getOperand(2), LL, LH);
6814 SplitVectorOp(Node->getOperand(3), RL, RH);
6815
6816 // Handle a simple select with vector operands.
6817 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
6818 LL, RL, CondCode);
6819 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
6820 LH, RH, CondCode);
6821 break;
6822 }
Nate Begeman9a1ce152008-05-12 19:40:03 +00006823 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006824 SDValue LL, LH, RL, RH;
Nate Begeman9a1ce152008-05-12 19:40:03 +00006825 SplitVectorOp(Node->getOperand(0), LL, LH);
6826 SplitVectorOp(Node->getOperand(1), RL, RH);
6827 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
6828 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
6829 break;
6830 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006831 case ISD::ADD:
6832 case ISD::SUB:
6833 case ISD::MUL:
6834 case ISD::FADD:
6835 case ISD::FSUB:
6836 case ISD::FMUL:
6837 case ISD::SDIV:
6838 case ISD::UDIV:
6839 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006840 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006841 case ISD::AND:
6842 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006843 case ISD::XOR:
6844 case ISD::UREM:
6845 case ISD::SREM:
6846 case ISD::FREM: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006847 SDValue LL, LH, RL, RH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006848 SplitVectorOp(Node->getOperand(0), LL, LH);
6849 SplitVectorOp(Node->getOperand(1), RL, RH);
6850
Nate Begeman4a365ad2007-11-15 21:15:26 +00006851 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6852 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006853 break;
6854 }
Dan Gohman29c3cef2008-08-14 20:04:46 +00006855 case ISD::FP_ROUND:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006856 case ISD::FPOWI: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006857 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00006858 SplitVectorOp(Node->getOperand(0), L, H);
6859
Nate Begeman4a365ad2007-11-15 21:15:26 +00006860 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6861 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006862 break;
6863 }
6864 case ISD::CTTZ:
6865 case ISD::CTLZ:
6866 case ISD::CTPOP:
6867 case ISD::FNEG:
6868 case ISD::FABS:
6869 case ISD::FSQRT:
6870 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006871 case ISD::FCOS:
6872 case ISD::FP_TO_SINT:
6873 case ISD::FP_TO_UINT:
6874 case ISD::SINT_TO_FP:
Dan Gohman29c3cef2008-08-14 20:04:46 +00006875 case ISD::UINT_TO_FP:
6876 case ISD::TRUNCATE:
6877 case ISD::ANY_EXTEND:
6878 case ISD::SIGN_EXTEND:
6879 case ISD::ZERO_EXTEND:
6880 case ISD::FP_EXTEND: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006881 SDValue L, H;
Dan Gohman6d05cac2007-10-11 23:57:53 +00006882 SplitVectorOp(Node->getOperand(0), L, H);
6883
Nate Begeman4a365ad2007-11-15 21:15:26 +00006884 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6885 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006886 break;
6887 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006888 case ISD::LOAD: {
6889 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00006890 SDValue Ch = LD->getChain();
6891 SDValue Ptr = LD->getBasePtr();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006892 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006893 const Value *SV = LD->getSrcValue();
6894 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00006895 MVT MemoryVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006896 unsigned Alignment = LD->getAlignment();
6897 bool isVolatile = LD->isVolatile();
6898
Dan Gohman29c3cef2008-08-14 20:04:46 +00006899 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
6900 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
6901
6902 MVT MemNewEltVT = MemoryVT.getVectorElementType();
6903 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
6904 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
6905
6906 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
6907 NewVT_Lo, Ch, Ptr, Offset,
6908 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
6909 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006910 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner5872a362008-01-17 07:00:52 +00006911 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006912 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006913 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman29c3cef2008-08-14 20:04:46 +00006914 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
6915 NewVT_Hi, Ch, Ptr, Offset,
6916 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006917
6918 // Build a factor node to remember that this load is independent of the
6919 // other one.
Dan Gohman8181bd12008-07-27 21:46:04 +00006920 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006921 Hi.getValue(1));
6922
6923 // Remember that we legalized the chain.
6924 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6925 break;
6926 }
6927 case ISD::BIT_CONVERT: {
6928 // We know the result is a vector. The input may be either a vector or a
6929 // scalar value.
Dan Gohman8181bd12008-07-27 21:46:04 +00006930 SDValue InOp = Node->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00006931 if (!InOp.getValueType().isVector() ||
6932 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006933 // The input is a scalar or single-element vector.
6934 // Lower to a store/load so that it can be split.
6935 // FIXME: this could be improved probably.
Mon P Wang36b59ac2008-07-15 05:28:34 +00006936 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
6937 Op.getValueType().getTypeForMVT());
Dan Gohman8181bd12008-07-27 21:46:04 +00006938 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Dan Gohman1fc34bc2008-07-11 22:44:52 +00006939 int FI = cast<FrameIndexSDNode>(Ptr.Val)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006940
Dan Gohman8181bd12008-07-27 21:46:04 +00006941 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman12a9c082008-02-06 22:27:42 +00006942 InOp, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00006943 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00006944 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00006945 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006946 }
6947 // Split the vector and convert each of the pieces now.
6948 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006949 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6950 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006951 break;
6952 }
6953 }
6954
6955 // Remember in a map if the values will be reused later.
6956 bool isNew =
6957 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6958 assert(isNew && "Value already split?!?");
6959}
6960
6961
6962/// ScalarizeVectorOp - Given an operand of single-element vector type
6963/// (e.g. v1f32), convert it into the equivalent operation that returns a
6964/// scalar (e.g. f32) value.
Dan Gohman8181bd12008-07-27 21:46:04 +00006965SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands92c43912008-06-06 12:08:01 +00006966 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006967 SDNode *Node = Op.Val;
Duncan Sands92c43912008-06-06 12:08:01 +00006968 MVT NewVT = Op.getValueType().getVectorElementType();
6969 assert(Op.getValueType().getVectorNumElements() == 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006970
6971 // See if we already scalarized it.
Dan Gohman8181bd12008-07-27 21:46:04 +00006972 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006973 if (I != ScalarizedNodes.end()) return I->second;
6974
Dan Gohman8181bd12008-07-27 21:46:04 +00006975 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006976 switch (Node->getOpcode()) {
6977 default:
6978#ifndef NDEBUG
6979 Node->dump(&DAG); cerr << "\n";
6980#endif
6981 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6982 case ISD::ADD:
6983 case ISD::FADD:
6984 case ISD::SUB:
6985 case ISD::FSUB:
6986 case ISD::MUL:
6987 case ISD::FMUL:
6988 case ISD::SDIV:
6989 case ISD::UDIV:
6990 case ISD::FDIV:
6991 case ISD::SREM:
6992 case ISD::UREM:
6993 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006994 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006995 case ISD::AND:
6996 case ISD::OR:
6997 case ISD::XOR:
6998 Result = DAG.getNode(Node->getOpcode(),
6999 NewVT,
7000 ScalarizeVectorOp(Node->getOperand(0)),
7001 ScalarizeVectorOp(Node->getOperand(1)));
7002 break;
7003 case ISD::FNEG:
7004 case ISD::FABS:
7005 case ISD::FSQRT:
7006 case ISD::FSIN:
7007 case ISD::FCOS:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007008 case ISD::FP_TO_SINT:
7009 case ISD::FP_TO_UINT:
7010 case ISD::SINT_TO_FP:
7011 case ISD::UINT_TO_FP:
7012 case ISD::SIGN_EXTEND:
7013 case ISD::ZERO_EXTEND:
7014 case ISD::ANY_EXTEND:
7015 case ISD::TRUNCATE:
7016 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007017 Result = DAG.getNode(Node->getOpcode(),
7018 NewVT,
7019 ScalarizeVectorOp(Node->getOperand(0)));
7020 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007021 case ISD::FPOWI:
Dan Gohman29c3cef2008-08-14 20:04:46 +00007022 case ISD::FP_ROUND:
Dan Gohmanae4c2f82007-10-12 14:13:46 +00007023 Result = DAG.getNode(Node->getOpcode(),
7024 NewVT,
7025 ScalarizeVectorOp(Node->getOperand(0)),
7026 Node->getOperand(1));
7027 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007028 case ISD::LOAD: {
7029 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman8181bd12008-07-27 21:46:04 +00007030 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7031 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman29c3cef2008-08-14 20:04:46 +00007032 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007033 const Value *SV = LD->getSrcValue();
7034 int SVOffset = LD->getSrcValueOffset();
Dan Gohman29c3cef2008-08-14 20:04:46 +00007035 MVT MemoryVT = LD->getMemoryVT();
7036 unsigned Alignment = LD->getAlignment();
7037 bool isVolatile = LD->isVolatile();
7038
7039 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7040 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7041
7042 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7043 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7044 MemoryVT.getVectorElementType(),
7045 isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007046
7047 // Remember that we legalized the chain.
7048 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7049 break;
7050 }
7051 case ISD::BUILD_VECTOR:
7052 Result = Node->getOperand(0);
7053 break;
7054 case ISD::INSERT_VECTOR_ELT:
7055 // Returning the inserted scalar element.
7056 Result = Node->getOperand(1);
7057 break;
7058 case ISD::CONCAT_VECTORS:
7059 assert(Node->getOperand(0).getValueType() == NewVT &&
7060 "Concat of non-legal vectors not yet supported!");
7061 Result = Node->getOperand(0);
7062 break;
7063 case ISD::VECTOR_SHUFFLE: {
7064 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00007065 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007066 if (cast<ConstantSDNode>(EltNum)->getValue())
7067 Result = ScalarizeVectorOp(Node->getOperand(1));
7068 else
7069 Result = ScalarizeVectorOp(Node->getOperand(0));
7070 break;
7071 }
7072 case ISD::EXTRACT_SUBVECTOR:
7073 Result = Node->getOperand(0);
7074 assert(Result.getValueType() == NewVT);
7075 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007076 case ISD::BIT_CONVERT: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007077 SDValue Op0 = Op.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00007078 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng2cc16e72008-05-16 17:19:05 +00007079 Op0 = ScalarizeVectorOp(Op0);
7080 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007081 break;
Evan Cheng2cc16e72008-05-16 17:19:05 +00007082 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007083 case ISD::SELECT:
7084 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
7085 ScalarizeVectorOp(Op.getOperand(1)),
7086 ScalarizeVectorOp(Op.getOperand(2)));
7087 break;
Chris Lattnerc7471452008-06-30 02:43:01 +00007088 case ISD::SELECT_CC:
7089 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7090 Node->getOperand(1),
7091 ScalarizeVectorOp(Op.getOperand(2)),
7092 ScalarizeVectorOp(Op.getOperand(3)),
7093 Node->getOperand(4));
7094 break;
Nate Begeman78ca4f92008-05-12 23:09:43 +00007095 case ISD::VSETCC: {
Dan Gohman8181bd12008-07-27 21:46:04 +00007096 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7097 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman78ca4f92008-05-12 23:09:43 +00007098 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7099 Op.getOperand(2));
7100 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7101 DAG.getConstant(-1ULL, NewVT),
7102 DAG.getConstant(0ULL, NewVT));
7103 break;
7104 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007105 }
7106
7107 if (TLI.isTypeLegal(NewVT))
7108 Result = LegalizeOp(Result);
7109 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7110 assert(isNew && "Value already scalarized?");
7111 return Result;
7112}
7113
7114
7115// SelectionDAG::Legalize - This is the entry point for the file.
7116//
7117void SelectionDAG::Legalize() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007118 /// run - This is the main entry point to this class.
7119 ///
7120 SelectionDAGLegalize(*this).LegalizeDAG();
7121}
7122