blob: 6fe5249af1625d12ac974782c2c1b249a2d29063 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000017#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000018#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000019#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000020#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000022#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/CommandLine.h"
Chris Lattner360e8202006-06-28 21:58:30 +000024#include "llvm/Support/Visibility.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000025#include <iostream>
Evan Cheng033e6812006-03-24 01:17:21 +000026#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000027using namespace llvm;
28
Chris Lattner5e46a192006-04-02 03:07:27 +000029#ifndef NDEBUG
30static cl::opt<bool>
31ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
32 cl::desc("Pop up a window to show dags before legalize"));
33#else
34static const bool ViewLegalizeDAGs = 0;
35#endif
36
Chris Lattner3e928bb2005-01-07 07:47:09 +000037//===----------------------------------------------------------------------===//
38/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
39/// hacks on it until the target machine can handle it. This involves
40/// eliminating value sizes the machine cannot handle (promoting small sizes to
41/// large sizes or splitting up large values into small values) as well as
42/// eliminating operations the machine cannot handle.
43///
44/// This code also does a small amount of optimization and recognition of idioms
45/// as part of its processing. For example, if a target does not support a
46/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
47/// will attempt merge setcc and brc instructions into brcc's.
48///
49namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000050class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000051 TargetLowering &TLI;
52 SelectionDAG &DAG;
53
Chris Lattner6831a812006-02-13 09:18:02 +000054 // Libcall insertion helpers.
55
56 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
57 /// legalized. We use this to ensure that calls are properly serialized
58 /// against each other, including inserted libcalls.
59 SDOperand LastCALLSEQ_END;
60
61 /// IsLegalizingCall - This member is used *only* for purposes of providing
62 /// helpful assertions that a libcall isn't created while another call is
63 /// being legalized (which could lead to non-serialized call sequences).
64 bool IsLegalizingCall;
65
Chris Lattner3e928bb2005-01-07 07:47:09 +000066 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000067 Legal, // The target natively supports this operation.
68 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000069 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000070 };
Chris Lattner6831a812006-02-13 09:18:02 +000071
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 /// ValueTypeActions - This is a bitvector that contains two bits for each
73 /// value type, where the two bits correspond to the LegalizeAction enum.
74 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000075 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000076
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 /// LegalizedNodes - For nodes that are of legal width, and that have more
78 /// than one use, this map indicates what regularized operand to use. This
79 /// allows us to avoid legalizing the same thing more than once.
80 std::map<SDOperand, SDOperand> LegalizedNodes;
81
Chris Lattner03c85462005-01-15 05:21:40 +000082 /// PromotedNodes - For nodes that are below legal width, and that have more
83 /// than one use, this map indicates what promoted value to use. This allows
84 /// us to avoid promoting the same thing more than once.
85 std::map<SDOperand, SDOperand> PromotedNodes;
86
Chris Lattnerc7029802006-03-18 01:44:44 +000087 /// ExpandedNodes - For nodes that need to be expanded this map indicates
88 /// which which operands are the expanded version of the input. This allows
89 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000090 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
91
Chris Lattnerc7029802006-03-18 01:44:44 +000092 /// SplitNodes - For vector nodes that need to be split, this map indicates
93 /// which which operands are the split version of the input. This allows us
94 /// to avoid splitting the same node more than once.
95 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
96
97 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
98 /// concrete packed types, this contains the mapping of ones we have already
99 /// processed to the result.
100 std::map<SDOperand, SDOperand> PackedNodes;
101
Chris Lattner8afc48e2005-01-07 22:28:47 +0000102 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000103 LegalizedNodes.insert(std::make_pair(From, To));
104 // If someone requests legalization of the new node, return itself.
105 if (From != To)
106 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000107 }
Chris Lattner03c85462005-01-15 05:21:40 +0000108 void AddPromotedOperand(SDOperand From, SDOperand To) {
109 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
110 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000111 // If someone requests legalization of the new node, return itself.
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000113 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000114
Chris Lattner3e928bb2005-01-07 07:47:09 +0000115public:
116
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000117 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000118
Chris Lattner3e928bb2005-01-07 07:47:09 +0000119 /// 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.
122 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000123 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124 }
125
126 /// isTypeLegal - Return true if this type is legal on this target.
127 ///
128 bool isTypeLegal(MVT::ValueType VT) const {
129 return getTypeAction(VT) == Legal;
130 }
131
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 void LegalizeDAG();
133
Chris Lattner456a93a2006-01-28 07:39:30 +0000134private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000135 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
136 /// appropriate for its type.
137 void HandleOp(SDOperand Op);
138
139 /// LegalizeOp - We know that the specified value has a legal type.
140 /// Recursively ensure that the operands have legal types, then return the
141 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000142 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000143
144 /// PromoteOp - Given an operation that produces a value in an invalid type,
145 /// promote it to compute the value into a larger type. The produced value
146 /// will have the correct bits for the low portion of the register, but no
147 /// guarantee is made about the top bits: it may be zero, sign-extended, or
148 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000149 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000150
Chris Lattnerc7029802006-03-18 01:44:44 +0000151 /// ExpandOp - Expand the specified SDOperand into its two component pieces
152 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
153 /// the LegalizeNodes map is filled in for any results that are not expanded,
154 /// the ExpandedNodes map is filled in for any results that are expanded, and
155 /// the Lo/Hi values are returned. This applies to integer types and Vector
156 /// types.
157 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
158
159 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
160 /// two smaller values of MVT::Vector type.
161 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
162
163 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
164 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
165 /// this is called, we know that PackedVT is the right type for the result and
166 /// we know that this type is legal for the target.
167 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
168
Chris Lattner4352cc92006-04-04 17:23:26 +0000169 /// isShuffleLegal - Return true if a vector shuffle is legal with the
170 /// specified mask and type. Targets can specify exactly which masks they
171 /// support and the code generator is tasked with not creating illegal masks.
172 ///
173 /// Note that this will also return true for shuffles that are promoted to a
174 /// different type.
175 ///
176 /// If this is a legal shuffle, this method returns the (possibly promoted)
177 /// build_vector Mask. If it's not a legal shuffle, it returns null.
178 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
179
Chris Lattner6831a812006-02-13 09:18:02 +0000180 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
181
Nate Begeman750ac1b2006-02-01 07:19:44 +0000182 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
183
Chris Lattnerce872152006-03-19 06:31:19 +0000184 SDOperand CreateStackTemporary(MVT::ValueType VT);
185
Chris Lattner77e77a62005-01-21 06:05:23 +0000186 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
187 SDOperand &Hi);
188 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
189 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000190
Chris Lattner35481892005-12-23 00:16:34 +0000191 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000192 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000193 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000194 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
195 SDOperand LegalOp,
196 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000197 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
198 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000199 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
200 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000201
Chris Lattner456a93a2006-01-28 07:39:30 +0000202 SDOperand ExpandBSWAP(SDOperand Op);
203 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000204 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
205 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000206 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
207 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000208
Chris Lattner15972212006-03-31 17:55:51 +0000209 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000210 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000211
Chris Lattner3e928bb2005-01-07 07:47:09 +0000212 SDOperand getIntPtrConstant(uint64_t Val) {
213 return DAG.getConstant(Val, TLI.getPointerTy());
214 }
215};
216}
217
Chris Lattner4352cc92006-04-04 17:23:26 +0000218/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
219/// specified mask and type. Targets can specify exactly which masks they
220/// support and the code generator is tasked with not creating illegal masks.
221///
222/// Note that this will also return true for shuffles that are promoted to a
223/// different type.
224SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
225 SDOperand Mask) const {
226 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
227 default: return 0;
228 case TargetLowering::Legal:
229 case TargetLowering::Custom:
230 break;
231 case TargetLowering::Promote: {
232 // If this is promoted to a different type, convert the shuffle mask and
233 // ask if it is legal in the promoted type!
234 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
235
236 // If we changed # elements, change the shuffle mask.
237 unsigned NumEltsGrowth =
238 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
239 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
240 if (NumEltsGrowth > 1) {
241 // Renumber the elements.
242 std::vector<SDOperand> Ops;
243 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
244 SDOperand InOp = Mask.getOperand(i);
245 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
246 if (InOp.getOpcode() == ISD::UNDEF)
247 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
248 else {
249 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
250 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
251 }
252 }
253 }
254 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, Ops);
255 }
256 VT = NVT;
257 break;
258 }
259 }
260 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
261}
262
Chris Lattnerc7029802006-03-18 01:44:44 +0000263/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
264/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000265static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000266 switch (VecOp) {
267 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000268 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
269 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
270 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
271 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
272 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
273 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
274 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
275 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000276 }
277}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000278
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000279SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
280 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
281 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000282 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000283 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284}
285
Chris Lattner32fca002005-10-06 01:20:27 +0000286/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
287/// not been visited yet and if all of its operands have already been visited.
288static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
289 std::map<SDNode*, unsigned> &Visited) {
290 if (++Visited[N] != N->getNumOperands())
291 return; // Haven't visited all operands yet
292
293 Order.push_back(N);
294
295 if (N->hasOneUse()) { // Tail recurse in common case.
296 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
297 return;
298 }
299
300 // Now that we have N in, add anything that uses it if all of their operands
301 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000302 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
303 ComputeTopDownOrdering(*UI, Order, Visited);
304}
305
Chris Lattner1618beb2005-07-29 00:11:56 +0000306
Chris Lattner3e928bb2005-01-07 07:47:09 +0000307void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000308 LastCALLSEQ_END = DAG.getEntryNode();
309 IsLegalizingCall = false;
310
Chris Lattnerab510a72005-10-02 17:49:46 +0000311 // The legalize process is inherently a bottom-up recursive process (users
312 // legalize their uses before themselves). Given infinite stack space, we
313 // could just start legalizing on the root and traverse the whole graph. In
314 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000315 // blocks. To avoid this problem, compute an ordering of the nodes where each
316 // node is only legalized after all of its operands are legalized.
317 std::map<SDNode*, unsigned> Visited;
318 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000319
Chris Lattner32fca002005-10-06 01:20:27 +0000320 // Compute ordering from all of the leaves in the graphs, those (like the
321 // entry node) that have no operands.
322 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
323 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000324 if (I->getNumOperands() == 0) {
325 Visited[I] = 0 - 1U;
326 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000327 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000328 }
329
Chris Lattnerde202b32005-11-09 23:47:37 +0000330 assert(Order.size() == Visited.size() &&
331 Order.size() ==
332 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000333 "Error: DAG is cyclic!");
334 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000335
Chris Lattnerc7029802006-03-18 01:44:44 +0000336 for (unsigned i = 0, e = Order.size(); i != e; ++i)
337 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000338
339 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000340 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000341 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
342 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343
344 ExpandedNodes.clear();
345 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000346 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000347 SplitNodes.clear();
348 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000349
350 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000351 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352}
353
Chris Lattner6831a812006-02-13 09:18:02 +0000354
355/// FindCallEndFromCallStart - Given a chained node that is part of a call
356/// sequence, find the CALLSEQ_END node that terminates the call sequence.
357static SDNode *FindCallEndFromCallStart(SDNode *Node) {
358 if (Node->getOpcode() == ISD::CALLSEQ_END)
359 return Node;
360 if (Node->use_empty())
361 return 0; // No CallSeqEnd
362
363 // The chain is usually at the end.
364 SDOperand TheChain(Node, Node->getNumValues()-1);
365 if (TheChain.getValueType() != MVT::Other) {
366 // Sometimes it's at the beginning.
367 TheChain = SDOperand(Node, 0);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Otherwise, hunt for it.
370 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
371 if (Node->getValueType(i) == MVT::Other) {
372 TheChain = SDOperand(Node, i);
373 break;
374 }
375
376 // Otherwise, we walked into a node without a chain.
377 if (TheChain.getValueType() != MVT::Other)
378 return 0;
379 }
380 }
381
382 for (SDNode::use_iterator UI = Node->use_begin(),
383 E = Node->use_end(); UI != E; ++UI) {
384
385 // Make sure to only follow users of our token chain.
386 SDNode *User = *UI;
387 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
388 if (User->getOperand(i) == TheChain)
389 if (SDNode *Result = FindCallEndFromCallStart(User))
390 return Result;
391 }
392 return 0;
393}
394
395/// FindCallStartFromCallEnd - Given a chained node that is part of a call
396/// sequence, find the CALLSEQ_START node that initiates the call sequence.
397static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
398 assert(Node && "Didn't find callseq_start for a call??");
399 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
400
401 assert(Node->getOperand(0).getValueType() == MVT::Other &&
402 "Node doesn't have a token chain argument!");
403 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
404}
405
406/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
407/// see if any uses can reach Dest. If no dest operands can get to dest,
408/// legalize them, legalize ourself, and return false, otherwise, return true.
409bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
410 SDNode *Dest) {
411 if (N == Dest) return true; // N certainly leads to Dest :)
412
413 // If the first result of this node has been already legalized, then it cannot
414 // reach N.
415 switch (getTypeAction(N->getValueType(0))) {
416 case Legal:
417 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
418 break;
419 case Promote:
420 if (PromotedNodes.count(SDOperand(N, 0))) return false;
421 break;
422 case Expand:
423 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 }
426
427 // Okay, this node has not already been legalized. Check and legalize all
428 // operands. If none lead to Dest, then we can legalize this node.
429 bool OperandsLeadToDest = false;
430 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
431 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
432 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
433
434 if (OperandsLeadToDest) return true;
435
436 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000437 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000438 return false;
439}
440
Chris Lattnerc7029802006-03-18 01:44:44 +0000441/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
442/// appropriate for its type.
443void SelectionDAGLegalize::HandleOp(SDOperand Op) {
444 switch (getTypeAction(Op.getValueType())) {
445 default: assert(0 && "Bad type action!");
446 case Legal: LegalizeOp(Op); break;
447 case Promote: PromoteOp(Op); break;
448 case Expand:
449 if (Op.getValueType() != MVT::Vector) {
450 SDOperand X, Y;
451 ExpandOp(Op, X, Y);
452 } else {
453 SDNode *N = Op.Val;
454 unsigned NumOps = N->getNumOperands();
455 unsigned NumElements =
456 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
457 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
458 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
459 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
460 // In the common case, this is a legal vector type, convert it to the
461 // packed operation and type now.
462 PackVectorOp(Op, PackedVT);
463 } else if (NumElements == 1) {
464 // Otherwise, if this is a single element vector, convert it to a
465 // scalar operation.
466 PackVectorOp(Op, EVT);
467 } else {
468 // Otherwise, this is a multiple element vector that isn't supported.
469 // Split it in half and legalize both parts.
470 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000471 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000472 }
473 }
474 break;
475 }
476}
Chris Lattner6831a812006-02-13 09:18:02 +0000477
478
Chris Lattnerc7029802006-03-18 01:44:44 +0000479/// LegalizeOp - We know that the specified value has a legal type.
480/// Recursively ensure that the operands have legal types, then return the
481/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000482SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000483 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000484 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000485 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000486
Chris Lattner3e928bb2005-01-07 07:47:09 +0000487 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000488 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000489 if (Node->getNumValues() > 1) {
490 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000491 if (getTypeAction(Node->getValueType(i)) != Legal) {
492 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000494 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000495 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000496 }
497 }
498
Chris Lattner45982da2005-05-12 16:53:42 +0000499 // Note that LegalizeOp may be reentered even from single-use nodes, which
500 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000501 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
502 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000503
Nate Begeman9373a812005-08-10 20:51:12 +0000504 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000505 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000506 bool isCustom = false;
507
Chris Lattner3e928bb2005-01-07 07:47:09 +0000508 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000509 case ISD::FrameIndex:
510 case ISD::EntryToken:
511 case ISD::Register:
512 case ISD::BasicBlock:
513 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000514 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000515 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000516 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000517 case ISD::TargetConstantPool:
518 case ISD::TargetGlobalAddress:
519 case ISD::TargetExternalSymbol:
520 case ISD::VALUETYPE:
521 case ISD::SRCVALUE:
522 case ISD::STRING:
523 case ISD::CONDCODE:
524 // Primitives must all be legal.
525 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
526 "This must be legal!");
527 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000528 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000529 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
530 // If this is a target node, legalize it by legalizing the operands then
531 // passing it through.
532 std::vector<SDOperand> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000533 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000534 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000535
536 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops);
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000537
538 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
539 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
540 return Result.getValue(Op.ResNo);
541 }
542 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000543#ifndef NDEBUG
Chris Lattner3e928bb2005-01-07 07:47:09 +0000544 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000545#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000546 assert(0 && "Do not know how to legalize this operator!");
547 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000548 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000549 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000550 case ISD::ConstantPool:
551 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000552 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
553 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000554 case TargetLowering::Custom:
555 Tmp1 = TLI.LowerOperation(Op, DAG);
556 if (Tmp1.Val) Result = Tmp1;
557 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000558 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000559 break;
560 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000561 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000562 case ISD::AssertSext:
563 case ISD::AssertZext:
564 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000566 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000567 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000568 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000569 Result = Node->getOperand(Op.ResNo);
570 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000571 case ISD::CopyFromReg:
572 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000573 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000574 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000576 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000577 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000578 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000579 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
581 } else {
582 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
583 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000584 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
585 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000586 // Since CopyFromReg produces two values, make sure to remember that we
587 // legalized both of them.
588 AddLegalizedOperand(Op.getValue(0), Result);
589 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
590 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000591 case ISD::UNDEF: {
592 MVT::ValueType VT = Op.getValueType();
593 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000594 default: assert(0 && "This action is not supported yet!");
595 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000596 if (MVT::isInteger(VT))
597 Result = DAG.getConstant(0, VT);
598 else if (MVT::isFloatingPoint(VT))
599 Result = DAG.getConstantFP(0, VT);
600 else
601 assert(0 && "Unknown value type!");
602 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000603 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000604 break;
605 }
606 break;
607 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000608
Chris Lattner48b61a72006-03-28 00:40:33 +0000609 case ISD::INTRINSIC_W_CHAIN:
610 case ISD::INTRINSIC_WO_CHAIN:
611 case ISD::INTRINSIC_VOID: {
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000612 std::vector<SDOperand> Ops;
613 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
614 Ops.push_back(LegalizeOp(Node->getOperand(i)));
615 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner10d7fa62006-03-26 09:12:51 +0000616
617 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000618 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000619 TargetLowering::Custom) {
620 Tmp3 = TLI.LowerOperation(Result, DAG);
621 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000622 }
623
624 if (Result.Val->getNumValues() == 1) break;
625
626 // Must have return value and chain result.
627 assert(Result.Val->getNumValues() == 2 &&
628 "Cannot return more than two values!");
629
630 // Since loads produce two values, make sure to remember that we
631 // legalized both of them.
632 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
633 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
634 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000635 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000636
637 case ISD::LOCATION:
638 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
640
641 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
642 case TargetLowering::Promote:
643 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000644 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000645 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000646 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
647 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
648
649 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
650 const std::string &FName =
651 cast<StringSDNode>(Node->getOperand(3))->getValue();
652 const std::string &DirName =
653 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000654 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000655
Jim Laskeye81aecb2005-12-21 20:51:37 +0000656 std::vector<SDOperand> Ops;
657 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000658 SDOperand LineOp = Node->getOperand(1);
659 SDOperand ColOp = Node->getOperand(2);
660
661 if (useDEBUG_LOC) {
662 Ops.push_back(LineOp); // line #
663 Ops.push_back(ColOp); // col #
664 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
665 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
666 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000667 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
668 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000669 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
670 Ops.push_back(DAG.getConstant(ID, MVT::i32));
671 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
672 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000673 } else {
674 Result = Tmp1; // chain
675 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000676 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000677 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000678 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000679 if (Tmp1 != Node->getOperand(0) ||
680 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000681 std::vector<SDOperand> Ops;
682 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000683 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
684 Ops.push_back(Node->getOperand(1)); // line # must be legal.
685 Ops.push_back(Node->getOperand(2)); // col # must be legal.
686 } else {
687 // Otherwise promote them.
688 Ops.push_back(PromoteOp(Node->getOperand(1)));
689 Ops.push_back(PromoteOp(Node->getOperand(2)));
690 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000691 Ops.push_back(Node->getOperand(3)); // filename must be legal.
692 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000693 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000694 }
695 break;
696 }
697 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000698
699 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000700 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000701 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000702 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000703 case TargetLowering::Legal:
704 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
705 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
706 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
707 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000708 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000709 break;
710 }
711 break;
712
713 case ISD::DEBUG_LABEL:
714 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
715 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000716 default: assert(0 && "This action is not supported yet!");
717 case TargetLowering::Legal:
718 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
719 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000721 break;
722 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000723 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000724
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725 case ISD::Constant:
726 // We know we don't need to expand constants here, constants only have one
727 // value and we check that it is fine above.
728
729 // FIXME: Maybe we should handle things like targets that don't support full
730 // 32-bit immediates?
731 break;
732 case ISD::ConstantFP: {
733 // Spill FP immediates to the constant pool if the target cannot directly
734 // codegen them. Targets often have some immediate values that can be
735 // efficiently generated into an FP register without a load. We explicitly
736 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000737 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
738
739 // Check to see if this FP immediate is already legal.
740 bool isLegal = false;
741 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
742 E = TLI.legal_fpimm_end(); I != E; ++I)
743 if (CFP->isExactlyValue(*I)) {
744 isLegal = true;
745 break;
746 }
747
Chris Lattner3181a772006-01-29 06:26:56 +0000748 // If this is a legal constant, turn it into a TargetConstantFP node.
749 if (isLegal) {
750 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
751 break;
752 }
753
754 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
755 default: assert(0 && "This action is not supported yet!");
756 case TargetLowering::Custom:
757 Tmp3 = TLI.LowerOperation(Result, DAG);
758 if (Tmp3.Val) {
759 Result = Tmp3;
760 break;
761 }
762 // FALLTHROUGH
763 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000764 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000765 bool Extend = false;
766
Chris Lattner456a93a2006-01-28 07:39:30 +0000767 // If a FP immediate is precise when represented as a float and if the
768 // target can do an extending load from float to double, we put it into
769 // the constant pool as a float, even if it's is statically typed as a
770 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000771 MVT::ValueType VT = CFP->getValueType(0);
772 bool isDouble = VT == MVT::f64;
773 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
774 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000775 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
776 // Only do this if the target has a native EXTLOAD instruction from
777 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000778 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000779 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
780 VT = MVT::f32;
781 Extend = true;
782 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000783
Chris Lattner456a93a2006-01-28 07:39:30 +0000784 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000785 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000786 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
787 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000788 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000789 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
790 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000791 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000792 }
793 break;
794 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000795 case ISD::TokenFactor:
796 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000797 Tmp1 = LegalizeOp(Node->getOperand(0));
798 Tmp2 = LegalizeOp(Node->getOperand(1));
799 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
800 } else if (Node->getNumOperands() == 3) {
801 Tmp1 = LegalizeOp(Node->getOperand(0));
802 Tmp2 = LegalizeOp(Node->getOperand(1));
803 Tmp3 = LegalizeOp(Node->getOperand(2));
804 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000805 } else {
806 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000807 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000808 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
809 Ops.push_back(LegalizeOp(Node->getOperand(i)));
810 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000811 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000812 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000813
814 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000815 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000816 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000817 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
818 assert(Tmp3.Val && "Target didn't custom lower this node!");
819 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
820 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000821
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000822 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000823 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000824 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
825 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000826 if (Op.ResNo == i)
827 Tmp2 = Tmp1;
828 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
829 }
830 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000831
Chris Lattnerb2827b02006-03-19 00:52:58 +0000832 case ISD::BUILD_VECTOR:
833 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000834 default: assert(0 && "This action is not supported yet!");
835 case TargetLowering::Custom:
836 Tmp3 = TLI.LowerOperation(Result, DAG);
837 if (Tmp3.Val) {
838 Result = Tmp3;
839 break;
840 }
841 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000842 case TargetLowering::Expand:
843 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000844 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000845 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000846 break;
847 case ISD::INSERT_VECTOR_ELT:
848 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
849 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
850 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
851 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
852
853 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
854 Node->getValueType(0))) {
855 default: assert(0 && "This action is not supported yet!");
856 case TargetLowering::Legal:
857 break;
858 case TargetLowering::Custom:
859 Tmp3 = TLI.LowerOperation(Result, DAG);
860 if (Tmp3.Val) {
861 Result = Tmp3;
862 break;
863 }
864 // FALLTHROUGH
865 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000866 // If the insert index is a constant, codegen this as a scalar_to_vector,
867 // then a shuffle that inserts it into the right position in the vector.
868 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
869 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
870 Tmp1.getValueType(), Tmp2);
871
872 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
873 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
874 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
875
876 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
877 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
878 // the RHS.
879 std::vector<SDOperand> ShufOps;
880 for (unsigned i = 0; i != NumElts; ++i) {
881 if (i != InsertPos->getValue())
882 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
883 else
884 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
885 }
886 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,ShufOps);
887
888 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
889 Tmp1, ScVec, ShufMask);
890 Result = LegalizeOp(Result);
891 break;
892 }
893
Chris Lattner2332b9f2006-03-19 01:17:20 +0000894 // If the target doesn't support this, we have to spill the input vector
895 // to a temporary stack slot, update the element, then reload it. This is
896 // badness. We could also load the value into a vector register (either
897 // with a "move to register" or "extload into register" instruction, then
898 // permute it into place, if the idx is a constant and if the idx is
899 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000900 MVT::ValueType VT = Tmp1.getValueType();
901 MVT::ValueType EltVT = Tmp2.getValueType();
902 MVT::ValueType IdxVT = Tmp3.getValueType();
903 MVT::ValueType PtrVT = TLI.getPointerTy();
904 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000905 // Store the vector.
906 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
907 Tmp1, StackPtr, DAG.getSrcValue(NULL));
908
909 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000910 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
911 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000912 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000913 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
914 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
915 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000916 // Store the scalar value.
917 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
918 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
919 // Load the updated vector.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000920 Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL));
Chris Lattner2332b9f2006-03-19 01:17:20 +0000921 break;
922 }
923 }
924 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000925 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000926 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
927 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
928 break;
929 }
930
Chris Lattnerce872152006-03-19 06:31:19 +0000931 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
932 Result = DAG.UpdateNodeOperands(Result, Tmp1);
933 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
934 Node->getValueType(0))) {
935 default: assert(0 && "This action is not supported yet!");
936 case TargetLowering::Legal:
937 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000938 case TargetLowering::Custom:
939 Tmp3 = TLI.LowerOperation(Result, DAG);
940 if (Tmp3.Val) {
941 Result = Tmp3;
942 break;
943 }
944 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000945 case TargetLowering::Expand:
946 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +0000947 break;
948 }
Chris Lattnerce872152006-03-19 06:31:19 +0000949 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000950 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +0000951 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
952 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
953 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
954
955 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +0000956 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
957 default: assert(0 && "Unknown operation action!");
958 case TargetLowering::Legal:
959 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
960 "vector shuffle should not be created if not legal!");
961 break;
962 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +0000963 Tmp3 = TLI.LowerOperation(Result, DAG);
964 if (Tmp3.Val) {
965 Result = Tmp3;
966 break;
967 }
968 // FALLTHROUGH
969 case TargetLowering::Expand: {
970 MVT::ValueType VT = Node->getValueType(0);
971 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
972 MVT::ValueType PtrVT = TLI.getPointerTy();
973 SDOperand Mask = Node->getOperand(2);
974 unsigned NumElems = Mask.getNumOperands();
975 std::vector<SDOperand> Ops;
976 for (unsigned i = 0; i != NumElems; ++i) {
977 SDOperand Arg = Mask.getOperand(i);
978 if (Arg.getOpcode() == ISD::UNDEF) {
979 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
980 } else {
981 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
982 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
983 if (Idx < NumElems)
984 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
985 DAG.getConstant(Idx, PtrVT)));
986 else
987 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
988 DAG.getConstant(Idx - NumElems, PtrVT)));
989 }
990 }
991 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
Chris Lattner4352cc92006-04-04 17:23:26 +0000992 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +0000993 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000994 case TargetLowering::Promote: {
995 // Change base type to a different vector type.
996 MVT::ValueType OVT = Node->getValueType(0);
997 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
998
999 // Cast the two input vectors.
1000 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1001 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1002
1003 // Convert the shuffle mask to the right # elements.
1004 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1005 assert(Tmp3.Val && "Shuffle not legal?");
1006 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1007 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1008 break;
1009 }
Chris Lattner87100e02006-03-20 01:52:29 +00001010 }
1011 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001012
1013 case ISD::EXTRACT_VECTOR_ELT:
1014 Tmp1 = LegalizeOp(Node->getOperand(0));
1015 Tmp2 = LegalizeOp(Node->getOperand(1));
1016 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001017
1018 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1019 Tmp1.getValueType())) {
1020 default: assert(0 && "This action is not supported yet!");
1021 case TargetLowering::Legal:
1022 break;
1023 case TargetLowering::Custom:
1024 Tmp3 = TLI.LowerOperation(Result, DAG);
1025 if (Tmp3.Val) {
1026 Result = Tmp3;
1027 break;
1028 }
1029 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001030 case TargetLowering::Expand:
1031 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001032 break;
1033 }
Chris Lattner384504c2006-03-21 20:44:12 +00001034 break;
1035
Chris Lattner15972212006-03-31 17:55:51 +00001036 case ISD::VEXTRACT_VECTOR_ELT:
1037 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001038 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001039
Chris Lattner6831a812006-02-13 09:18:02 +00001040 case ISD::CALLSEQ_START: {
1041 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1042
1043 // Recursively Legalize all of the inputs of the call end that do not lead
1044 // to this call start. This ensures that any libcalls that need be inserted
1045 // are inserted *before* the CALLSEQ_START.
1046 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1047 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
1048
1049 // Now that we legalized all of the inputs (which may have inserted
1050 // libcalls) create the new CALLSEQ_START node.
1051 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1052
1053 // Merge in the last call, to ensure that this call start after the last
1054 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001055 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001056 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1057 Tmp1 = LegalizeOp(Tmp1);
1058 }
Chris Lattner6831a812006-02-13 09:18:02 +00001059
1060 // Do not try to legalize the target-specific arguments (#1+).
1061 if (Tmp1 != Node->getOperand(0)) {
1062 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1063 Ops[0] = Tmp1;
1064 Result = DAG.UpdateNodeOperands(Result, Ops);
1065 }
1066
1067 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001068 AddLegalizedOperand(Op.getValue(0), Result);
1069 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1070 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1071
Chris Lattner6831a812006-02-13 09:18:02 +00001072 // Now that the callseq_start and all of the non-call nodes above this call
1073 // sequence have been legalized, legalize the call itself. During this
1074 // process, no libcalls can/will be inserted, guaranteeing that no calls
1075 // can overlap.
1076 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1077 SDOperand InCallSEQ = LastCALLSEQ_END;
1078 // Note that we are selecting this call!
1079 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1080 IsLegalizingCall = true;
1081
1082 // Legalize the call, starting from the CALLSEQ_END.
1083 LegalizeOp(LastCALLSEQ_END);
1084 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1085 return Result;
1086 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001087 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001088 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1089 // will cause this node to be legalized as well as handling libcalls right.
1090 if (LastCALLSEQ_END.Val != Node) {
1091 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1092 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1093 assert(I != LegalizedNodes.end() &&
1094 "Legalizing the call start should have legalized this node!");
1095 return I->second;
1096 }
1097
1098 // Otherwise, the call start has been legalized and everything is going
1099 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001100 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001101 // Do not try to legalize the target-specific arguments (#1+), except for
1102 // an optional flag input.
1103 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1104 if (Tmp1 != Node->getOperand(0)) {
1105 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1106 Ops[0] = Tmp1;
1107 Result = DAG.UpdateNodeOperands(Result, Ops);
1108 }
1109 } else {
1110 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1111 if (Tmp1 != Node->getOperand(0) ||
1112 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1113 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1114 Ops[0] = Tmp1;
1115 Ops.back() = Tmp2;
1116 Result = DAG.UpdateNodeOperands(Result, Ops);
1117 }
Chris Lattner6a542892006-01-24 05:48:21 +00001118 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001119 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001120 // This finishes up call legalization.
1121 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001122
1123 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1124 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1125 if (Node->getNumValues() == 2)
1126 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1127 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001128 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1130 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1131 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001132 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001133
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001134 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001135 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001136 switch (TLI.getOperationAction(Node->getOpcode(),
1137 Node->getValueType(0))) {
1138 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001139 case TargetLowering::Expand: {
1140 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1141 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1142 " not tell us which reg is the stack pointer!");
1143 SDOperand Chain = Tmp1.getOperand(0);
1144 SDOperand Size = Tmp2.getOperand(1);
1145 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1146 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1147 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001148 Tmp1 = LegalizeOp(Tmp1);
1149 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001150 break;
1151 }
1152 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001153 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1154 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001155 Tmp1 = LegalizeOp(Tmp3);
1156 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001157 }
Chris Lattner903d2782006-01-15 08:54:32 +00001158 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001159 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001160 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001161 }
Chris Lattner903d2782006-01-15 08:54:32 +00001162 // Since this op produce two values, make sure to remember that we
1163 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001164 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1165 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001166 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001167 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001168 case ISD::INLINEASM: {
1169 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1170 bool Changed = false;
1171 // Legalize all of the operands of the inline asm, in case they are nodes
1172 // that need to be expanded or something. Note we skip the asm string and
1173 // all of the TargetConstant flags.
1174 SDOperand Op = LegalizeOp(Ops[0]);
1175 Changed = Op != Ops[0];
1176 Ops[0] = Op;
1177
1178 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1179 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1180 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1181 for (++i; NumVals; ++i, --NumVals) {
1182 SDOperand Op = LegalizeOp(Ops[i]);
1183 if (Op != Ops[i]) {
1184 Changed = true;
1185 Ops[i] = Op;
1186 }
1187 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001188 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001189
1190 if (HasInFlag) {
1191 Op = LegalizeOp(Ops.back());
1192 Changed |= Op != Ops.back();
1193 Ops.back() = Op;
1194 }
1195
1196 if (Changed)
1197 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001198
1199 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001200 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001201 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1202 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001203 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001204 case ISD::BR:
1205 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001206 // Ensure that libcalls are emitted before a branch.
1207 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1208 Tmp1 = LegalizeOp(Tmp1);
1209 LastCALLSEQ_END = DAG.getEntryNode();
1210
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001211 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001212 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001213 case ISD::BRIND:
1214 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1215 // Ensure that libcalls are emitted before a branch.
1216 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1217 Tmp1 = LegalizeOp(Tmp1);
1218 LastCALLSEQ_END = DAG.getEntryNode();
1219
1220 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1221 default: assert(0 && "Indirect target must be legal type (pointer)!");
1222 case Legal:
1223 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1224 break;
1225 }
1226 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1227 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001228 case ISD::BRCOND:
1229 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001230 // Ensure that libcalls are emitted before a return.
1231 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1232 Tmp1 = LegalizeOp(Tmp1);
1233 LastCALLSEQ_END = DAG.getEntryNode();
1234
Chris Lattner47e92232005-01-18 19:27:06 +00001235 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1236 case Expand: assert(0 && "It's impossible to expand bools");
1237 case Legal:
1238 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1239 break;
1240 case Promote:
1241 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1242 break;
1243 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001244
1245 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001246 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001247
1248 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1249 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001250 case TargetLowering::Legal: break;
1251 case TargetLowering::Custom:
1252 Tmp1 = TLI.LowerOperation(Result, DAG);
1253 if (Tmp1.Val) Result = Tmp1;
1254 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001255 case TargetLowering::Expand:
1256 // Expand brcond's setcc into its constituent parts and create a BR_CC
1257 // Node.
1258 if (Tmp2.getOpcode() == ISD::SETCC) {
1259 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1260 Tmp2.getOperand(0), Tmp2.getOperand(1),
1261 Node->getOperand(2));
1262 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001263 // Make sure the condition is either zero or one. It may have been
1264 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001265 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1266 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1267 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001268
Nate Begeman7cbd5252005-08-16 19:49:35 +00001269 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1270 DAG.getCondCode(ISD::SETNE), Tmp2,
1271 DAG.getConstant(0, Tmp2.getValueType()),
1272 Node->getOperand(2));
1273 }
1274 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001275 }
1276 break;
1277 case ISD::BR_CC:
1278 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001279 // Ensure that libcalls are emitted before a branch.
1280 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1281 Tmp1 = LegalizeOp(Tmp1);
1282 LastCALLSEQ_END = DAG.getEntryNode();
1283
Nate Begeman750ac1b2006-02-01 07:19:44 +00001284 Tmp2 = Node->getOperand(2); // LHS
1285 Tmp3 = Node->getOperand(3); // RHS
1286 Tmp4 = Node->getOperand(1); // CC
1287
1288 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1289
1290 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1291 // the LHS is a legal SETCC itself. In this case, we need to compare
1292 // the result against zero to select between true and false values.
1293 if (Tmp3.Val == 0) {
1294 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1295 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001296 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001297
1298 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1299 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001300
Chris Lattner181b7a32005-12-17 23:46:46 +00001301 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1302 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001303 case TargetLowering::Legal: break;
1304 case TargetLowering::Custom:
1305 Tmp4 = TLI.LowerOperation(Result, DAG);
1306 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001307 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001308 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001309 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001310 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1312 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001313
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001314 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001315 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001316 Tmp3 = Result.getValue(0);
1317 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001318
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001319 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1320 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001321 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001322 case TargetLowering::Custom:
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001323 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001324 if (Tmp1.Val) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001325 Tmp3 = LegalizeOp(Tmp1);
1326 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001327 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001328 break;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001329 case TargetLowering::Promote: {
1330 // Only promote a load of vector type to another.
1331 assert(MVT::isVector(VT) && "Cannot promote this load!");
1332 // Change base type to a different vector type.
1333 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1334
1335 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, Node->getOperand(2));
1336 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1337 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1338 break;
1339 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001340 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001341 // Since loads produce two values, make sure to remember that we
1342 // legalized both of them.
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001343 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1344 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1345 return Op.ResNo ? Tmp4 : Tmp3;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001346 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001347 case ISD::EXTLOAD:
1348 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001349 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001350 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1351 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001352
Chris Lattner5f056bf2005-07-10 01:55:33 +00001353 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001354 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001355 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001356 case TargetLowering::Promote:
1357 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1359 DAG.getValueType(MVT::i8));
1360 Tmp1 = Result.getValue(0);
1361 Tmp2 = Result.getValue(1);
1362 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001363 case TargetLowering::Custom:
1364 isCustom = true;
1365 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001366 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001367 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1368 Node->getOperand(3));
1369 Tmp1 = Result.getValue(0);
1370 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001371
1372 if (isCustom) {
1373 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1374 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001375 Tmp1 = LegalizeOp(Tmp3);
1376 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001377 }
1378 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001379 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001380 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001381 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001382 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1383 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001384 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001385 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1386 Tmp2 = LegalizeOp(Load.getValue(1));
1387 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001388 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001389 assert(Node->getOpcode() != ISD::EXTLOAD &&
1390 "EXTLOAD should always be supported!");
1391 // Turn the unsupported load into an EXTLOAD followed by an explicit
1392 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001393 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1394 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001395 SDOperand ValRes;
1396 if (Node->getOpcode() == ISD::SEXTLOAD)
1397 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001398 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001399 else
1400 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001401 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1402 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1403 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001404 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001405 // Since loads produce two values, make sure to remember that we legalized
1406 // both of them.
1407 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1408 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1409 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001410 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001411 case ISD::EXTRACT_ELEMENT: {
1412 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1413 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001414 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001415 case Legal:
1416 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1417 // 1 -> Hi
1418 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1419 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1420 TLI.getShiftAmountTy()));
1421 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1422 } else {
1423 // 0 -> Lo
1424 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1425 Node->getOperand(0));
1426 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001427 break;
1428 case Expand:
1429 // Get both the low and high parts.
1430 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1431 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1432 Result = Tmp2; // 1 -> Hi
1433 else
1434 Result = Tmp1; // 0 -> Lo
1435 break;
1436 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001437 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001438 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001439
1440 case ISD::CopyToReg:
1441 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001442
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001443 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001444 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001445 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001446 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001447 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001449 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001450 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001451 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001452 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1454 Tmp3);
1455 } else {
1456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001457 }
1458
1459 // Since this produces two values, make sure to remember that we legalized
1460 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001461 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001462 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001463 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001464 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001465 break;
1466
1467 case ISD::RET:
1468 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001469
1470 // Ensure that libcalls are emitted before a return.
1471 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1472 Tmp1 = LegalizeOp(Tmp1);
1473 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001474
Chris Lattner3e928bb2005-01-07 07:47:09 +00001475 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001476 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001477 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001478 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001479 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001480 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001481 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001482 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001483 case Expand:
1484 if (Tmp2.getValueType() != MVT::Vector) {
1485 SDOperand Lo, Hi;
1486 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001487 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001488 } else {
1489 SDNode *InVal = Tmp2.Val;
1490 unsigned NumElems =
1491 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1492 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1493
1494 // Figure out if there is a Packed type corresponding to this Vector
1495 // type. If so, convert to the packed type.
1496 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1497 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1498 // Turn this into a return of the packed type.
1499 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001501 } else if (NumElems == 1) {
1502 // Turn this into a return of the scalar type.
1503 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001505
1506 // FIXME: Returns of gcc generic vectors smaller than a legal type
1507 // should be returned in integer registers!
1508
Chris Lattnerf87324e2006-04-11 01:31:51 +00001509 // The scalarized value type may not be legal, e.g. it might require
1510 // promotion or expansion. Relegalize the return.
1511 Result = LegalizeOp(Result);
1512 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001513 // FIXME: Returns of gcc generic vectors larger than a legal vector
1514 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001515 SDOperand Lo, Hi;
1516 SplitVectorOp(Tmp2, Lo, Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001517 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001518 Result = LegalizeOp(Result);
1519 }
1520 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001521 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001522 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001523 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001525 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001526 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 }
1528 break;
1529 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001530 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001531 break;
1532 default: { // ret <values>
1533 std::vector<SDOperand> NewValues;
1534 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001535 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001536 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1537 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001538 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001539 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001540 break;
1541 case Expand: {
1542 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001543 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1544 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001545 ExpandOp(Node->getOperand(i), Lo, Hi);
1546 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001547 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001548 NewValues.push_back(Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001549 NewValues.push_back(Node->getOperand(i+1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001550 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001551 }
1552 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001553 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001554 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001555
1556 if (NewValues.size() == Node->getNumOperands())
1557 Result = DAG.UpdateNodeOperands(Result, NewValues);
1558 else
1559 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001560 break;
1561 }
1562 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001563
Chris Lattner6862dbc2006-01-29 21:02:23 +00001564 if (Result.getOpcode() == ISD::RET) {
1565 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1566 default: assert(0 && "This action is not supported yet!");
1567 case TargetLowering::Legal: break;
1568 case TargetLowering::Custom:
1569 Tmp1 = TLI.LowerOperation(Result, DAG);
1570 if (Tmp1.Val) Result = Tmp1;
1571 break;
1572 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001573 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001574 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001575 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1577 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1578
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001579 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001580 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001581 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001582 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001583 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001584 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001585 } else {
1586 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001587 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001588 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001589 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1590 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001591 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001592 }
1593
Chris Lattner3e928bb2005-01-07 07:47:09 +00001594 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1595 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001596 Tmp3 = LegalizeOp(Node->getOperand(1));
1597 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1598 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001599
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001600 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001601 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1602 default: assert(0 && "This action is not supported yet!");
1603 case TargetLowering::Legal: break;
1604 case TargetLowering::Custom:
1605 Tmp1 = TLI.LowerOperation(Result, DAG);
1606 if (Tmp1.Val) Result = Tmp1;
1607 break;
Chris Lattner2efce0a2006-04-16 01:36:45 +00001608 case TargetLowering::Promote:
1609 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1610 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1611 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1612 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1613 Node->getOperand(3));
1614 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001615 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001616 break;
1617 }
1618 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001619 // Truncate the value and store the result.
1620 Tmp3 = PromoteOp(Node->getOperand(1));
1621 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001622 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001623 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001624 break;
1625
Chris Lattner3e928bb2005-01-07 07:47:09 +00001626 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001627 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001628 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001629
1630 // If this is a vector type, then we have to calculate the increment as
1631 // the product of the element size in bytes, and the number of elements
1632 // in the high half of the vector.
1633 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1634 SDNode *InVal = Node->getOperand(1).Val;
1635 unsigned NumElems =
1636 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1637 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1638
1639 // Figure out if there is a Packed type corresponding to this Vector
1640 // type. If so, convert to the packed type.
1641 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1642 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1643 // Turn this into a normal store of the packed type.
1644 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1645 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1646 Node->getOperand(3));
Chris Lattner2efce0a2006-04-16 01:36:45 +00001647 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001648 break;
1649 } else if (NumElems == 1) {
1650 // Turn this into a normal store of the scalar type.
1651 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1652 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1653 Node->getOperand(3));
Chris Lattner2ae2e982006-03-31 17:37:22 +00001654 // The scalarized value type may not be legal, e.g. it might require
1655 // promotion or expansion. Relegalize the scalar store.
1656 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001657 break;
1658 } else {
1659 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1660 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1661 }
1662 } else {
1663 ExpandOp(Node->getOperand(1), Lo, Hi);
1664 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001665
Chris Lattnerd9731af2006-03-31 18:20:46 +00001666 if (!TLI.isLittleEndian())
1667 std::swap(Lo, Hi);
1668 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001669
Chris Lattneredb1add2005-05-11 04:51:16 +00001670 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1671 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001672 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1673 getIntPtrConstant(IncrementSize));
1674 assert(isTypeLegal(Tmp2.getValueType()) &&
1675 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001676 // FIXME: This sets the srcvalue of both halves to be the same, which is
1677 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001678 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1679 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001680 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1681 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001682 }
1683 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001684 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001685 case ISD::PCMARKER:
1686 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001688 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001689 case ISD::STACKSAVE:
1690 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001691 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1692 Tmp1 = Result.getValue(0);
1693 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001694
Chris Lattner140d53c2006-01-13 02:50:02 +00001695 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1696 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001697 case TargetLowering::Legal: break;
1698 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001699 Tmp3 = TLI.LowerOperation(Result, DAG);
1700 if (Tmp3.Val) {
1701 Tmp1 = LegalizeOp(Tmp3);
1702 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001703 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001704 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001705 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001706 // Expand to CopyFromReg if the target set
1707 // StackPointerRegisterToSaveRestore.
1708 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001709 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001710 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001711 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001712 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001713 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1714 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001715 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001716 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001717 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001718
1719 // Since stacksave produce two values, make sure to remember that we
1720 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001721 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1722 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1723 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001724
Chris Lattner140d53c2006-01-13 02:50:02 +00001725 case ISD::STACKRESTORE:
1726 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1727 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001729
1730 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1731 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001732 case TargetLowering::Legal: break;
1733 case TargetLowering::Custom:
1734 Tmp1 = TLI.LowerOperation(Result, DAG);
1735 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001736 break;
1737 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001738 // Expand to CopyToReg if the target set
1739 // StackPointerRegisterToSaveRestore.
1740 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1741 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1742 } else {
1743 Result = Tmp1;
1744 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001745 break;
1746 }
1747 break;
1748
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001749 case ISD::READCYCLECOUNTER:
1750 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001751 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001752
1753 // Since rdcc produce two values, make sure to remember that we legalized
1754 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001755 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001756 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001757 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001758
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001759 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001760 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1761 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1762
Chris Lattner456a93a2006-01-28 07:39:30 +00001763 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1764 "Cannot handle illegal TRUNCSTORE yet!");
1765 Tmp2 = LegalizeOp(Node->getOperand(1));
1766
1767 // The only promote case we handle is TRUNCSTORE:i1 X into
1768 // -> TRUNCSTORE:i8 (and X, 1)
1769 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1770 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1771 TargetLowering::Promote) {
1772 // Promote the bool to a mask then store.
1773 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1774 DAG.getConstant(1, Tmp2.getValueType()));
1775 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1776 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001777
Chris Lattner456a93a2006-01-28 07:39:30 +00001778 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1779 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001780 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1781 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001782 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001783
Chris Lattner456a93a2006-01-28 07:39:30 +00001784 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1785 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1786 default: assert(0 && "This action is not supported yet!");
1787 case TargetLowering::Legal: break;
1788 case TargetLowering::Custom:
1789 Tmp1 = TLI.LowerOperation(Result, DAG);
1790 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001791 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001792 }
1793 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001794 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001795 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001796 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1797 case Expand: assert(0 && "It's impossible to expand bools");
1798 case Legal:
1799 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1800 break;
1801 case Promote:
1802 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1803 break;
1804 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001805 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001806 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001807
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001808 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001809
Nate Begemanb942a3d2005-08-23 04:29:48 +00001810 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001811 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001812 case TargetLowering::Legal: break;
1813 case TargetLowering::Custom: {
1814 Tmp1 = TLI.LowerOperation(Result, DAG);
1815 if (Tmp1.Val) Result = Tmp1;
1816 break;
1817 }
Nate Begeman9373a812005-08-10 20:51:12 +00001818 case TargetLowering::Expand:
1819 if (Tmp1.getOpcode() == ISD::SETCC) {
1820 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1821 Tmp2, Tmp3,
1822 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1823 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001824 // Make sure the condition is either zero or one. It may have been
1825 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001826 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1827 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1828 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001829 Result = DAG.getSelectCC(Tmp1,
1830 DAG.getConstant(0, Tmp1.getValueType()),
1831 Tmp2, Tmp3, ISD::SETNE);
1832 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001833 break;
1834 case TargetLowering::Promote: {
1835 MVT::ValueType NVT =
1836 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1837 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001838 if (MVT::isVector(Tmp2.getValueType())) {
1839 ExtOp = ISD::BIT_CONVERT;
1840 TruncOp = ISD::BIT_CONVERT;
1841 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001842 ExtOp = ISD::ANY_EXTEND;
1843 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001844 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001845 ExtOp = ISD::FP_EXTEND;
1846 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001847 }
1848 // Promote each of the values to the new type.
1849 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1850 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1851 // Perform the larger operation, then round down.
1852 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1853 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1854 break;
1855 }
1856 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001857 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001858 case ISD::SELECT_CC: {
1859 Tmp1 = Node->getOperand(0); // LHS
1860 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001861 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1862 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001863 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001864
Nate Begeman750ac1b2006-02-01 07:19:44 +00001865 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1866
1867 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1868 // the LHS is a legal SETCC itself. In this case, we need to compare
1869 // the result against zero to select between true and false values.
1870 if (Tmp2.Val == 0) {
1871 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1872 CC = DAG.getCondCode(ISD::SETNE);
1873 }
1874 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1875
1876 // Everything is legal, see if we should expand this op or something.
1877 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1878 default: assert(0 && "This action is not supported yet!");
1879 case TargetLowering::Legal: break;
1880 case TargetLowering::Custom:
1881 Tmp1 = TLI.LowerOperation(Result, DAG);
1882 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001883 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001884 }
1885 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001886 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001887 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001888 Tmp1 = Node->getOperand(0);
1889 Tmp2 = Node->getOperand(1);
1890 Tmp3 = Node->getOperand(2);
1891 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1892
1893 // If we had to Expand the SetCC operands into a SELECT node, then it may
1894 // not always be possible to return a true LHS & RHS. In this case, just
1895 // return the value we legalized, returned in the LHS
1896 if (Tmp2.Val == 0) {
1897 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001898 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001899 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001900
Chris Lattner73e142f2006-01-30 22:43:50 +00001901 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001902 default: assert(0 && "Cannot handle this action for SETCC yet!");
1903 case TargetLowering::Custom:
1904 isCustom = true;
1905 // FALLTHROUGH.
1906 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001908 if (isCustom) {
1909 Tmp3 = TLI.LowerOperation(Result, DAG);
1910 if (Tmp3.Val) Result = Tmp3;
1911 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001912 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001913 case TargetLowering::Promote: {
1914 // First step, figure out the appropriate operation to use.
1915 // Allow SETCC to not be supported for all legal data types
1916 // Mostly this targets FP
1917 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1918 MVT::ValueType OldVT = NewInTy;
1919
1920 // Scan for the appropriate larger type to use.
1921 while (1) {
1922 NewInTy = (MVT::ValueType)(NewInTy+1);
1923
1924 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1925 "Fell off of the edge of the integer world");
1926 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1927 "Fell off of the edge of the floating point world");
1928
1929 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001930 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001931 break;
1932 }
1933 if (MVT::isInteger(NewInTy))
1934 assert(0 && "Cannot promote Legal Integer SETCC yet");
1935 else {
1936 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1937 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1938 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001939 Tmp1 = LegalizeOp(Tmp1);
1940 Tmp2 = LegalizeOp(Tmp2);
1941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001942 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001943 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001944 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001945 case TargetLowering::Expand:
1946 // Expand a setcc node into a select_cc of the same condition, lhs, and
1947 // rhs that selects between const 1 (true) and const 0 (false).
1948 MVT::ValueType VT = Node->getValueType(0);
1949 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1950 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1951 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001952 break;
1953 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001954 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001955 case ISD::MEMSET:
1956 case ISD::MEMCPY:
1957 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001959 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1960
1961 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1962 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1963 case Expand: assert(0 && "Cannot expand a byte!");
1964 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001965 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001966 break;
1967 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001968 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001969 break;
1970 }
1971 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001972 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001973 }
Chris Lattner272455b2005-02-02 03:44:41 +00001974
1975 SDOperand Tmp4;
1976 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001977 case Expand: {
1978 // Length is too big, just take the lo-part of the length.
1979 SDOperand HiPart;
1980 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1981 break;
1982 }
Chris Lattnere5605212005-01-28 22:29:18 +00001983 case Legal:
1984 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001985 break;
1986 case Promote:
1987 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001988 break;
1989 }
1990
1991 SDOperand Tmp5;
1992 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1993 case Expand: assert(0 && "Cannot expand this yet!");
1994 case Legal:
1995 Tmp5 = LegalizeOp(Node->getOperand(4));
1996 break;
1997 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001998 Tmp5 = PromoteOp(Node->getOperand(4));
1999 break;
2000 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002001
2002 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2003 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002004 case TargetLowering::Custom:
2005 isCustom = true;
2006 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002007 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002009 if (isCustom) {
2010 Tmp1 = TLI.LowerOperation(Result, DAG);
2011 if (Tmp1.Val) Result = Tmp1;
2012 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002013 break;
2014 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002015 // Otherwise, the target does not support this operation. Lower the
2016 // operation to an explicit libcall as appropriate.
2017 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002018 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Chris Lattnere1bd8222005-01-11 05:57:22 +00002019 std::vector<std::pair<SDOperand, const Type*> > Args;
2020
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002021 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002022 if (Node->getOpcode() == ISD::MEMSET) {
2023 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002024 // Extend the (previously legalized) ubyte argument to be an int value
2025 // for the call.
2026 if (Tmp3.getValueType() > MVT::i32)
2027 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2028 else
2029 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002030 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
2031 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
2032
2033 FnName = "memset";
2034 } else if (Node->getOpcode() == ISD::MEMCPY ||
2035 Node->getOpcode() == ISD::MEMMOVE) {
2036 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
2037 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
2038 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
2039 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2040 } else {
2041 assert(0 && "Unknown op!");
2042 }
Chris Lattner45982da2005-05-12 16:53:42 +00002043
Chris Lattnere1bd8222005-01-11 05:57:22 +00002044 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002045 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002046 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002047 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002048 break;
2049 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002050 }
2051 break;
2052 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002053
Chris Lattner5b359c62005-04-02 04:00:59 +00002054 case ISD::SHL_PARTS:
2055 case ISD::SRA_PARTS:
2056 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002057 std::vector<SDOperand> Ops;
2058 bool Changed = false;
2059 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2060 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2061 Changed |= Ops.back() != Node->getOperand(i);
2062 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002063 if (Changed)
2064 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00002065
Evan Cheng05a2d562006-01-09 18:31:59 +00002066 switch (TLI.getOperationAction(Node->getOpcode(),
2067 Node->getValueType(0))) {
2068 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002069 case TargetLowering::Legal: break;
2070 case TargetLowering::Custom:
2071 Tmp1 = TLI.LowerOperation(Result, DAG);
2072 if (Tmp1.Val) {
2073 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002074 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002075 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002076 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2077 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002078 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002079 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002080 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002081 return RetVal;
2082 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002083 break;
2084 }
2085
Chris Lattner2c8086f2005-04-02 05:00:07 +00002086 // Since these produce multiple values, make sure to remember that we
2087 // legalized all of them.
2088 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2089 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2090 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002091 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002092
2093 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002094 case ISD::ADD:
2095 case ISD::SUB:
2096 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002097 case ISD::MULHS:
2098 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002099 case ISD::UDIV:
2100 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002101 case ISD::AND:
2102 case ISD::OR:
2103 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002104 case ISD::SHL:
2105 case ISD::SRL:
2106 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002107 case ISD::FADD:
2108 case ISD::FSUB:
2109 case ISD::FMUL:
2110 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002111 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002112 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2113 case Expand: assert(0 && "Not possible");
2114 case Legal:
2115 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2116 break;
2117 case Promote:
2118 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2119 break;
2120 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002121
2122 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002123
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002124 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002125 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002126 case TargetLowering::Legal: break;
2127 case TargetLowering::Custom:
2128 Tmp1 = TLI.LowerOperation(Result, DAG);
2129 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002130 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002131 case TargetLowering::Expand: {
2132 assert(MVT::isVector(Node->getValueType(0)) &&
2133 "Cannot expand this binary operator!");
2134 // Expand the operation into a bunch of nasty scalar code.
2135 std::vector<SDOperand> Ops;
2136 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2137 MVT::ValueType PtrVT = TLI.getPointerTy();
2138 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2139 i != e; ++i) {
2140 SDOperand Idx = DAG.getConstant(i, PtrVT);
2141 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2142 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2143 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2144 }
2145 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
2146 break;
2147 }
Evan Chengcc987612006-04-12 21:20:24 +00002148 case TargetLowering::Promote: {
2149 switch (Node->getOpcode()) {
2150 default: assert(0 && "Do not know how to promote this BinOp!");
2151 case ISD::AND:
2152 case ISD::OR:
2153 case ISD::XOR: {
2154 MVT::ValueType OVT = Node->getValueType(0);
2155 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2156 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2157 // Bit convert each of the values to the new type.
2158 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2159 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2160 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2161 // Bit convert the result back the original type.
2162 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2163 break;
2164 }
2165 }
2166 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002167 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002168 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002169
2170 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2171 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2172 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2173 case Expand: assert(0 && "Not possible");
2174 case Legal:
2175 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2176 break;
2177 case Promote:
2178 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2179 break;
2180 }
2181
2182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2183
2184 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2185 default: assert(0 && "Operation not supported");
2186 case TargetLowering::Custom:
2187 Tmp1 = TLI.LowerOperation(Result, DAG);
2188 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002189 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002190 case TargetLowering::Legal: break;
2191 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00002192 // If this target supports fabs/fneg natively, do this efficiently.
2193 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2194 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2195 // Get the sign bit of the RHS.
2196 MVT::ValueType IVT =
2197 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2198 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2199 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2200 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2201 // Get the absolute value of the result.
2202 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2203 // Select between the nabs and abs value based on the sign bit of
2204 // the input.
2205 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2206 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2207 AbsVal),
2208 AbsVal);
2209 Result = LegalizeOp(Result);
2210 break;
2211 }
2212
2213 // Otherwise, do bitwise ops!
2214
2215 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00002216 const char *FnName;
2217 if (Node->getValueType(0) == MVT::f32) {
2218 FnName = "copysignf";
2219 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2220 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2221 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2222 } else {
2223 FnName = "copysign";
2224 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2225 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2226 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2227 }
2228 SDOperand Dummy;
2229 Result = ExpandLibCall(FnName, Node, Dummy);
2230 break;
2231 }
2232 break;
2233
Nate Begeman551bf3f2006-02-17 05:43:56 +00002234 case ISD::ADDC:
2235 case ISD::SUBC:
2236 Tmp1 = LegalizeOp(Node->getOperand(0));
2237 Tmp2 = LegalizeOp(Node->getOperand(1));
2238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2239 // Since this produces two values, make sure to remember that we legalized
2240 // both of them.
2241 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2242 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2243 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002244
Nate Begeman551bf3f2006-02-17 05:43:56 +00002245 case ISD::ADDE:
2246 case ISD::SUBE:
2247 Tmp1 = LegalizeOp(Node->getOperand(0));
2248 Tmp2 = LegalizeOp(Node->getOperand(1));
2249 Tmp3 = LegalizeOp(Node->getOperand(2));
2250 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2251 // Since this produces two values, make sure to remember that we legalized
2252 // both of them.
2253 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2254 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2255 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002256
Nate Begeman419f8b62005-10-18 00:27:41 +00002257 case ISD::BUILD_PAIR: {
2258 MVT::ValueType PairTy = Node->getValueType(0);
2259 // TODO: handle the case where the Lo and Hi operands are not of legal type
2260 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2261 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2262 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002263 case TargetLowering::Promote:
2264 case TargetLowering::Custom:
2265 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002266 case TargetLowering::Legal:
2267 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2268 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2269 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002270 case TargetLowering::Expand:
2271 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2272 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2273 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2274 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2275 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002276 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002277 break;
2278 }
2279 break;
2280 }
2281
Nate Begemanc105e192005-04-06 00:23:54 +00002282 case ISD::UREM:
2283 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002284 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002285 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2286 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002287
Nate Begemanc105e192005-04-06 00:23:54 +00002288 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002289 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2290 case TargetLowering::Custom:
2291 isCustom = true;
2292 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002293 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002294 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002295 if (isCustom) {
2296 Tmp1 = TLI.LowerOperation(Result, DAG);
2297 if (Tmp1.Val) Result = Tmp1;
2298 }
Nate Begemanc105e192005-04-06 00:23:54 +00002299 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002300 case TargetLowering::Expand:
2301 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002302 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002303 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002304 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002305 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2306 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2307 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2308 } else {
2309 // Floating point mod -> fmod libcall.
2310 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2311 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002312 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002313 }
2314 break;
2315 }
2316 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002317 case ISD::VAARG: {
2318 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2319 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2320
Chris Lattner5c62f332006-01-28 07:42:08 +00002321 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002322 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2323 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002324 case TargetLowering::Custom:
2325 isCustom = true;
2326 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002327 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2329 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002330 Tmp1 = Result.getValue(1);
2331
2332 if (isCustom) {
2333 Tmp2 = TLI.LowerOperation(Result, DAG);
2334 if (Tmp2.Val) {
2335 Result = LegalizeOp(Tmp2);
2336 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2337 }
2338 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002339 break;
2340 case TargetLowering::Expand: {
2341 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2342 Node->getOperand(2));
2343 // Increment the pointer, VAList, to the next vaarg
2344 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2345 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2346 TLI.getPointerTy()));
2347 // Store the incremented VAList to the legalized pointer
2348 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2349 Node->getOperand(2));
2350 // Load the actual argument out of the pointer VAList
2351 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002352 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002353 Result = LegalizeOp(Result);
2354 break;
2355 }
2356 }
2357 // Since VAARG produces two values, make sure to remember that we
2358 // legalized both of them.
2359 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002360 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2361 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002362 }
2363
2364 case ISD::VACOPY:
2365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2366 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2367 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2368
2369 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2370 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002371 case TargetLowering::Custom:
2372 isCustom = true;
2373 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002374 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002375 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2376 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002377 if (isCustom) {
2378 Tmp1 = TLI.LowerOperation(Result, DAG);
2379 if (Tmp1.Val) Result = Tmp1;
2380 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002381 break;
2382 case TargetLowering::Expand:
2383 // This defaults to loading a pointer from the input and storing it to the
2384 // output, returning the chain.
2385 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2386 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2387 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002388 break;
2389 }
2390 break;
2391
2392 case ISD::VAEND:
2393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2395
2396 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2397 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002398 case TargetLowering::Custom:
2399 isCustom = true;
2400 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002401 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002402 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002403 if (isCustom) {
2404 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2405 if (Tmp1.Val) Result = Tmp1;
2406 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002407 break;
2408 case TargetLowering::Expand:
2409 Result = Tmp1; // Default to a no-op, return the chain
2410 break;
2411 }
2412 break;
2413
2414 case ISD::VASTART:
2415 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2416 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2417
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002418 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2419
Nate Begemanacc398c2006-01-25 18:21:52 +00002420 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2421 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002422 case TargetLowering::Legal: break;
2423 case TargetLowering::Custom:
2424 Tmp1 = TLI.LowerOperation(Result, DAG);
2425 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002426 break;
2427 }
2428 break;
2429
Nate Begeman35ef9132006-01-11 21:21:00 +00002430 case ISD::ROTL:
2431 case ISD::ROTR:
2432 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2433 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002434
2435 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2436 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002438 break;
2439
Nate Begemand88fc032006-01-14 03:14:10 +00002440 case ISD::BSWAP:
2441 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2442 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002443 case TargetLowering::Custom:
2444 assert(0 && "Cannot custom legalize this yet!");
2445 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002446 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002447 break;
2448 case TargetLowering::Promote: {
2449 MVT::ValueType OVT = Tmp1.getValueType();
2450 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2451 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002452
Chris Lattner456a93a2006-01-28 07:39:30 +00002453 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2454 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2455 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2456 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2457 break;
2458 }
2459 case TargetLowering::Expand:
2460 Result = ExpandBSWAP(Tmp1);
2461 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002462 }
2463 break;
2464
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002465 case ISD::CTPOP:
2466 case ISD::CTTZ:
2467 case ISD::CTLZ:
2468 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2469 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002470 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002471 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002472 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002473 break;
2474 case TargetLowering::Promote: {
2475 MVT::ValueType OVT = Tmp1.getValueType();
2476 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002477
2478 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002479 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2480 // Perform the larger operation, then subtract if needed.
2481 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002482 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002483 case ISD::CTPOP:
2484 Result = Tmp1;
2485 break;
2486 case ISD::CTTZ:
2487 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002488 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2489 DAG.getConstant(getSizeInBits(NVT), NVT),
2490 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002491 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002492 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2493 break;
2494 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002495 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002496 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2497 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002498 getSizeInBits(OVT), NVT));
2499 break;
2500 }
2501 break;
2502 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002503 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002504 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002505 break;
2506 }
2507 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002508
Chris Lattner2c8086f2005-04-02 05:00:07 +00002509 // Unary operators
2510 case ISD::FABS:
2511 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002512 case ISD::FSQRT:
2513 case ISD::FSIN:
2514 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002515 Tmp1 = LegalizeOp(Node->getOperand(0));
2516 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002517 case TargetLowering::Promote:
2518 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002519 isCustom = true;
2520 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002521 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002522 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002523 if (isCustom) {
2524 Tmp1 = TLI.LowerOperation(Result, DAG);
2525 if (Tmp1.Val) Result = Tmp1;
2526 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002527 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002528 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002529 switch (Node->getOpcode()) {
2530 default: assert(0 && "Unreachable!");
2531 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002532 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2533 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002534 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002535 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002536 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002537 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2538 MVT::ValueType VT = Node->getValueType(0);
2539 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002540 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002541 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2542 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002543 break;
2544 }
2545 case ISD::FSQRT:
2546 case ISD::FSIN:
2547 case ISD::FCOS: {
2548 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002549 const char *FnName = 0;
2550 switch(Node->getOpcode()) {
2551 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2552 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2553 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2554 default: assert(0 && "Unreachable!");
2555 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002556 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002557 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002558 break;
2559 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002560 }
2561 break;
2562 }
2563 break;
Chris Lattner35481892005-12-23 00:16:34 +00002564
2565 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002566 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002567 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002568 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002569 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2570 Node->getOperand(0).getValueType())) {
2571 default: assert(0 && "Unknown operation action!");
2572 case TargetLowering::Expand:
2573 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2574 break;
2575 case TargetLowering::Legal:
2576 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002577 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002578 break;
2579 }
2580 }
2581 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002582 case ISD::VBIT_CONVERT: {
2583 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2584 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2585
2586 // The input has to be a vector type, we have to either scalarize it, pack
2587 // it, or convert it based on whether the input vector type is legal.
2588 SDNode *InVal = Node->getOperand(0).Val;
2589 unsigned NumElems =
2590 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2591 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2592
2593 // Figure out if there is a Packed type corresponding to this Vector
2594 // type. If so, convert to the packed type.
2595 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2596 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2597 // Turn this into a bit convert of the packed input.
2598 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2599 PackVectorOp(Node->getOperand(0), TVT));
2600 break;
2601 } else if (NumElems == 1) {
2602 // Turn this into a bit convert of the scalar input.
2603 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2604 PackVectorOp(Node->getOperand(0), EVT));
2605 break;
2606 } else {
2607 // FIXME: UNIMP! Store then reload
2608 assert(0 && "Cast from unsupported vector type not implemented yet!");
2609 }
2610 }
2611
Chris Lattner2c8086f2005-04-02 05:00:07 +00002612 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002613 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002614 case ISD::UINT_TO_FP: {
2615 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2617 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002618 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002619 Node->getOperand(0).getValueType())) {
2620 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002621 case TargetLowering::Custom:
2622 isCustom = true;
2623 // FALLTHROUGH
2624 case TargetLowering::Legal:
2625 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002626 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002627 if (isCustom) {
2628 Tmp1 = TLI.LowerOperation(Result, DAG);
2629 if (Tmp1.Val) Result = Tmp1;
2630 }
2631 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002632 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002633 Result = ExpandLegalINT_TO_FP(isSigned,
2634 LegalizeOp(Node->getOperand(0)),
2635 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002636 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002637 case TargetLowering::Promote:
2638 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2639 Node->getValueType(0),
2640 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002641 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002642 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002643 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002644 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002645 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2646 Node->getValueType(0), Node->getOperand(0));
2647 break;
2648 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002649 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002650 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002651 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2652 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002653 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002654 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2655 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002656 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002657 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2658 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002659 break;
2660 }
2661 break;
2662 }
2663 case ISD::TRUNCATE:
2664 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2665 case Legal:
2666 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002667 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002668 break;
2669 case Expand:
2670 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2671
2672 // Since the result is legal, we should just be able to truncate the low
2673 // part of the source.
2674 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2675 break;
2676 case Promote:
2677 Result = PromoteOp(Node->getOperand(0));
2678 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2679 break;
2680 }
2681 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002682
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002683 case ISD::FP_TO_SINT:
2684 case ISD::FP_TO_UINT:
2685 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2686 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002687 Tmp1 = LegalizeOp(Node->getOperand(0));
2688
Chris Lattner1618beb2005-07-29 00:11:56 +00002689 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2690 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002691 case TargetLowering::Custom:
2692 isCustom = true;
2693 // FALLTHROUGH
2694 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002695 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002696 if (isCustom) {
2697 Tmp1 = TLI.LowerOperation(Result, DAG);
2698 if (Tmp1.Val) Result = Tmp1;
2699 }
2700 break;
2701 case TargetLowering::Promote:
2702 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2703 Node->getOpcode() == ISD::FP_TO_SINT);
2704 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002705 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002706 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2707 SDOperand True, False;
2708 MVT::ValueType VT = Node->getOperand(0).getValueType();
2709 MVT::ValueType NVT = Node->getValueType(0);
2710 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2711 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2712 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2713 Node->getOperand(0), Tmp2, ISD::SETLT);
2714 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2715 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002716 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002717 Tmp2));
2718 False = DAG.getNode(ISD::XOR, NVT, False,
2719 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002720 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2721 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002722 } else {
2723 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2724 }
2725 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002726 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002727 break;
2728 case Expand:
2729 assert(0 && "Shouldn't need to expand other operators here!");
2730 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002731 Tmp1 = PromoteOp(Node->getOperand(0));
2732 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2733 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002734 break;
2735 }
2736 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002737
Chris Lattner13c78e22005-09-02 00:18:10 +00002738 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002739 case ISD::ZERO_EXTEND:
2740 case ISD::SIGN_EXTEND:
2741 case ISD::FP_EXTEND:
2742 case ISD::FP_ROUND:
2743 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002744 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002745 case Legal:
2746 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002747 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002748 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002749 case Promote:
2750 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002751 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002752 Tmp1 = PromoteOp(Node->getOperand(0));
2753 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002754 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002755 case ISD::ZERO_EXTEND:
2756 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002757 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002758 Result = DAG.getZeroExtendInReg(Result,
2759 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002760 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002761 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002762 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002763 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002764 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002765 Result,
2766 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002767 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002768 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002769 Result = PromoteOp(Node->getOperand(0));
2770 if (Result.getValueType() != Op.getValueType())
2771 // Dynamically dead while we have only 2 FP types.
2772 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2773 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002774 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002775 Result = PromoteOp(Node->getOperand(0));
2776 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2777 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002778 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002779 }
2780 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002781 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002782 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002783 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002784 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002785
2786 // If this operation is not supported, convert it to a shl/shr or load/store
2787 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002788 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2789 default: assert(0 && "This action not supported for this op yet!");
2790 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002792 break;
2793 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002794 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002795 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002796 // NOTE: we could fall back on load/store here too for targets without
2797 // SAR. However, it is doubtful that any exist.
2798 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2799 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002800 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002801 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2802 Node->getOperand(0), ShiftCst);
2803 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2804 Result, ShiftCst);
2805 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2806 // The only way we can lower this is to turn it into a STORETRUNC,
2807 // EXTLOAD pair, targetting a temporary location (a stack slot).
2808
2809 // NOTE: there is a choice here between constantly creating new stack
2810 // slots and always reusing the same one. We currently always create
2811 // new ones, as reuse may inhibit scheduling.
2812 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00002813 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
2814 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002815 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002816 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002817 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2818 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2819 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002820 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002821 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002822 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2823 Result, StackSlot, DAG.getSrcValue(NULL),
2824 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002825 } else {
2826 assert(0 && "Unknown op");
2827 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002828 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002829 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002830 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002831 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002832 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002833
Chris Lattner4ddd2832006-04-08 04:13:17 +00002834 assert(Result.getValueType() == Op.getValueType() &&
2835 "Bad legalization!");
2836
Chris Lattner456a93a2006-01-28 07:39:30 +00002837 // Make sure that the generated code is itself legal.
2838 if (Result != Op)
2839 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002840
Chris Lattner45982da2005-05-12 16:53:42 +00002841 // Note that LegalizeOp may be reentered even from single-use nodes, which
2842 // means that we always must cache transformed nodes.
2843 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002844 return Result;
2845}
2846
Chris Lattner8b6fa222005-01-15 22:16:26 +00002847/// PromoteOp - Given an operation that produces a value in an invalid type,
2848/// promote it to compute the value into a larger type. The produced value will
2849/// have the correct bits for the low portion of the register, but no guarantee
2850/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002851SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2852 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002853 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002854 assert(getTypeAction(VT) == Promote &&
2855 "Caller should expand or legalize operands that are not promotable!");
2856 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2857 "Cannot promote to smaller type!");
2858
Chris Lattner03c85462005-01-15 05:21:40 +00002859 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002860 SDOperand Result;
2861 SDNode *Node = Op.Val;
2862
Chris Lattner6fdcb252005-09-02 20:32:45 +00002863 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2864 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002865
Chris Lattner03c85462005-01-15 05:21:40 +00002866 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002867 case ISD::CopyFromReg:
2868 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002869 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00002870#ifndef NDEBUG
Chris Lattner03c85462005-01-15 05:21:40 +00002871 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00002872#endif
Chris Lattner03c85462005-01-15 05:21:40 +00002873 assert(0 && "Do not know how to promote this operator!");
2874 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002875 case ISD::UNDEF:
2876 Result = DAG.getNode(ISD::UNDEF, NVT);
2877 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002878 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002879 if (VT != MVT::i1)
2880 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2881 else
2882 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002883 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2884 break;
2885 case ISD::ConstantFP:
2886 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2887 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2888 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002889
Chris Lattner82fbfb62005-01-18 02:59:52 +00002890 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002891 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002892 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2893 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002894 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00002895
Chris Lattner03c85462005-01-15 05:21:40 +00002896 case ISD::TRUNCATE:
2897 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2898 case Legal:
2899 Result = LegalizeOp(Node->getOperand(0));
2900 assert(Result.getValueType() >= NVT &&
2901 "This truncation doesn't make sense!");
2902 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2903 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2904 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002905 case Promote:
2906 // The truncation is not required, because we don't guarantee anything
2907 // about high bits anyway.
2908 Result = PromoteOp(Node->getOperand(0));
2909 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002910 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002911 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2912 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002913 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002914 }
2915 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002916 case ISD::SIGN_EXTEND:
2917 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002918 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002919 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2920 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2921 case Legal:
2922 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002923 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002924 break;
2925 case Promote:
2926 // Promote the reg if it's smaller.
2927 Result = PromoteOp(Node->getOperand(0));
2928 // The high bits are not guaranteed to be anything. Insert an extend.
2929 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002930 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002931 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002932 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002933 Result = DAG.getZeroExtendInReg(Result,
2934 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002935 break;
2936 }
2937 break;
Chris Lattner35481892005-12-23 00:16:34 +00002938 case ISD::BIT_CONVERT:
2939 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2940 Result = PromoteOp(Result);
2941 break;
2942
Chris Lattner8b6fa222005-01-15 22:16:26 +00002943 case ISD::FP_EXTEND:
2944 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2945 case ISD::FP_ROUND:
2946 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2947 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2948 case Promote: assert(0 && "Unreachable with 2 FP types!");
2949 case Legal:
2950 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002951 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002952 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002953 break;
2954 }
2955 break;
2956
2957 case ISD::SINT_TO_FP:
2958 case ISD::UINT_TO_FP:
2959 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2960 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002961 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002962 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002963 break;
2964
2965 case Promote:
2966 Result = PromoteOp(Node->getOperand(0));
2967 if (Node->getOpcode() == ISD::SINT_TO_FP)
2968 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002969 Result,
2970 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002971 else
Chris Lattner23993562005-04-13 02:38:47 +00002972 Result = DAG.getZeroExtendInReg(Result,
2973 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002974 // No extra round required here.
2975 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002976 break;
2977 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002978 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2979 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002980 // Round if we cannot tolerate excess precision.
2981 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002982 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2983 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002984 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002985 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002986 break;
2987
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002988 case ISD::SIGN_EXTEND_INREG:
2989 Result = PromoteOp(Node->getOperand(0));
2990 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2991 Node->getOperand(1));
2992 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002993 case ISD::FP_TO_SINT:
2994 case ISD::FP_TO_UINT:
2995 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2996 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002997 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002998 break;
2999 case Promote:
3000 // The input result is prerounded, so we don't have to do anything
3001 // special.
3002 Tmp1 = PromoteOp(Node->getOperand(0));
3003 break;
3004 case Expand:
3005 assert(0 && "not implemented");
3006 }
Nate Begemand2558e32005-08-14 01:20:53 +00003007 // If we're promoting a UINT to a larger size, check to see if the new node
3008 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3009 // we can use that instead. This allows us to generate better code for
3010 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3011 // legal, such as PowerPC.
3012 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003013 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003014 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3015 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003016 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3017 } else {
3018 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3019 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003020 break;
3021
Chris Lattner2c8086f2005-04-02 05:00:07 +00003022 case ISD::FABS:
3023 case ISD::FNEG:
3024 Tmp1 = PromoteOp(Node->getOperand(0));
3025 assert(Tmp1.getValueType() == NVT);
3026 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3027 // NOTE: we do not have to do any extra rounding here for
3028 // NoExcessFPPrecision, because we know the input will have the appropriate
3029 // precision, and these operations don't modify precision at all.
3030 break;
3031
Chris Lattnerda6ba872005-04-28 21:44:33 +00003032 case ISD::FSQRT:
3033 case ISD::FSIN:
3034 case ISD::FCOS:
3035 Tmp1 = PromoteOp(Node->getOperand(0));
3036 assert(Tmp1.getValueType() == NVT);
3037 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003038 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003039 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3040 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003041 break;
3042
Chris Lattner03c85462005-01-15 05:21:40 +00003043 case ISD::AND:
3044 case ISD::OR:
3045 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003046 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003047 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003048 case ISD::MUL:
3049 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003050 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003051 // that too is okay if they are integer operations.
3052 Tmp1 = PromoteOp(Node->getOperand(0));
3053 Tmp2 = PromoteOp(Node->getOperand(1));
3054 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3055 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003056 break;
3057 case ISD::FADD:
3058 case ISD::FSUB:
3059 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003060 Tmp1 = PromoteOp(Node->getOperand(0));
3061 Tmp2 = PromoteOp(Node->getOperand(1));
3062 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3063 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3064
3065 // Floating point operations will give excess precision that we may not be
3066 // able to tolerate. If we DO allow excess precision, just leave it,
3067 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003068 // FIXME: Why would we need to round FP ops more than integer ones?
3069 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003070 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003071 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3072 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003073 break;
3074
Chris Lattner8b6fa222005-01-15 22:16:26 +00003075 case ISD::SDIV:
3076 case ISD::SREM:
3077 // These operators require that their input be sign extended.
3078 Tmp1 = PromoteOp(Node->getOperand(0));
3079 Tmp2 = PromoteOp(Node->getOperand(1));
3080 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003081 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3082 DAG.getValueType(VT));
3083 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3084 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003085 }
3086 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3087
3088 // Perform FP_ROUND: this is probably overly pessimistic.
3089 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003090 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3091 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003092 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003093 case ISD::FDIV:
3094 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003095 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003096 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3098 case Legal:
3099 Tmp1 = LegalizeOp(Node->getOperand(0));
3100 break;
3101 case Promote:
3102 Tmp1 = PromoteOp(Node->getOperand(0));
3103 break;
3104 case Expand:
3105 assert(0 && "not implemented");
3106 }
3107 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3108 case Legal:
3109 Tmp2 = LegalizeOp(Node->getOperand(1));
3110 break;
3111 case Promote:
3112 Tmp2 = PromoteOp(Node->getOperand(1));
3113 break;
3114 case Expand:
3115 assert(0 && "not implemented");
3116 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003117 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3118
3119 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003120 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003121 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3122 DAG.getValueType(VT));
3123 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003124
3125 case ISD::UDIV:
3126 case ISD::UREM:
3127 // These operators require that their input be zero extended.
3128 Tmp1 = PromoteOp(Node->getOperand(0));
3129 Tmp2 = PromoteOp(Node->getOperand(1));
3130 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003131 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3132 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003133 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3134 break;
3135
3136 case ISD::SHL:
3137 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003138 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003139 break;
3140 case ISD::SRA:
3141 // The input value must be properly sign extended.
3142 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003143 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3144 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003145 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003146 break;
3147 case ISD::SRL:
3148 // The input value must be properly zero extended.
3149 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003150 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003151 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003152 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003153
3154 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003155 Tmp1 = Node->getOperand(0); // Get the chain.
3156 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003157 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3158 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3159 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3160 } else {
3161 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3162 Node->getOperand(2));
3163 // Increment the pointer, VAList, to the next vaarg
3164 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3165 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3166 TLI.getPointerTy()));
3167 // Store the incremented VAList to the legalized pointer
3168 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3169 Node->getOperand(2));
3170 // Load the actual argument out of the pointer VAList
3171 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3172 DAG.getSrcValue(0), VT);
3173 }
3174 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003175 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003176 break;
3177
Chris Lattner03c85462005-01-15 05:21:40 +00003178 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003179 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3180 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003181 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003182 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003183 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003184 case ISD::SEXTLOAD:
3185 case ISD::ZEXTLOAD:
3186 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003187 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3188 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00003189 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003190 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003191 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003192 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003193 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003194 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3195 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003196 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003197 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003198 case ISD::SELECT_CC:
3199 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3200 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3201 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003202 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003203 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003204 case ISD::BSWAP:
3205 Tmp1 = Node->getOperand(0);
3206 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3207 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3208 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3209 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3210 TLI.getShiftAmountTy()));
3211 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003212 case ISD::CTPOP:
3213 case ISD::CTTZ:
3214 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003215 // Zero extend the argument
3216 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003217 // Perform the larger operation, then subtract if needed.
3218 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003219 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003220 case ISD::CTPOP:
3221 Result = Tmp1;
3222 break;
3223 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003224 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003225 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003226 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003227 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003228 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003229 break;
3230 case ISD::CTLZ:
3231 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003232 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3233 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003234 getSizeInBits(VT), NVT));
3235 break;
3236 }
3237 break;
Chris Lattner15972212006-03-31 17:55:51 +00003238 case ISD::VEXTRACT_VECTOR_ELT:
3239 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3240 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003241 case ISD::EXTRACT_VECTOR_ELT:
3242 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3243 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003244 }
3245
3246 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003247
3248 // Make sure the result is itself legal.
3249 Result = LegalizeOp(Result);
3250
3251 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003252 AddPromotedOperand(Op, Result);
3253 return Result;
3254}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003255
Chris Lattner15972212006-03-31 17:55:51 +00003256/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3257/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3258/// on the vector type. The return type of this matches the element type of the
3259/// vector, which may not be legal for the target.
3260SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3261 // We know that operand #0 is the Vec vector. If the index is a constant
3262 // or if the invec is a supported hardware type, we can use it. Otherwise,
3263 // lower to a store then an indexed load.
3264 SDOperand Vec = Op.getOperand(0);
3265 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3266
3267 SDNode *InVal = Vec.Val;
3268 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3269 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3270
3271 // Figure out if there is a Packed type corresponding to this Vector
3272 // type. If so, convert to the packed type.
3273 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3274 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3275 // Turn this into a packed extract_vector_elt operation.
3276 Vec = PackVectorOp(Vec, TVT);
3277 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3278 } else if (NumElems == 1) {
3279 // This must be an access of the only element. Return it.
3280 return PackVectorOp(Vec, EVT);
3281 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3282 SDOperand Lo, Hi;
3283 SplitVectorOp(Vec, Lo, Hi);
3284 if (CIdx->getValue() < NumElems/2) {
3285 Vec = Lo;
3286 } else {
3287 Vec = Hi;
3288 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3289 }
3290
3291 // It's now an extract from the appropriate high or low part. Recurse.
3292 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3293 return LowerVEXTRACT_VECTOR_ELT(Op);
3294 } else {
3295 // Variable index case for extract element.
3296 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3297 assert(0 && "unimp!");
3298 return SDOperand();
3299 }
3300}
3301
Chris Lattner4aab2f42006-04-02 05:06:04 +00003302/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3303/// memory traffic.
3304SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3305 SDOperand Vector = Op.getOperand(0);
3306 SDOperand Idx = Op.getOperand(1);
3307
3308 // If the target doesn't support this, store the value to a temporary
3309 // stack slot, then LOAD the scalar element back out.
3310 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3311 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3312 Vector, StackPtr, DAG.getSrcValue(NULL));
3313
3314 // Add the offset to the index.
3315 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3316 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3317 DAG.getConstant(EltSize, Idx.getValueType()));
3318 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3319
3320 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3321}
3322
Chris Lattner15972212006-03-31 17:55:51 +00003323
Nate Begeman750ac1b2006-02-01 07:19:44 +00003324/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3325/// with condition CC on the current target. This usually involves legalizing
3326/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3327/// there may be no choice but to create a new SetCC node to represent the
3328/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3329/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3330void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3331 SDOperand &RHS,
3332 SDOperand &CC) {
3333 SDOperand Tmp1, Tmp2, Result;
3334
3335 switch (getTypeAction(LHS.getValueType())) {
3336 case Legal:
3337 Tmp1 = LegalizeOp(LHS); // LHS
3338 Tmp2 = LegalizeOp(RHS); // RHS
3339 break;
3340 case Promote:
3341 Tmp1 = PromoteOp(LHS); // LHS
3342 Tmp2 = PromoteOp(RHS); // RHS
3343
3344 // If this is an FP compare, the operands have already been extended.
3345 if (MVT::isInteger(LHS.getValueType())) {
3346 MVT::ValueType VT = LHS.getValueType();
3347 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3348
3349 // Otherwise, we have to insert explicit sign or zero extends. Note
3350 // that we could insert sign extends for ALL conditions, but zero extend
3351 // is cheaper on many machines (an AND instead of two shifts), so prefer
3352 // it.
3353 switch (cast<CondCodeSDNode>(CC)->get()) {
3354 default: assert(0 && "Unknown integer comparison!");
3355 case ISD::SETEQ:
3356 case ISD::SETNE:
3357 case ISD::SETUGE:
3358 case ISD::SETUGT:
3359 case ISD::SETULE:
3360 case ISD::SETULT:
3361 // ALL of these operations will work if we either sign or zero extend
3362 // the operands (including the unsigned comparisons!). Zero extend is
3363 // usually a simpler/cheaper operation, so prefer it.
3364 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3365 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3366 break;
3367 case ISD::SETGE:
3368 case ISD::SETGT:
3369 case ISD::SETLT:
3370 case ISD::SETLE:
3371 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3372 DAG.getValueType(VT));
3373 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3374 DAG.getValueType(VT));
3375 break;
3376 }
3377 }
3378 break;
3379 case Expand:
3380 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3381 ExpandOp(LHS, LHSLo, LHSHi);
3382 ExpandOp(RHS, RHSLo, RHSHi);
3383 switch (cast<CondCodeSDNode>(CC)->get()) {
3384 case ISD::SETEQ:
3385 case ISD::SETNE:
3386 if (RHSLo == RHSHi)
3387 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3388 if (RHSCST->isAllOnesValue()) {
3389 // Comparison to -1.
3390 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3391 Tmp2 = RHSLo;
3392 break;
3393 }
3394
3395 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3396 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3397 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3398 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3399 break;
3400 default:
3401 // If this is a comparison of the sign bit, just look at the top part.
3402 // X > -1, x < 0
3403 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3404 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3405 CST->getValue() == 0) || // X < 0
3406 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3407 CST->isAllOnesValue())) { // X > -1
3408 Tmp1 = LHSHi;
3409 Tmp2 = RHSHi;
3410 break;
3411 }
3412
3413 // FIXME: This generated code sucks.
3414 ISD::CondCode LowCC;
3415 switch (cast<CondCodeSDNode>(CC)->get()) {
3416 default: assert(0 && "Unknown integer setcc!");
3417 case ISD::SETLT:
3418 case ISD::SETULT: LowCC = ISD::SETULT; break;
3419 case ISD::SETGT:
3420 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3421 case ISD::SETLE:
3422 case ISD::SETULE: LowCC = ISD::SETULE; break;
3423 case ISD::SETGE:
3424 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3425 }
3426
3427 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3428 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3429 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3430
3431 // NOTE: on targets without efficient SELECT of bools, we can always use
3432 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3433 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3434 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3435 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3436 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3437 Result, Tmp1, Tmp2));
3438 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003439 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003440 }
3441 }
3442 LHS = Tmp1;
3443 RHS = Tmp2;
3444}
3445
Chris Lattner35481892005-12-23 00:16:34 +00003446/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003447/// The resultant code need not be legal. Note that SrcOp is the input operand
3448/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003449SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3450 SDOperand SrcOp) {
3451 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003452 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003453
3454 // Emit a store to the stack slot.
3455 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003456 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003457 // Result is a load from the stack slot.
3458 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3459}
3460
Chris Lattner4352cc92006-04-04 17:23:26 +00003461SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3462 // Create a vector sized/aligned stack slot, store the value to element #0,
3463 // then load the whole vector back out.
3464 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3465 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3466 Node->getOperand(0), StackPtr,
3467 DAG.getSrcValue(NULL));
3468 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3469}
3470
3471
Chris Lattnerce872152006-03-19 06:31:19 +00003472/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3473/// support the operation, but do support the resultant packed vector type.
3474SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3475
3476 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003477 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003478 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003479 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003480 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003481 std::map<SDOperand, std::vector<unsigned> > Values;
3482 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003483 bool isConstant = true;
3484 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3485 SplatValue.getOpcode() != ISD::UNDEF)
3486 isConstant = false;
3487
Evan Cheng033e6812006-03-24 01:17:21 +00003488 for (unsigned i = 1; i < NumElems; ++i) {
3489 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003490 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003491 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003492 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003493 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003494 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003495
3496 // If this isn't a constant element or an undef, we can't use a constant
3497 // pool load.
3498 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3499 V.getOpcode() != ISD::UNDEF)
3500 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003501 }
3502
3503 if (isOnlyLowElement) {
3504 // If the low element is an undef too, then this whole things is an undef.
3505 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3506 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3507 // Otherwise, turn this into a scalar_to_vector node.
3508 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3509 Node->getOperand(0));
3510 }
3511
Chris Lattner2eb86532006-03-24 07:29:17 +00003512 // If all elements are constants, create a load from the constant pool.
3513 if (isConstant) {
3514 MVT::ValueType VT = Node->getValueType(0);
3515 const Type *OpNTy =
3516 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3517 std::vector<Constant*> CV;
3518 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3519 if (ConstantFPSDNode *V =
3520 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3521 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3522 } else if (ConstantSDNode *V =
3523 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3524 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3525 } else {
3526 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3527 CV.push_back(UndefValue::get(OpNTy));
3528 }
3529 }
3530 Constant *CP = ConstantPacked::get(CV);
3531 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3532 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3533 DAG.getSrcValue(NULL));
3534 }
3535
Chris Lattner87100e02006-03-20 01:52:29 +00003536 if (SplatValue.Val) { // Splat of one value?
3537 // Build the shuffle constant vector: <0, 0, 0, 0>
3538 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003539 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003540 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003541 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003542 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3543
3544 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003545 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003546 // Get the splatted value into the low element of a vector register.
3547 SDOperand LowValVec =
3548 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3549
3550 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3551 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3552 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3553 SplatMask);
3554 }
3555 }
3556
Evan Cheng033e6812006-03-24 01:17:21 +00003557 // If there are only two unique elements, we may be able to turn this into a
3558 // vector shuffle.
3559 if (Values.size() == 2) {
3560 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3561 MVT::ValueType MaskVT =
3562 MVT::getIntVectorWithNumElements(NumElems);
3563 std::vector<SDOperand> MaskVec(NumElems);
3564 unsigned i = 0;
3565 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3566 E = Values.end(); I != E; ++I) {
3567 for (std::vector<unsigned>::iterator II = I->second.begin(),
3568 EE = I->second.end(); II != EE; ++II)
3569 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3570 i += NumElems;
3571 }
3572 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3573
3574 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003575 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3576 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng033e6812006-03-24 01:17:21 +00003577 std::vector<SDOperand> Ops;
3578 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3579 E = Values.end(); I != E; ++I) {
3580 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3581 I->first);
3582 Ops.push_back(Op);
3583 }
3584 Ops.push_back(ShuffleMask);
3585
3586 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3587 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3588 }
3589 }
Chris Lattnerce872152006-03-19 06:31:19 +00003590
3591 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3592 // aligned object on the stack, store each element into it, then load
3593 // the result as a vector.
3594 MVT::ValueType VT = Node->getValueType(0);
3595 // Create the stack frame object.
3596 SDOperand FIPtr = CreateStackTemporary(VT);
3597
3598 // Emit a store of each element to the stack slot.
3599 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003600 unsigned TypeByteSize =
3601 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3602 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3603 // Store (in the right endianness) the elements to memory.
3604 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3605 // Ignore undef elements.
3606 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3607
Chris Lattner841c8822006-03-22 01:46:54 +00003608 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003609
3610 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3611 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3612
3613 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3614 Node->getOperand(i), Idx,
3615 DAG.getSrcValue(NULL)));
3616 }
3617
3618 SDOperand StoreChain;
3619 if (!Stores.empty()) // Not all undef elements?
3620 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3621 else
3622 StoreChain = DAG.getEntryNode();
3623
3624 // Result is a load from the stack slot.
3625 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3626}
3627
3628/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3629/// specified value type.
3630SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3631 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3632 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3633 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3634 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3635}
3636
Chris Lattner5b359c62005-04-02 04:00:59 +00003637void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3638 SDOperand Op, SDOperand Amt,
3639 SDOperand &Lo, SDOperand &Hi) {
3640 // Expand the subcomponents.
3641 SDOperand LHSL, LHSH;
3642 ExpandOp(Op, LHSL, LHSH);
3643
3644 std::vector<SDOperand> Ops;
3645 Ops.push_back(LHSL);
3646 Ops.push_back(LHSH);
3647 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003648 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003649 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003650 Hi = Lo.getValue(1);
3651}
3652
3653
Chris Lattnere34b3962005-01-19 04:19:40 +00003654/// ExpandShift - Try to find a clever way to expand this shift operation out to
3655/// smaller elements. If we can't find a way that is more efficient than a
3656/// libcall on this target, return false. Otherwise, return true with the
3657/// low-parts expanded into Lo and Hi.
3658bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3659 SDOperand &Lo, SDOperand &Hi) {
3660 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3661 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003662
Chris Lattnere34b3962005-01-19 04:19:40 +00003663 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003664 SDOperand ShAmt = LegalizeOp(Amt);
3665 MVT::ValueType ShTy = ShAmt.getValueType();
3666 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3667 unsigned NVTBits = MVT::getSizeInBits(NVT);
3668
3669 // Handle the case when Amt is an immediate. Other cases are currently broken
3670 // and are disabled.
3671 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3672 unsigned Cst = CN->getValue();
3673 // Expand the incoming operand to be shifted, so that we have its parts
3674 SDOperand InL, InH;
3675 ExpandOp(Op, InL, InH);
3676 switch(Opc) {
3677 case ISD::SHL:
3678 if (Cst > VTBits) {
3679 Lo = DAG.getConstant(0, NVT);
3680 Hi = DAG.getConstant(0, NVT);
3681 } else if (Cst > NVTBits) {
3682 Lo = DAG.getConstant(0, NVT);
3683 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003684 } else if (Cst == NVTBits) {
3685 Lo = DAG.getConstant(0, NVT);
3686 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003687 } else {
3688 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3689 Hi = DAG.getNode(ISD::OR, NVT,
3690 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3691 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3692 }
3693 return true;
3694 case ISD::SRL:
3695 if (Cst > VTBits) {
3696 Lo = DAG.getConstant(0, NVT);
3697 Hi = DAG.getConstant(0, NVT);
3698 } else if (Cst > NVTBits) {
3699 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3700 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003701 } else if (Cst == NVTBits) {
3702 Lo = InH;
3703 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003704 } else {
3705 Lo = DAG.getNode(ISD::OR, NVT,
3706 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3707 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3708 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3709 }
3710 return true;
3711 case ISD::SRA:
3712 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003713 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003714 DAG.getConstant(NVTBits-1, ShTy));
3715 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003716 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003717 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003718 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003719 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003720 } else if (Cst == NVTBits) {
3721 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003722 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003723 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003724 } else {
3725 Lo = DAG.getNode(ISD::OR, NVT,
3726 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3727 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3728 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3729 }
3730 return true;
3731 }
3732 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003733 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003734}
Chris Lattner77e77a62005-01-21 06:05:23 +00003735
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003736
Chris Lattner77e77a62005-01-21 06:05:23 +00003737// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3738// does not fit into a register, return the lo part and set the hi part to the
3739// by-reg argument. If it does fit into a single register, return the result
3740// and leave the Hi part unset.
3741SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3742 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003743 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3744 // The input chain to this libcall is the entry node of the function.
3745 // Legalizing the call will automatically add the previous call to the
3746 // dependence.
3747 SDOperand InChain = DAG.getEntryNode();
3748
Chris Lattner77e77a62005-01-21 06:05:23 +00003749 TargetLowering::ArgListTy Args;
3750 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3751 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3752 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3753 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3754 }
3755 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003756
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003757 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003758 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003759 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003760 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3761 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003762
Chris Lattner6831a812006-02-13 09:18:02 +00003763 // Legalize the call sequence, starting with the chain. This will advance
3764 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3765 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3766 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003767 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003768 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003769 default: assert(0 && "Unknown thing");
3770 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003771 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003772 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003773 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003774 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003775 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003776 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003777 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003778}
3779
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003780
Chris Lattner77e77a62005-01-21 06:05:23 +00003781/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3782/// destination type is legal.
3783SDOperand SelectionDAGLegalize::
3784ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003785 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003786 assert(getTypeAction(Source.getValueType()) == Expand &&
3787 "This is not an expansion!");
3788 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3789
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003790 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003791 assert(Source.getValueType() == MVT::i64 &&
3792 "This only works for 64-bit -> FP");
3793 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3794 // incoming integer is set. To handle this, we dynamically test to see if
3795 // it is set, and, if so, add a fudge factor.
3796 SDOperand Lo, Hi;
3797 ExpandOp(Source, Lo, Hi);
3798
Chris Lattner66de05b2005-05-13 04:45:13 +00003799 // If this is unsigned, and not supported, first perform the conversion to
3800 // signed, then adjust the result if the sign bit is set.
3801 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3802 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3803
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003804 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3805 DAG.getConstant(0, Hi.getValueType()),
3806 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003807 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3808 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3809 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003810 uint64_t FF = 0x5f800000ULL;
3811 if (TLI.isLittleEndian()) FF <<= 32;
3812 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003813
Chris Lattner5839bf22005-08-26 17:15:30 +00003814 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003815 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3816 SDOperand FudgeInReg;
3817 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003818 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3819 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003820 else {
3821 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003822 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3823 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003824 }
Chris Lattner473a9902005-09-29 06:44:39 +00003825 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003826 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003827
Chris Lattnera88a2602005-05-14 05:33:54 +00003828 // Check to see if the target has a custom way to lower this. If so, use it.
3829 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3830 default: assert(0 && "This action not implemented for this operation!");
3831 case TargetLowering::Legal:
3832 case TargetLowering::Expand:
3833 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003834 case TargetLowering::Custom: {
3835 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3836 Source), DAG);
3837 if (NV.Val)
3838 return LegalizeOp(NV);
3839 break; // The target decided this was legal after all
3840 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003841 }
3842
Chris Lattner13689e22005-05-12 07:00:44 +00003843 // Expand the source, then glue it back together for the call. We must expand
3844 // the source in case it is shared (this pass of legalize must traverse it).
3845 SDOperand SrcLo, SrcHi;
3846 ExpandOp(Source, SrcLo, SrcHi);
3847 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3848
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003849 const char *FnName = 0;
3850 if (DestTy == MVT::f32)
3851 FnName = "__floatdisf";
3852 else {
3853 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3854 FnName = "__floatdidf";
3855 }
Chris Lattner6831a812006-02-13 09:18:02 +00003856
3857 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3858 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003859 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003860}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003861
Chris Lattner22cde6a2006-01-28 08:25:58 +00003862/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3863/// INT_TO_FP operation of the specified operand when the target requests that
3864/// we expand it. At this point, we know that the result and operand types are
3865/// legal for the target.
3866SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3867 SDOperand Op0,
3868 MVT::ValueType DestVT) {
3869 if (Op0.getValueType() == MVT::i32) {
3870 // simple 32-bit [signed|unsigned] integer to float/double expansion
3871
3872 // get the stack frame index of a 8 byte buffer
3873 MachineFunction &MF = DAG.getMachineFunction();
3874 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3875 // get address of 8 byte buffer
3876 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3877 // word offset constant for Hi/Lo address computation
3878 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3879 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003880 SDOperand Hi = StackSlot;
3881 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3882 if (TLI.isLittleEndian())
3883 std::swap(Hi, Lo);
3884
Chris Lattner22cde6a2006-01-28 08:25:58 +00003885 // if signed map to unsigned space
3886 SDOperand Op0Mapped;
3887 if (isSigned) {
3888 // constant used to invert sign bit (signed to unsigned mapping)
3889 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3890 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3891 } else {
3892 Op0Mapped = Op0;
3893 }
3894 // store the lo of the constructed double - based on integer input
3895 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3896 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3897 // initial hi portion of constructed double
3898 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3899 // store the hi of the constructed double - biased exponent
3900 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3901 InitialHi, Hi, DAG.getSrcValue(NULL));
3902 // load the constructed double
3903 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3904 DAG.getSrcValue(NULL));
3905 // FP constant to bias correct the final result
3906 SDOperand Bias = DAG.getConstantFP(isSigned ?
3907 BitsToDouble(0x4330000080000000ULL)
3908 : BitsToDouble(0x4330000000000000ULL),
3909 MVT::f64);
3910 // subtract the bias
3911 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3912 // final result
3913 SDOperand Result;
3914 // handle final rounding
3915 if (DestVT == MVT::f64) {
3916 // do nothing
3917 Result = Sub;
3918 } else {
3919 // if f32 then cast to f32
3920 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3921 }
3922 return Result;
3923 }
3924 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3925 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3926
3927 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3928 DAG.getConstant(0, Op0.getValueType()),
3929 ISD::SETLT);
3930 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3931 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3932 SignSet, Four, Zero);
3933
3934 // If the sign bit of the integer is set, the large number will be treated
3935 // as a negative number. To counteract this, the dynamic code adds an
3936 // offset depending on the data type.
3937 uint64_t FF;
3938 switch (Op0.getValueType()) {
3939 default: assert(0 && "Unsupported integer type!");
3940 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3941 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3942 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3943 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3944 }
3945 if (TLI.isLittleEndian()) FF <<= 32;
3946 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3947
3948 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3949 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3950 SDOperand FudgeInReg;
3951 if (DestVT == MVT::f32)
3952 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3953 DAG.getSrcValue(NULL));
3954 else {
3955 assert(DestVT == MVT::f64 && "Unexpected conversion");
3956 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3957 DAG.getEntryNode(), CPIdx,
3958 DAG.getSrcValue(NULL), MVT::f32));
3959 }
3960
3961 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3962}
3963
3964/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3965/// *INT_TO_FP operation of the specified operand when the target requests that
3966/// we promote it. At this point, we know that the result and operand types are
3967/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3968/// operation that takes a larger input.
3969SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3970 MVT::ValueType DestVT,
3971 bool isSigned) {
3972 // First step, figure out the appropriate *INT_TO_FP operation to use.
3973 MVT::ValueType NewInTy = LegalOp.getValueType();
3974
3975 unsigned OpToUse = 0;
3976
3977 // Scan for the appropriate larger type to use.
3978 while (1) {
3979 NewInTy = (MVT::ValueType)(NewInTy+1);
3980 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3981
3982 // If the target supports SINT_TO_FP of this type, use it.
3983 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3984 default: break;
3985 case TargetLowering::Legal:
3986 if (!TLI.isTypeLegal(NewInTy))
3987 break; // Can't use this datatype.
3988 // FALL THROUGH.
3989 case TargetLowering::Custom:
3990 OpToUse = ISD::SINT_TO_FP;
3991 break;
3992 }
3993 if (OpToUse) break;
3994 if (isSigned) continue;
3995
3996 // If the target supports UINT_TO_FP of this type, use it.
3997 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3998 default: break;
3999 case TargetLowering::Legal:
4000 if (!TLI.isTypeLegal(NewInTy))
4001 break; // Can't use this datatype.
4002 // FALL THROUGH.
4003 case TargetLowering::Custom:
4004 OpToUse = ISD::UINT_TO_FP;
4005 break;
4006 }
4007 if (OpToUse) break;
4008
4009 // Otherwise, try a larger type.
4010 }
4011
4012 // Okay, we found the operation and type to use. Zero extend our input to the
4013 // desired type then run the operation on it.
4014 return DAG.getNode(OpToUse, DestVT,
4015 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4016 NewInTy, LegalOp));
4017}
4018
4019/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4020/// FP_TO_*INT operation of the specified operand when the target requests that
4021/// we promote it. At this point, we know that the result and operand types are
4022/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4023/// operation that returns a larger result.
4024SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4025 MVT::ValueType DestVT,
4026 bool isSigned) {
4027 // First step, figure out the appropriate FP_TO*INT operation to use.
4028 MVT::ValueType NewOutTy = DestVT;
4029
4030 unsigned OpToUse = 0;
4031
4032 // Scan for the appropriate larger type to use.
4033 while (1) {
4034 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4035 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4036
4037 // If the target supports FP_TO_SINT returning this type, use it.
4038 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4039 default: break;
4040 case TargetLowering::Legal:
4041 if (!TLI.isTypeLegal(NewOutTy))
4042 break; // Can't use this datatype.
4043 // FALL THROUGH.
4044 case TargetLowering::Custom:
4045 OpToUse = ISD::FP_TO_SINT;
4046 break;
4047 }
4048 if (OpToUse) break;
4049
4050 // If the target supports FP_TO_UINT of this type, use it.
4051 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4052 default: break;
4053 case TargetLowering::Legal:
4054 if (!TLI.isTypeLegal(NewOutTy))
4055 break; // Can't use this datatype.
4056 // FALL THROUGH.
4057 case TargetLowering::Custom:
4058 OpToUse = ISD::FP_TO_UINT;
4059 break;
4060 }
4061 if (OpToUse) break;
4062
4063 // Otherwise, try a larger type.
4064 }
4065
4066 // Okay, we found the operation and type to use. Truncate the result of the
4067 // extended FP_TO_*INT operation to the desired size.
4068 return DAG.getNode(ISD::TRUNCATE, DestVT,
4069 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4070}
4071
4072/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4073///
4074SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4075 MVT::ValueType VT = Op.getValueType();
4076 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4077 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4078 switch (VT) {
4079 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4080 case MVT::i16:
4081 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4082 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4083 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4084 case MVT::i32:
4085 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4086 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4087 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4088 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4089 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4090 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4091 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4092 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4093 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4094 case MVT::i64:
4095 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4096 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4097 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4098 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4099 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4100 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4101 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4102 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4103 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4104 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4105 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4106 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4107 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4108 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4109 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4110 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4111 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4112 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4113 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4114 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4115 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4116 }
4117}
4118
4119/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4120///
4121SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4122 switch (Opc) {
4123 default: assert(0 && "Cannot expand this yet!");
4124 case ISD::CTPOP: {
4125 static const uint64_t mask[6] = {
4126 0x5555555555555555ULL, 0x3333333333333333ULL,
4127 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4128 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4129 };
4130 MVT::ValueType VT = Op.getValueType();
4131 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4132 unsigned len = getSizeInBits(VT);
4133 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4134 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4135 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4136 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4137 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4138 DAG.getNode(ISD::AND, VT,
4139 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4140 }
4141 return Op;
4142 }
4143 case ISD::CTLZ: {
4144 // for now, we do this:
4145 // x = x | (x >> 1);
4146 // x = x | (x >> 2);
4147 // ...
4148 // x = x | (x >>16);
4149 // x = x | (x >>32); // for 64-bit input
4150 // return popcount(~x);
4151 //
4152 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4153 MVT::ValueType VT = Op.getValueType();
4154 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4155 unsigned len = getSizeInBits(VT);
4156 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4157 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4158 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4159 }
4160 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4161 return DAG.getNode(ISD::CTPOP, VT, Op);
4162 }
4163 case ISD::CTTZ: {
4164 // for now, we use: { return popcount(~x & (x - 1)); }
4165 // unless the target has ctlz but not ctpop, in which case we use:
4166 // { return 32 - nlz(~x & (x-1)); }
4167 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4168 MVT::ValueType VT = Op.getValueType();
4169 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4170 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4171 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4172 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4173 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4174 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4175 TLI.isOperationLegal(ISD::CTLZ, VT))
4176 return DAG.getNode(ISD::SUB, VT,
4177 DAG.getConstant(getSizeInBits(VT), VT),
4178 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4179 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4180 }
4181 }
4182}
Chris Lattnere34b3962005-01-19 04:19:40 +00004183
Chris Lattner3e928bb2005-01-07 07:47:09 +00004184/// ExpandOp - Expand the specified SDOperand into its two component pieces
4185/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4186/// LegalizeNodes map is filled in for any results that are not expanded, the
4187/// ExpandedNodes map is filled in for any results that are expanded, and the
4188/// Lo/Hi values are returned.
4189void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4190 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004191 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004192 SDNode *Node = Op.Val;
4193 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00004194 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4195 "Cannot expand FP values!");
4196 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004197 "Cannot expand to FP value or to larger int value!");
4198
Chris Lattner6fdcb252005-09-02 20:32:45 +00004199 // See if we already expanded it.
4200 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4201 = ExpandedNodes.find(Op);
4202 if (I != ExpandedNodes.end()) {
4203 Lo = I->second.first;
4204 Hi = I->second.second;
4205 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004206 }
4207
Chris Lattner3e928bb2005-01-07 07:47:09 +00004208 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004209 case ISD::CopyFromReg:
4210 assert(0 && "CopyFromReg must be legal!");
4211 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004212#ifndef NDEBUG
Chris Lattner3e928bb2005-01-07 07:47:09 +00004213 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004214#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004215 assert(0 && "Do not know how to expand this operator!");
4216 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004217 case ISD::UNDEF:
4218 Lo = DAG.getNode(ISD::UNDEF, NVT);
4219 Hi = DAG.getNode(ISD::UNDEF, NVT);
4220 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004221 case ISD::Constant: {
4222 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4223 Lo = DAG.getConstant(Cst, NVT);
4224 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4225 break;
4226 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004227 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004228 // Return the operands.
4229 Lo = Node->getOperand(0);
4230 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004231 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004232
4233 case ISD::SIGN_EXTEND_INREG:
4234 ExpandOp(Node->getOperand(0), Lo, Hi);
4235 // Sign extend the lo-part.
4236 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4237 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4238 TLI.getShiftAmountTy()));
4239 // sext_inreg the low part if needed.
4240 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4241 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004242
Nate Begemand88fc032006-01-14 03:14:10 +00004243 case ISD::BSWAP: {
4244 ExpandOp(Node->getOperand(0), Lo, Hi);
4245 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4246 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4247 Lo = TempLo;
4248 break;
4249 }
4250
Chris Lattneredb1add2005-05-11 04:51:16 +00004251 case ISD::CTPOP:
4252 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004253 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4254 DAG.getNode(ISD::CTPOP, NVT, Lo),
4255 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004256 Hi = DAG.getConstant(0, NVT);
4257 break;
4258
Chris Lattner39a8f332005-05-12 19:05:01 +00004259 case ISD::CTLZ: {
4260 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004261 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004262 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4263 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004264 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4265 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004266 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4267 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4268
4269 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4270 Hi = DAG.getConstant(0, NVT);
4271 break;
4272 }
4273
4274 case ISD::CTTZ: {
4275 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004276 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004277 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4278 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004279 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4280 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004281 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4282 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4283
4284 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4285 Hi = DAG.getConstant(0, NVT);
4286 break;
4287 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004288
Nate Begemanacc398c2006-01-25 18:21:52 +00004289 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004290 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4291 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004292 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4293 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4294
4295 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004296 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004297 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4298 if (!TLI.isLittleEndian())
4299 std::swap(Lo, Hi);
4300 break;
4301 }
4302
Chris Lattner3e928bb2005-01-07 07:47:09 +00004303 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004304 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4305 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004306 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004307
4308 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00004309 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004310 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4311 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00004312 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004313 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00004314
4315 // Build a factor node to remember that this load is independent of the
4316 // other one.
4317 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4318 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004319
Chris Lattner3e928bb2005-01-07 07:47:09 +00004320 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004321 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004322 if (!TLI.isLittleEndian())
4323 std::swap(Lo, Hi);
4324 break;
4325 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004326 case ISD::AND:
4327 case ISD::OR:
4328 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4329 SDOperand LL, LH, RL, RH;
4330 ExpandOp(Node->getOperand(0), LL, LH);
4331 ExpandOp(Node->getOperand(1), RL, RH);
4332 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4333 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4334 break;
4335 }
4336 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004337 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004338 ExpandOp(Node->getOperand(1), LL, LH);
4339 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00004340 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4341 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004342 break;
4343 }
Nate Begeman9373a812005-08-10 20:51:12 +00004344 case ISD::SELECT_CC: {
4345 SDOperand TL, TH, FL, FH;
4346 ExpandOp(Node->getOperand(2), TL, TH);
4347 ExpandOp(Node->getOperand(3), FL, FH);
4348 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4349 Node->getOperand(1), TL, FL, Node->getOperand(4));
4350 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4351 Node->getOperand(1), TH, FH, Node->getOperand(4));
4352 break;
4353 }
Nate Begeman144ff662005-10-13 17:15:37 +00004354 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004355 SDOperand Chain = Node->getOperand(0);
4356 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004357 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4358
4359 if (EVT == NVT)
4360 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4361 else
4362 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4363 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004364
4365 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004366 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004367
Nate Begeman144ff662005-10-13 17:15:37 +00004368 // The high part is obtained by SRA'ing all but one of the bits of the lo
4369 // part.
4370 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4371 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4372 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00004373 break;
4374 }
4375 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004376 SDOperand Chain = Node->getOperand(0);
4377 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004378 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4379
4380 if (EVT == NVT)
4381 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4382 else
4383 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4384 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004385
4386 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004387 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004388
Nate Begeman144ff662005-10-13 17:15:37 +00004389 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004390 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004391 break;
4392 }
4393 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004394 SDOperand Chain = Node->getOperand(0);
4395 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00004396 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4397
4398 if (EVT == NVT)
4399 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4400 else
4401 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4402 EVT);
4403
4404 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004405 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004406
4407 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004408 Hi = DAG.getNode(ISD::UNDEF, NVT);
4409 break;
4410 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004411 case ISD::ANY_EXTEND:
4412 // The low part is any extension of the input (which degenerates to a copy).
4413 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4414 // The high part is undefined.
4415 Hi = DAG.getNode(ISD::UNDEF, NVT);
4416 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004417 case ISD::SIGN_EXTEND: {
4418 // The low part is just a sign extension of the input (which degenerates to
4419 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004420 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004421
Chris Lattner3e928bb2005-01-07 07:47:09 +00004422 // The high part is obtained by SRA'ing all but one of the bits of the lo
4423 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004424 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004425 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4426 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004427 break;
4428 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004429 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004430 // The low part is just a zero extension of the input (which degenerates to
4431 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004432 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004433
Chris Lattner3e928bb2005-01-07 07:47:09 +00004434 // The high part is just a zero.
4435 Hi = DAG.getConstant(0, NVT);
4436 break;
Chris Lattner35481892005-12-23 00:16:34 +00004437
4438 case ISD::BIT_CONVERT: {
4439 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4440 Node->getOperand(0));
4441 ExpandOp(Tmp, Lo, Hi);
4442 break;
4443 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004444
Chris Lattner8137c9e2006-01-28 05:07:51 +00004445 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004446 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4447 TargetLowering::Custom &&
4448 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004449 Lo = TLI.LowerOperation(Op, DAG);
4450 assert(Lo.Val && "Node must be custom expanded!");
4451 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004452 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004453 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004454 break;
4455
Chris Lattner4e6c7462005-01-08 19:27:05 +00004456 // These operators cannot be expanded directly, emit them as calls to
4457 // library functions.
4458 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004459 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004460 SDOperand Op;
4461 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4462 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004463 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4464 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004465 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004466
Chris Lattnerf20d1832005-07-30 01:40:57 +00004467 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4468
Chris Lattner80a3e942005-07-29 00:33:32 +00004469 // Now that the custom expander is done, expand the result, which is still
4470 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004471 if (Op.Val) {
4472 ExpandOp(Op, Lo, Hi);
4473 break;
4474 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004475 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004476
Chris Lattner4e6c7462005-01-08 19:27:05 +00004477 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004478 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004479 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004480 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004481 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004482
Chris Lattner4e6c7462005-01-08 19:27:05 +00004483 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004484 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004485 SDOperand Op;
4486 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4487 case Expand: assert(0 && "cannot expand FP!");
4488 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4489 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4490 }
4491
4492 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4493
4494 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004495 if (Op.Val) {
4496 ExpandOp(Op, Lo, Hi);
4497 break;
4498 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004499 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004500
Chris Lattner4e6c7462005-01-08 19:27:05 +00004501 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004502 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004503 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004504 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004505 break;
4506
Evan Cheng05a2d562006-01-09 18:31:59 +00004507 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004508 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004509 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004510 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004511 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004512 Op = TLI.LowerOperation(Op, DAG);
4513 if (Op.Val) {
4514 // Now that the custom expander is done, expand the result, which is
4515 // still VT.
4516 ExpandOp(Op, Lo, Hi);
4517 break;
4518 }
4519 }
4520
Chris Lattnere34b3962005-01-19 04:19:40 +00004521 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004522 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004523 break;
Chris Lattner47599822005-04-02 03:38:53 +00004524
4525 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004526 TargetLowering::LegalizeAction Action =
4527 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4528 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4529 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004530 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004531 break;
4532 }
4533
Chris Lattnere34b3962005-01-19 04:19:40 +00004534 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004535 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004536 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004537 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004538
Evan Cheng05a2d562006-01-09 18:31:59 +00004539 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004540 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004541 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004542 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004543 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004544 Op = TLI.LowerOperation(Op, DAG);
4545 if (Op.Val) {
4546 // Now that the custom expander is done, expand the result, which is
4547 // still VT.
4548 ExpandOp(Op, Lo, Hi);
4549 break;
4550 }
4551 }
4552
Chris Lattnere34b3962005-01-19 04:19:40 +00004553 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004554 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004555 break;
Chris Lattner47599822005-04-02 03:38:53 +00004556
4557 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004558 TargetLowering::LegalizeAction Action =
4559 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4560 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4561 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004562 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004563 break;
4564 }
4565
Chris Lattnere34b3962005-01-19 04:19:40 +00004566 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004567 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004568 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004569 }
4570
4571 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004572 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004573 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004574 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004575 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004576 Op = TLI.LowerOperation(Op, DAG);
4577 if (Op.Val) {
4578 // Now that the custom expander is done, expand the result, which is
4579 // still VT.
4580 ExpandOp(Op, Lo, Hi);
4581 break;
4582 }
4583 }
4584
Chris Lattnere34b3962005-01-19 04:19:40 +00004585 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004586 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004587 break;
Chris Lattner47599822005-04-02 03:38:53 +00004588
4589 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004590 TargetLowering::LegalizeAction Action =
4591 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4592 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4593 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004594 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004595 break;
4596 }
4597
Chris Lattnere34b3962005-01-19 04:19:40 +00004598 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004599 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004600 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004601 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004602
Misha Brukmanedf128a2005-04-21 22:36:52 +00004603 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004604 case ISD::SUB: {
4605 // If the target wants to custom expand this, let them.
4606 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4607 TargetLowering::Custom) {
4608 Op = TLI.LowerOperation(Op, DAG);
4609 if (Op.Val) {
4610 ExpandOp(Op, Lo, Hi);
4611 break;
4612 }
4613 }
4614
4615 // Expand the subcomponents.
4616 SDOperand LHSL, LHSH, RHSL, RHSH;
4617 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4618 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004619 std::vector<MVT::ValueType> VTs;
4620 std::vector<SDOperand> LoOps, HiOps;
4621 VTs.push_back(LHSL.getValueType());
4622 VTs.push_back(MVT::Flag);
4623 LoOps.push_back(LHSL);
4624 LoOps.push_back(RHSL);
4625 HiOps.push_back(LHSH);
4626 HiOps.push_back(RHSH);
4627 if (Node->getOpcode() == ISD::ADD) {
4628 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4629 HiOps.push_back(Lo.getValue(1));
4630 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4631 } else {
4632 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4633 HiOps.push_back(Lo.getValue(1));
4634 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4635 }
Chris Lattner84f67882005-01-20 18:52:28 +00004636 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004637 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004638 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004639 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004640 SDOperand LL, LH, RL, RH;
4641 ExpandOp(Node->getOperand(0), LL, LH);
4642 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004643 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4644 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4645 // extended the sign bit of the low half through the upper half, and if so
4646 // emit a MULHS instead of the alternate sequence that is valid for any
4647 // i64 x i64 multiply.
4648 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4649 // is RH an extension of the sign bit of RL?
4650 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4651 RH.getOperand(1).getOpcode() == ISD::Constant &&
4652 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4653 // is LH an extension of the sign bit of LL?
4654 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4655 LH.getOperand(1).getOpcode() == ISD::Constant &&
4656 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4657 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4658 } else {
4659 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4660 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4661 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4662 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4663 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4664 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004665 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4666 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004667 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004668 }
4669 break;
4670 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004671 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4672 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4673 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4674 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004675 }
4676
Chris Lattner83397362005-12-21 18:02:52 +00004677 // Make sure the resultant values have been legalized themselves, unless this
4678 // is a type that requires multi-step expansion.
4679 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4680 Lo = LegalizeOp(Lo);
4681 Hi = LegalizeOp(Hi);
4682 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004683
4684 // Remember in a map if the values will be reused later.
4685 bool isNew =
4686 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4687 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004688}
4689
Chris Lattnerc7029802006-03-18 01:44:44 +00004690/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4691/// two smaller values of MVT::Vector type.
4692void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4693 SDOperand &Hi) {
4694 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4695 SDNode *Node = Op.Val;
4696 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4697 assert(NumElements > 1 && "Cannot split a single element vector!");
4698 unsigned NewNumElts = NumElements/2;
4699 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4700 SDOperand TypeNode = *(Node->op_end()-1);
4701
4702 // See if we already split it.
4703 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4704 = SplitNodes.find(Op);
4705 if (I != SplitNodes.end()) {
4706 Lo = I->second.first;
4707 Hi = I->second.second;
4708 return;
4709 }
4710
4711 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004712 default:
4713#ifndef NDEBUG
4714 Node->dump();
4715#endif
4716 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004717 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004718 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4719 LoOps.push_back(NewNumEltsNode);
4720 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004721 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004722
4723 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4724 HiOps.push_back(NewNumEltsNode);
4725 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004726 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004727 break;
4728 }
4729 case ISD::VADD:
4730 case ISD::VSUB:
4731 case ISD::VMUL:
4732 case ISD::VSDIV:
4733 case ISD::VUDIV:
4734 case ISD::VAND:
4735 case ISD::VOR:
4736 case ISD::VXOR: {
4737 SDOperand LL, LH, RL, RH;
4738 SplitVectorOp(Node->getOperand(0), LL, LH);
4739 SplitVectorOp(Node->getOperand(1), RL, RH);
4740
4741 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4742 NewNumEltsNode, TypeNode);
4743 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4744 NewNumEltsNode, TypeNode);
4745 break;
4746 }
4747 case ISD::VLOAD: {
4748 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4749 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4750 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4751
4752 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4753 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4754 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4755 getIntPtrConstant(IncrementSize));
4756 // FIXME: This creates a bogus srcvalue!
4757 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4758
4759 // Build a factor node to remember that this load is independent of the
4760 // other one.
4761 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4762 Hi.getValue(1));
4763
4764 // Remember that we legalized the chain.
4765 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00004766 break;
4767 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004768 case ISD::VBIT_CONVERT: {
4769 // We know the result is a vector. The input may be either a vector or a
4770 // scalar value.
4771 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4772 // Lower to a store/load. FIXME: this could be improved probably.
4773 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4774
4775 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4776 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4777 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4778 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4779 SplitVectorOp(St, Lo, Hi);
4780 } else {
4781 // If the input is a vector type, we have to either scalarize it, pack it
4782 // or convert it based on whether the input vector type is legal.
4783 SDNode *InVal = Node->getOperand(0).Val;
4784 unsigned NumElems =
4785 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4786 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4787
4788 // If the input is from a single element vector, scalarize the vector,
4789 // then treat like a scalar.
4790 if (NumElems == 1) {
4791 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4792 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4793 Op.getOperand(1), Op.getOperand(2));
4794 SplitVectorOp(Scalar, Lo, Hi);
4795 } else {
4796 // Split the input vector.
4797 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4798
4799 // Convert each of the pieces now.
4800 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4801 NewNumEltsNode, TypeNode);
4802 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4803 NewNumEltsNode, TypeNode);
4804 }
4805 break;
4806 }
4807 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004808 }
4809
4810 // Remember in a map if the values will be reused later.
4811 bool isNew =
4812 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4813 assert(isNew && "Value already expanded?!?");
4814}
4815
4816
4817/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4818/// equivalent operation that returns a scalar (e.g. F32) or packed value
4819/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4820/// type for the result.
4821SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4822 MVT::ValueType NewVT) {
4823 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4824 SDNode *Node = Op.Val;
4825
4826 // See if we already packed it.
4827 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4828 if (I != PackedNodes.end()) return I->second;
4829
4830 SDOperand Result;
4831 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004832 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004833#ifndef NDEBUG
Chris Lattner2332b9f2006-03-19 01:17:20 +00004834 Node->dump(); std::cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004835#endif
Chris Lattner2332b9f2006-03-19 01:17:20 +00004836 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004837 case ISD::VADD:
4838 case ISD::VSUB:
4839 case ISD::VMUL:
4840 case ISD::VSDIV:
4841 case ISD::VUDIV:
4842 case ISD::VAND:
4843 case ISD::VOR:
4844 case ISD::VXOR:
4845 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4846 NewVT,
4847 PackVectorOp(Node->getOperand(0), NewVT),
4848 PackVectorOp(Node->getOperand(1), NewVT));
4849 break;
4850 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004851 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4852 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004853
Chris Lattner4794a6b2006-03-19 00:07:49 +00004854 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004855
4856 // Remember that we legalized the chain.
4857 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4858 break;
4859 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004860 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00004861 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004862 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004863 Result = Node->getOperand(0);
4864 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004865 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00004866
4867 // If all elements of the build_vector are undefs, return an undef.
4868 bool AllUndef = true;
4869 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
4870 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
4871 AllUndef = false;
4872 break;
4873 }
4874 if (AllUndef) {
4875 Result = DAG.getNode(ISD::UNDEF, NewVT);
4876 } else {
4877 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4878 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
4879 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004880 }
4881 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004882 case ISD::VINSERT_VECTOR_ELT:
4883 if (!MVT::isVector(NewVT)) {
4884 // Returning a scalar? Must be the inserted element.
4885 Result = Node->getOperand(1);
4886 } else {
4887 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4888 PackVectorOp(Node->getOperand(0), NewVT),
4889 Node->getOperand(1), Node->getOperand(2));
4890 }
4891 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00004892 case ISD::VVECTOR_SHUFFLE:
4893 if (!MVT::isVector(NewVT)) {
4894 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4895 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4896 if (cast<ConstantSDNode>(EltNum)->getValue())
4897 Result = PackVectorOp(Node->getOperand(1), NewVT);
4898 else
4899 Result = PackVectorOp(Node->getOperand(0), NewVT);
4900 } else {
4901 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4902 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4903 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4904 Node->getOperand(2).Val->op_end()-2);
4905 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4906 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4907
4908 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4909 PackVectorOp(Node->getOperand(0), NewVT),
4910 PackVectorOp(Node->getOperand(1), NewVT), BV);
4911 }
4912 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004913 case ISD::VBIT_CONVERT:
4914 if (Op.getOperand(0).getValueType() != MVT::Vector)
4915 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4916 else {
4917 // If the input is a vector type, we have to either scalarize it, pack it
4918 // or convert it based on whether the input vector type is legal.
4919 SDNode *InVal = Node->getOperand(0).Val;
4920 unsigned NumElems =
4921 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4922 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4923
4924 // Figure out if there is a Packed type corresponding to this Vector
4925 // type. If so, convert to the packed type.
4926 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4927 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4928 // Turn this into a bit convert of the packed input.
4929 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4930 PackVectorOp(Node->getOperand(0), TVT));
4931 break;
4932 } else if (NumElems == 1) {
4933 // Turn this into a bit convert of the scalar input.
4934 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4935 PackVectorOp(Node->getOperand(0), EVT));
4936 break;
4937 } else {
4938 // FIXME: UNIMP!
4939 assert(0 && "Cast from unsupported vector type not implemented yet!");
4940 }
4941 }
Evan Chengdb3c6262006-04-10 18:54:36 +00004942 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004943 case ISD::VSELECT:
4944 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
4945 PackVectorOp(Op.getOperand(1), NewVT),
4946 PackVectorOp(Op.getOperand(2), NewVT));
4947 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004948 }
4949
4950 if (TLI.isTypeLegal(NewVT))
4951 Result = LegalizeOp(Result);
4952 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4953 assert(isNew && "Value already packed?");
4954 return Result;
4955}
4956
Chris Lattner3e928bb2005-01-07 07:47:09 +00004957
4958// SelectionDAG::Legalize - This is the entry point for the file.
4959//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004960void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00004961 if (ViewLegalizeDAGs) viewGraph();
4962
Chris Lattner3e928bb2005-01-07 07:47:09 +00004963 /// run - This is the main entry point to this class.
4964 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004965 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004966}
4967