blob: a5b3120afecd1b2fa88370938cab62835c3d7bd4 [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"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000024#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000027#include "llvm/ADT/SmallVector.h"
Evan Cheng033e6812006-03-24 01:17:21 +000028#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000029using namespace llvm;
30
Chris Lattner5e46a192006-04-02 03:07:27 +000031#ifndef NDEBUG
32static cl::opt<bool>
33ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
34 cl::desc("Pop up a window to show dags before legalize"));
35#else
36static const bool ViewLegalizeDAGs = 0;
37#endif
38
Chris Lattner3e928bb2005-01-07 07:47:09 +000039//===----------------------------------------------------------------------===//
40/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
41/// hacks on it until the target machine can handle it. This involves
42/// eliminating value sizes the machine cannot handle (promoting small sizes to
43/// large sizes or splitting up large values into small values) as well as
44/// eliminating operations the machine cannot handle.
45///
46/// This code also does a small amount of optimization and recognition of idioms
47/// as part of its processing. For example, if a target does not support a
48/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
49/// will attempt merge setcc and brc instructions into brcc's.
50///
51namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000052class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000053 TargetLowering &TLI;
54 SelectionDAG &DAG;
55
Chris Lattner6831a812006-02-13 09:18:02 +000056 // Libcall insertion helpers.
57
58 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
59 /// legalized. We use this to ensure that calls are properly serialized
60 /// against each other, including inserted libcalls.
61 SDOperand LastCALLSEQ_END;
62
63 /// IsLegalizingCall - This member is used *only* for purposes of providing
64 /// helpful assertions that a libcall isn't created while another call is
65 /// being legalized (which could lead to non-serialized call sequences).
66 bool IsLegalizingCall;
67
Chris Lattner3e928bb2005-01-07 07:47:09 +000068 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000069 Legal, // The target natively supports this operation.
70 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000071 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 };
Chris Lattner6831a812006-02-13 09:18:02 +000073
Chris Lattner3e928bb2005-01-07 07:47:09 +000074 /// ValueTypeActions - This is a bitvector that contains two bits for each
75 /// value type, where the two bits correspond to the LegalizeAction enum.
76 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000077 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000078
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 /// LegalizedNodes - For nodes that are of legal width, and that have more
80 /// than one use, this map indicates what regularized operand to use. This
81 /// allows us to avoid legalizing the same thing more than once.
82 std::map<SDOperand, SDOperand> LegalizedNodes;
83
Chris Lattner03c85462005-01-15 05:21:40 +000084 /// PromotedNodes - For nodes that are below legal width, and that have more
85 /// than one use, this map indicates what promoted value to use. This allows
86 /// us to avoid promoting the same thing more than once.
87 std::map<SDOperand, SDOperand> PromotedNodes;
88
Chris Lattnerc7029802006-03-18 01:44:44 +000089 /// ExpandedNodes - For nodes that need to be expanded this map indicates
90 /// which which operands are the expanded version of the input. This allows
91 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000092 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
93
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// SplitNodes - For vector nodes that need to be split, this map indicates
95 /// which which operands are the split version of the input. This allows us
96 /// to avoid splitting the same node more than once.
97 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
98
99 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
100 /// concrete packed types, this contains the mapping of ones we have already
101 /// processed to the result.
102 std::map<SDOperand, SDOperand> PackedNodes;
103
Chris Lattner8afc48e2005-01-07 22:28:47 +0000104 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000105 LegalizedNodes.insert(std::make_pair(From, To));
106 // If someone requests legalization of the new node, return itself.
107 if (From != To)
108 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000109 }
Chris Lattner03c85462005-01-15 05:21:40 +0000110 void AddPromotedOperand(SDOperand From, SDOperand To) {
111 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
112 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000113 // If someone requests legalization of the new node, return itself.
114 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000115 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000116
Chris Lattner3e928bb2005-01-07 07:47:09 +0000117public:
118
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000119 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000120
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121 /// getTypeAction - Return how we should legalize values of this type, either
122 /// it is already legal or we need to expand it into multiple registers of
123 /// smaller integer type, or we need to promote it to a larger type.
124 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000125 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000126 }
127
128 /// isTypeLegal - Return true if this type is legal on this target.
129 ///
130 bool isTypeLegal(MVT::ValueType VT) const {
131 return getTypeAction(VT) == Legal;
132 }
133
Chris Lattner3e928bb2005-01-07 07:47:09 +0000134 void LegalizeDAG();
135
Chris Lattner456a93a2006-01-28 07:39:30 +0000136private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000137 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
138 /// appropriate for its type.
139 void HandleOp(SDOperand Op);
140
141 /// LegalizeOp - We know that the specified value has a legal type.
142 /// Recursively ensure that the operands have legal types, then return the
143 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000144 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000145
146 /// PromoteOp - Given an operation that produces a value in an invalid type,
147 /// promote it to compute the value into a larger type. The produced value
148 /// will have the correct bits for the low portion of the register, but no
149 /// guarantee is made about the top bits: it may be zero, sign-extended, or
150 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000151 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000152
Chris Lattnerc7029802006-03-18 01:44:44 +0000153 /// ExpandOp - Expand the specified SDOperand into its two component pieces
154 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
155 /// the LegalizeNodes map is filled in for any results that are not expanded,
156 /// the ExpandedNodes map is filled in for any results that are expanded, and
157 /// the Lo/Hi values are returned. This applies to integer types and Vector
158 /// types.
159 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
160
161 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
162 /// two smaller values of MVT::Vector type.
163 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164
165 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
166 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
167 /// this is called, we know that PackedVT is the right type for the result and
168 /// we know that this type is legal for the target.
169 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
170
Chris Lattner4352cc92006-04-04 17:23:26 +0000171 /// isShuffleLegal - Return true if a vector shuffle is legal with the
172 /// specified mask and type. Targets can specify exactly which masks they
173 /// support and the code generator is tasked with not creating illegal masks.
174 ///
175 /// Note that this will also return true for shuffles that are promoted to a
176 /// different type.
177 ///
178 /// If this is a legal shuffle, this method returns the (possibly promoted)
179 /// build_vector Mask. If it's not a legal shuffle, it returns null.
180 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
181
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000182 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
183 std::set<SDNode*> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000184
Nate Begeman750ac1b2006-02-01 07:19:44 +0000185 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
186
Chris Lattnerce872152006-03-19 06:31:19 +0000187 SDOperand CreateStackTemporary(MVT::ValueType VT);
188
Reid Spencer47857812006-12-31 05:55:36 +0000189 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000190 SDOperand &Hi);
191 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
192 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000193
Chris Lattner35481892005-12-23 00:16:34 +0000194 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000195 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000196 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000197 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
198 SDOperand LegalOp,
199 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000200 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
201 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000202 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
203 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000204
Chris Lattner456a93a2006-01-28 07:39:30 +0000205 SDOperand ExpandBSWAP(SDOperand Op);
206 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000207 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
208 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000209 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
210 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000211
Chris Lattner15972212006-03-31 17:55:51 +0000212 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000213 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000214
Chris Lattner3e928bb2005-01-07 07:47:09 +0000215 SDOperand getIntPtrConstant(uint64_t Val) {
216 return DAG.getConstant(Val, TLI.getPointerTy());
217 }
218};
219}
220
Chris Lattner4352cc92006-04-04 17:23:26 +0000221/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
222/// specified mask and type. Targets can specify exactly which masks they
223/// support and the code generator is tasked with not creating illegal masks.
224///
225/// Note that this will also return true for shuffles that are promoted to a
226/// different type.
227SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
228 SDOperand Mask) const {
229 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
230 default: return 0;
231 case TargetLowering::Legal:
232 case TargetLowering::Custom:
233 break;
234 case TargetLowering::Promote: {
235 // If this is promoted to a different type, convert the shuffle mask and
236 // ask if it is legal in the promoted type!
237 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
238
239 // If we changed # elements, change the shuffle mask.
240 unsigned NumEltsGrowth =
241 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
242 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
243 if (NumEltsGrowth > 1) {
244 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000245 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000246 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
247 SDOperand InOp = Mask.getOperand(i);
248 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
249 if (InOp.getOpcode() == ISD::UNDEF)
250 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
251 else {
252 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
253 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
254 }
255 }
256 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000257 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000258 }
259 VT = NVT;
260 break;
261 }
262 }
263 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
264}
265
Chris Lattnerc7029802006-03-18 01:44:44 +0000266/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
267/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000268static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000269 switch (VecOp) {
270 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000271 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
272 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
273 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
274 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
275 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
276 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
277 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
278 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000279 }
280}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000281
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000282SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
283 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
284 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000285 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000286 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000287}
288
Chris Lattner32fca002005-10-06 01:20:27 +0000289/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
290/// not been visited yet and if all of its operands have already been visited.
291static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
292 std::map<SDNode*, unsigned> &Visited) {
293 if (++Visited[N] != N->getNumOperands())
294 return; // Haven't visited all operands yet
295
296 Order.push_back(N);
297
298 if (N->hasOneUse()) { // Tail recurse in common case.
299 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
300 return;
301 }
302
303 // Now that we have N in, add anything that uses it if all of their operands
304 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000305 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
306 ComputeTopDownOrdering(*UI, Order, Visited);
307}
308
Chris Lattner1618beb2005-07-29 00:11:56 +0000309
Chris Lattner3e928bb2005-01-07 07:47:09 +0000310void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000311 LastCALLSEQ_END = DAG.getEntryNode();
312 IsLegalizingCall = false;
313
Chris Lattnerab510a72005-10-02 17:49:46 +0000314 // The legalize process is inherently a bottom-up recursive process (users
315 // legalize their uses before themselves). Given infinite stack space, we
316 // could just start legalizing on the root and traverse the whole graph. In
317 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000318 // blocks. To avoid this problem, compute an ordering of the nodes where each
319 // node is only legalized after all of its operands are legalized.
320 std::map<SDNode*, unsigned> Visited;
321 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000322
Chris Lattner32fca002005-10-06 01:20:27 +0000323 // Compute ordering from all of the leaves in the graphs, those (like the
324 // entry node) that have no operands.
325 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
326 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000327 if (I->getNumOperands() == 0) {
328 Visited[I] = 0 - 1U;
329 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000330 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000331 }
332
Chris Lattnerde202b32005-11-09 23:47:37 +0000333 assert(Order.size() == Visited.size() &&
334 Order.size() ==
335 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000336 "Error: DAG is cyclic!");
337 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000338
Chris Lattnerc7029802006-03-18 01:44:44 +0000339 for (unsigned i = 0, e = Order.size(); i != e; ++i)
340 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000341
342 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000344 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
345 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346
347 ExpandedNodes.clear();
348 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000349 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000350 SplitNodes.clear();
351 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352
353 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000354 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355}
356
Chris Lattner6831a812006-02-13 09:18:02 +0000357
358/// FindCallEndFromCallStart - Given a chained node that is part of a call
359/// sequence, find the CALLSEQ_END node that terminates the call sequence.
360static SDNode *FindCallEndFromCallStart(SDNode *Node) {
361 if (Node->getOpcode() == ISD::CALLSEQ_END)
362 return Node;
363 if (Node->use_empty())
364 return 0; // No CallSeqEnd
365
366 // The chain is usually at the end.
367 SDOperand TheChain(Node, Node->getNumValues()-1);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Sometimes it's at the beginning.
370 TheChain = SDOperand(Node, 0);
371 if (TheChain.getValueType() != MVT::Other) {
372 // Otherwise, hunt for it.
373 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
374 if (Node->getValueType(i) == MVT::Other) {
375 TheChain = SDOperand(Node, i);
376 break;
377 }
378
379 // Otherwise, we walked into a node without a chain.
380 if (TheChain.getValueType() != MVT::Other)
381 return 0;
382 }
383 }
384
385 for (SDNode::use_iterator UI = Node->use_begin(),
386 E = Node->use_end(); UI != E; ++UI) {
387
388 // Make sure to only follow users of our token chain.
389 SDNode *User = *UI;
390 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
391 if (User->getOperand(i) == TheChain)
392 if (SDNode *Result = FindCallEndFromCallStart(User))
393 return Result;
394 }
395 return 0;
396}
397
398/// FindCallStartFromCallEnd - Given a chained node that is part of a call
399/// sequence, find the CALLSEQ_START node that initiates the call sequence.
400static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
401 assert(Node && "Didn't find callseq_start for a call??");
402 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
403
404 assert(Node->getOperand(0).getValueType() == MVT::Other &&
405 "Node doesn't have a token chain argument!");
406 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
407}
408
409/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
410/// see if any uses can reach Dest. If no dest operands can get to dest,
411/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000412///
413/// Keep track of the nodes we fine that actually do lead to Dest in
414/// NodesLeadingTo. This avoids retraversing them exponential number of times.
415///
416bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
417 std::set<SDNode*> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000418 if (N == Dest) return true; // N certainly leads to Dest :)
419
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000420 // If we've already processed this node and it does lead to Dest, there is no
421 // need to reprocess it.
422 if (NodesLeadingTo.count(N)) return true;
423
Chris Lattner6831a812006-02-13 09:18:02 +0000424 // If the first result of this node has been already legalized, then it cannot
425 // reach N.
426 switch (getTypeAction(N->getValueType(0))) {
427 case Legal:
428 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 case Promote:
431 if (PromotedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 case Expand:
434 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
435 break;
436 }
437
438 // Okay, this node has not already been legalized. Check and legalize all
439 // operands. If none lead to Dest, then we can legalize this node.
440 bool OperandsLeadToDest = false;
441 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
442 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000443 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000444
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000445 if (OperandsLeadToDest) {
446 NodesLeadingTo.insert(N);
447 return true;
448 }
Chris Lattner6831a812006-02-13 09:18:02 +0000449
450 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000451 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000452 return false;
453}
454
Chris Lattnerc7029802006-03-18 01:44:44 +0000455/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
456/// appropriate for its type.
457void SelectionDAGLegalize::HandleOp(SDOperand Op) {
458 switch (getTypeAction(Op.getValueType())) {
459 default: assert(0 && "Bad type action!");
460 case Legal: LegalizeOp(Op); break;
461 case Promote: PromoteOp(Op); break;
462 case Expand:
463 if (Op.getValueType() != MVT::Vector) {
464 SDOperand X, Y;
465 ExpandOp(Op, X, Y);
466 } else {
467 SDNode *N = Op.Val;
468 unsigned NumOps = N->getNumOperands();
469 unsigned NumElements =
470 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
471 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
472 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
473 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
474 // In the common case, this is a legal vector type, convert it to the
475 // packed operation and type now.
476 PackVectorOp(Op, PackedVT);
477 } else if (NumElements == 1) {
478 // Otherwise, if this is a single element vector, convert it to a
479 // scalar operation.
480 PackVectorOp(Op, EVT);
481 } else {
482 // Otherwise, this is a multiple element vector that isn't supported.
483 // Split it in half and legalize both parts.
484 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000485 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000486 }
487 }
488 break;
489 }
490}
Chris Lattner6831a812006-02-13 09:18:02 +0000491
Evan Cheng9f877882006-12-13 20:57:08 +0000492/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
493/// a load from the constant pool.
494static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000495 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000496 bool Extend = false;
497
498 // If a FP immediate is precise when represented as a float and if the
499 // target can do an extending load from float to double, we put it into
500 // the constant pool as a float, even if it's is statically typed as a
501 // double.
502 MVT::ValueType VT = CFP->getValueType(0);
503 bool isDouble = VT == MVT::f64;
504 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
505 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000506 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000507 double Val = LLVMC->getValue();
508 return isDouble
509 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
510 : DAG.getConstant(FloatToBits(Val), MVT::i32);
511 }
512
Evan Cheng00495212006-12-12 21:32:44 +0000513 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
514 // Only do this if the target has a native EXTLOAD instruction from f32.
515 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
516 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
517 VT = MVT::f32;
518 Extend = true;
519 }
520
521 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
522 if (Extend) {
523 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
524 CPIdx, NULL, 0, MVT::f32);
525 } else {
526 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
527 }
528}
529
Chris Lattner6831a812006-02-13 09:18:02 +0000530
Evan Cheng912095b2007-01-04 21:56:39 +0000531/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
532/// operations.
533static
534SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
535 SelectionDAG &DAG, TargetLowering &TLI) {
536 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
537 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
538 // First get the sign bit of second operand.
539 SDOperand Mask = (SrcVT == MVT::f64)
540 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
541 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
542 Mask = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask);
543 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
544 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask);
545 // Shift right or sign-extend it if the two operands have different types.
546 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
547 if (SizeDiff > 0) {
548 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
549 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
550 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
551 } else if (SizeDiff < 0)
552 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
553 // Or the first operand with the sign bit.
554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
555 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
556 return Result;
557}
558
559
Chris Lattnerc7029802006-03-18 01:44:44 +0000560/// LegalizeOp - We know that the specified value has a legal type.
561/// Recursively ensure that the operands have legal types, then return the
562/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000563SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000564 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000565 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000566 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000567
Chris Lattner3e928bb2005-01-07 07:47:09 +0000568 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000569 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000570 if (Node->getNumValues() > 1) {
571 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000572 if (getTypeAction(Node->getValueType(i)) != Legal) {
573 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000574 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000575 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000576 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000577 }
578 }
579
Chris Lattner45982da2005-05-12 16:53:42 +0000580 // Note that LegalizeOp may be reentered even from single-use nodes, which
581 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000582 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
583 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000584
Nate Begeman9373a812005-08-10 20:51:12 +0000585 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000586 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000587 bool isCustom = false;
588
Chris Lattner3e928bb2005-01-07 07:47:09 +0000589 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000590 case ISD::FrameIndex:
591 case ISD::EntryToken:
592 case ISD::Register:
593 case ISD::BasicBlock:
594 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000595 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000596 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000597 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000598 case ISD::TargetConstantPool:
599 case ISD::TargetGlobalAddress:
600 case ISD::TargetExternalSymbol:
601 case ISD::VALUETYPE:
602 case ISD::SRCVALUE:
603 case ISD::STRING:
604 case ISD::CONDCODE:
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +0000605 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner948c1b12006-01-28 08:31:04 +0000606 // Primitives must all be legal.
607 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
608 "This must be legal!");
609 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000610 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000611 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
612 // If this is a target node, legalize it by legalizing the operands then
613 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000614 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000615 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000616 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000617
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000618 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000619
620 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
621 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
622 return Result.getValue(Op.ResNo);
623 }
624 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000625#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +0000626 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000627#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000628 assert(0 && "Do not know how to legalize this operator!");
629 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000630 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000631 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000632 case ISD::ConstantPool:
633 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000634 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
635 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000636 case TargetLowering::Custom:
637 Tmp1 = TLI.LowerOperation(Op, DAG);
638 if (Tmp1.Val) Result = Tmp1;
639 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000640 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000641 break;
642 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000643 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000644 case ISD::AssertSext:
645 case ISD::AssertZext:
646 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000648 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000649 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000650 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000651 Result = Node->getOperand(Op.ResNo);
652 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000653 case ISD::CopyFromReg:
654 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000655 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000656 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000657 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000658 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000659 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000660 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000661 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000662 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
663 } else {
664 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
665 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000666 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
667 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000668 // Since CopyFromReg produces two values, make sure to remember that we
669 // legalized both of them.
670 AddLegalizedOperand(Op.getValue(0), Result);
671 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
672 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000673 case ISD::UNDEF: {
674 MVT::ValueType VT = Op.getValueType();
675 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000676 default: assert(0 && "This action is not supported yet!");
677 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000678 if (MVT::isInteger(VT))
679 Result = DAG.getConstant(0, VT);
680 else if (MVT::isFloatingPoint(VT))
681 Result = DAG.getConstantFP(0, VT);
682 else
683 assert(0 && "Unknown value type!");
684 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000685 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000686 break;
687 }
688 break;
689 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000690
Chris Lattner48b61a72006-03-28 00:40:33 +0000691 case ISD::INTRINSIC_W_CHAIN:
692 case ISD::INTRINSIC_WO_CHAIN:
693 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000694 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000695 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
696 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000697 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000698
699 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000700 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000701 TargetLowering::Custom) {
702 Tmp3 = TLI.LowerOperation(Result, DAG);
703 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000704 }
705
706 if (Result.Val->getNumValues() == 1) break;
707
708 // Must have return value and chain result.
709 assert(Result.Val->getNumValues() == 2 &&
710 "Cannot return more than two values!");
711
712 // Since loads produce two values, make sure to remember that we
713 // legalized both of them.
714 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
715 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
716 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000717 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000718
719 case ISD::LOCATION:
720 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
721 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
722
723 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
724 case TargetLowering::Promote:
725 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000726 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000727 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000728 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
729 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
730
731 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
732 const std::string &FName =
733 cast<StringSDNode>(Node->getOperand(3))->getValue();
734 const std::string &DirName =
735 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000736 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000737
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000738 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000739 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000740 SDOperand LineOp = Node->getOperand(1);
741 SDOperand ColOp = Node->getOperand(2);
742
743 if (useDEBUG_LOC) {
744 Ops.push_back(LineOp); // line #
745 Ops.push_back(ColOp); // col #
746 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000747 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000748 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000749 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
750 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000751 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
752 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000753 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000754 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000755 } else {
756 Result = Tmp1; // chain
757 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000758 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000759 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000760 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000761 if (Tmp1 != Node->getOperand(0) ||
762 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000763 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000764 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000765 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
766 Ops.push_back(Node->getOperand(1)); // line # must be legal.
767 Ops.push_back(Node->getOperand(2)); // col # must be legal.
768 } else {
769 // Otherwise promote them.
770 Ops.push_back(PromoteOp(Node->getOperand(1)));
771 Ops.push_back(PromoteOp(Node->getOperand(2)));
772 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000773 Ops.push_back(Node->getOperand(3)); // filename must be legal.
774 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000775 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000776 }
777 break;
778 }
779 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000780
781 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000782 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000783 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000784 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000785 case TargetLowering::Legal:
786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
787 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
788 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
789 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000790 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000791 break;
792 }
793 break;
794
795 case ISD::DEBUG_LABEL:
796 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
797 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000798 default: assert(0 && "This action is not supported yet!");
799 case TargetLowering::Legal:
800 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
801 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000802 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000803 break;
804 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000805 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000806
Chris Lattner3e928bb2005-01-07 07:47:09 +0000807 case ISD::Constant:
808 // We know we don't need to expand constants here, constants only have one
809 // value and we check that it is fine above.
810
811 // FIXME: Maybe we should handle things like targets that don't support full
812 // 32-bit immediates?
813 break;
814 case ISD::ConstantFP: {
815 // Spill FP immediates to the constant pool if the target cannot directly
816 // codegen them. Targets often have some immediate values that can be
817 // efficiently generated into an FP register without a load. We explicitly
818 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000819 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
820
821 // Check to see if this FP immediate is already legal.
822 bool isLegal = false;
823 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
824 E = TLI.legal_fpimm_end(); I != E; ++I)
825 if (CFP->isExactlyValue(*I)) {
826 isLegal = true;
827 break;
828 }
829
Chris Lattner3181a772006-01-29 06:26:56 +0000830 // If this is a legal constant, turn it into a TargetConstantFP node.
831 if (isLegal) {
832 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
833 break;
834 }
835
836 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
837 default: assert(0 && "This action is not supported yet!");
838 case TargetLowering::Custom:
839 Tmp3 = TLI.LowerOperation(Result, DAG);
840 if (Tmp3.Val) {
841 Result = Tmp3;
842 break;
843 }
844 // FALLTHROUGH
845 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000846 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000847 }
848 break;
849 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000850 case ISD::TokenFactor:
851 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000852 Tmp1 = LegalizeOp(Node->getOperand(0));
853 Tmp2 = LegalizeOp(Node->getOperand(1));
854 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
855 } else if (Node->getNumOperands() == 3) {
856 Tmp1 = LegalizeOp(Node->getOperand(0));
857 Tmp2 = LegalizeOp(Node->getOperand(1));
858 Tmp3 = LegalizeOp(Node->getOperand(2));
859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000860 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000861 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000862 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000863 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
864 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000865 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000866 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000867 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000868
869 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000870 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000871 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000872 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
873 assert(Tmp3.Val && "Target didn't custom lower this node!");
874 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
875 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000876
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000877 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000878 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000879 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
880 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000881 if (Op.ResNo == i)
882 Tmp2 = Tmp1;
883 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
884 }
885 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000886
Chris Lattnerb2827b02006-03-19 00:52:58 +0000887 case ISD::BUILD_VECTOR:
888 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000889 default: assert(0 && "This action is not supported yet!");
890 case TargetLowering::Custom:
891 Tmp3 = TLI.LowerOperation(Result, DAG);
892 if (Tmp3.Val) {
893 Result = Tmp3;
894 break;
895 }
896 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000897 case TargetLowering::Expand:
898 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000899 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000900 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000901 break;
902 case ISD::INSERT_VECTOR_ELT:
903 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
904 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
905 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
907
908 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
909 Node->getValueType(0))) {
910 default: assert(0 && "This action is not supported yet!");
911 case TargetLowering::Legal:
912 break;
913 case TargetLowering::Custom:
914 Tmp3 = TLI.LowerOperation(Result, DAG);
915 if (Tmp3.Val) {
916 Result = Tmp3;
917 break;
918 }
919 // FALLTHROUGH
920 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000921 // If the insert index is a constant, codegen this as a scalar_to_vector,
922 // then a shuffle that inserts it into the right position in the vector.
923 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
924 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
925 Tmp1.getValueType(), Tmp2);
926
927 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
928 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
929 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
930
931 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
932 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
933 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000934 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +0000935 for (unsigned i = 0; i != NumElts; ++i) {
936 if (i != InsertPos->getValue())
937 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
938 else
939 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
940 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000941 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
942 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +0000943
944 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
945 Tmp1, ScVec, ShufMask);
946 Result = LegalizeOp(Result);
947 break;
948 }
949
Chris Lattner2332b9f2006-03-19 01:17:20 +0000950 // If the target doesn't support this, we have to spill the input vector
951 // to a temporary stack slot, update the element, then reload it. This is
952 // badness. We could also load the value into a vector register (either
953 // with a "move to register" or "extload into register" instruction, then
954 // permute it into place, if the idx is a constant and if the idx is
955 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000956 MVT::ValueType VT = Tmp1.getValueType();
957 MVT::ValueType EltVT = Tmp2.getValueType();
958 MVT::ValueType IdxVT = Tmp3.getValueType();
959 MVT::ValueType PtrVT = TLI.getPointerTy();
960 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000961 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000962 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000963
964 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000965 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
966 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000967 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000968 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
969 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
970 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000971 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000972 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000973 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +0000974 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +0000975 break;
976 }
977 }
978 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000979 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000980 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
981 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
982 break;
983 }
984
Chris Lattnerce872152006-03-19 06:31:19 +0000985 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
986 Result = DAG.UpdateNodeOperands(Result, Tmp1);
987 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
988 Node->getValueType(0))) {
989 default: assert(0 && "This action is not supported yet!");
990 case TargetLowering::Legal:
991 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000992 case TargetLowering::Custom:
993 Tmp3 = TLI.LowerOperation(Result, DAG);
994 if (Tmp3.Val) {
995 Result = Tmp3;
996 break;
997 }
998 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000999 case TargetLowering::Expand:
1000 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001001 break;
1002 }
Chris Lattnerce872152006-03-19 06:31:19 +00001003 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001004 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1006 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1008
1009 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001010 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1011 default: assert(0 && "Unknown operation action!");
1012 case TargetLowering::Legal:
1013 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1014 "vector shuffle should not be created if not legal!");
1015 break;
1016 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001017 Tmp3 = TLI.LowerOperation(Result, DAG);
1018 if (Tmp3.Val) {
1019 Result = Tmp3;
1020 break;
1021 }
1022 // FALLTHROUGH
1023 case TargetLowering::Expand: {
1024 MVT::ValueType VT = Node->getValueType(0);
1025 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1026 MVT::ValueType PtrVT = TLI.getPointerTy();
1027 SDOperand Mask = Node->getOperand(2);
1028 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001029 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001030 for (unsigned i = 0; i != NumElems; ++i) {
1031 SDOperand Arg = Mask.getOperand(i);
1032 if (Arg.getOpcode() == ISD::UNDEF) {
1033 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1034 } else {
1035 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1036 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1037 if (Idx < NumElems)
1038 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1039 DAG.getConstant(Idx, PtrVT)));
1040 else
1041 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1042 DAG.getConstant(Idx - NumElems, PtrVT)));
1043 }
1044 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001045 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001046 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001047 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001048 case TargetLowering::Promote: {
1049 // Change base type to a different vector type.
1050 MVT::ValueType OVT = Node->getValueType(0);
1051 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1052
1053 // Cast the two input vectors.
1054 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1055 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1056
1057 // Convert the shuffle mask to the right # elements.
1058 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1059 assert(Tmp3.Val && "Shuffle not legal?");
1060 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1061 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1062 break;
1063 }
Chris Lattner87100e02006-03-20 01:52:29 +00001064 }
1065 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001066
1067 case ISD::EXTRACT_VECTOR_ELT:
1068 Tmp1 = LegalizeOp(Node->getOperand(0));
1069 Tmp2 = LegalizeOp(Node->getOperand(1));
1070 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001071
1072 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1073 Tmp1.getValueType())) {
1074 default: assert(0 && "This action is not supported yet!");
1075 case TargetLowering::Legal:
1076 break;
1077 case TargetLowering::Custom:
1078 Tmp3 = TLI.LowerOperation(Result, DAG);
1079 if (Tmp3.Val) {
1080 Result = Tmp3;
1081 break;
1082 }
1083 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001084 case TargetLowering::Expand:
1085 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001086 break;
1087 }
Chris Lattner384504c2006-03-21 20:44:12 +00001088 break;
1089
Chris Lattner15972212006-03-31 17:55:51 +00001090 case ISD::VEXTRACT_VECTOR_ELT:
1091 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001092 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001093
Chris Lattner6831a812006-02-13 09:18:02 +00001094 case ISD::CALLSEQ_START: {
1095 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1096
1097 // Recursively Legalize all of the inputs of the call end that do not lead
1098 // to this call start. This ensures that any libcalls that need be inserted
1099 // are inserted *before* the CALLSEQ_START.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001100 {std::set<SDNode*> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001101 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001102 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1103 NodesLeadingTo);
1104 }
Chris Lattner6831a812006-02-13 09:18:02 +00001105
1106 // Now that we legalized all of the inputs (which may have inserted
1107 // libcalls) create the new CALLSEQ_START node.
1108 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1109
1110 // Merge in the last call, to ensure that this call start after the last
1111 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001112 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001113 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1114 Tmp1 = LegalizeOp(Tmp1);
1115 }
Chris Lattner6831a812006-02-13 09:18:02 +00001116
1117 // Do not try to legalize the target-specific arguments (#1+).
1118 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001119 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001120 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001121 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001122 }
1123
1124 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001125 AddLegalizedOperand(Op.getValue(0), Result);
1126 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1127 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1128
Chris Lattner6831a812006-02-13 09:18:02 +00001129 // Now that the callseq_start and all of the non-call nodes above this call
1130 // sequence have been legalized, legalize the call itself. During this
1131 // process, no libcalls can/will be inserted, guaranteeing that no calls
1132 // can overlap.
1133 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1134 SDOperand InCallSEQ = LastCALLSEQ_END;
1135 // Note that we are selecting this call!
1136 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1137 IsLegalizingCall = true;
1138
1139 // Legalize the call, starting from the CALLSEQ_END.
1140 LegalizeOp(LastCALLSEQ_END);
1141 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1142 return Result;
1143 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001144 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001145 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1146 // will cause this node to be legalized as well as handling libcalls right.
1147 if (LastCALLSEQ_END.Val != Node) {
1148 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1149 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1150 assert(I != LegalizedNodes.end() &&
1151 "Legalizing the call start should have legalized this node!");
1152 return I->second;
1153 }
1154
1155 // Otherwise, the call start has been legalized and everything is going
1156 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001158 // Do not try to legalize the target-specific arguments (#1+), except for
1159 // an optional flag input.
1160 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1161 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001162 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001163 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001164 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001165 }
1166 } else {
1167 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1168 if (Tmp1 != Node->getOperand(0) ||
1169 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001170 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001171 Ops[0] = Tmp1;
1172 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001173 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001174 }
Chris Lattner6a542892006-01-24 05:48:21 +00001175 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001176 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001177 // This finishes up call legalization.
1178 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001179
1180 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1181 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1182 if (Node->getNumValues() == 2)
1183 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1184 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001185 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1187 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1188 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001190
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001191 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001192 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001193 switch (TLI.getOperationAction(Node->getOpcode(),
1194 Node->getValueType(0))) {
1195 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001196 case TargetLowering::Expand: {
1197 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1198 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1199 " not tell us which reg is the stack pointer!");
1200 SDOperand Chain = Tmp1.getOperand(0);
1201 SDOperand Size = Tmp2.getOperand(1);
1202 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1203 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1204 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001205 Tmp1 = LegalizeOp(Tmp1);
1206 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001207 break;
1208 }
1209 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001210 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1211 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001212 Tmp1 = LegalizeOp(Tmp3);
1213 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001214 }
Chris Lattner903d2782006-01-15 08:54:32 +00001215 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001216 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001217 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001218 }
Chris Lattner903d2782006-01-15 08:54:32 +00001219 // Since this op produce two values, make sure to remember that we
1220 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001221 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1222 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001223 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001224 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001225 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001226 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001227 bool Changed = false;
1228 // Legalize all of the operands of the inline asm, in case they are nodes
1229 // that need to be expanded or something. Note we skip the asm string and
1230 // all of the TargetConstant flags.
1231 SDOperand Op = LegalizeOp(Ops[0]);
1232 Changed = Op != Ops[0];
1233 Ops[0] = Op;
1234
1235 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1236 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1237 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1238 for (++i; NumVals; ++i, --NumVals) {
1239 SDOperand Op = LegalizeOp(Ops[i]);
1240 if (Op != Ops[i]) {
1241 Changed = true;
1242 Ops[i] = Op;
1243 }
1244 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001245 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001246
1247 if (HasInFlag) {
1248 Op = LegalizeOp(Ops.back());
1249 Changed |= Op != Ops.back();
1250 Ops.back() = Op;
1251 }
1252
1253 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001254 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001255
1256 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001257 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001258 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1259 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001260 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001261 case ISD::BR:
1262 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001263 // Ensure that libcalls are emitted before a branch.
1264 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1265 Tmp1 = LegalizeOp(Tmp1);
1266 LastCALLSEQ_END = DAG.getEntryNode();
1267
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001269 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001270 case ISD::BRIND:
1271 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1272 // Ensure that libcalls are emitted before a branch.
1273 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1274 Tmp1 = LegalizeOp(Tmp1);
1275 LastCALLSEQ_END = DAG.getEntryNode();
1276
1277 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1278 default: assert(0 && "Indirect target must be legal type (pointer)!");
1279 case Legal:
1280 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1281 break;
1282 }
1283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1284 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001285 case ISD::BR_JT:
1286 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1287 // Ensure that libcalls are emitted before a branch.
1288 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1289 Tmp1 = LegalizeOp(Tmp1);
1290 LastCALLSEQ_END = DAG.getEntryNode();
1291
1292 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1294
1295 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1296 default: assert(0 && "This action is not supported yet!");
1297 case TargetLowering::Legal: break;
1298 case TargetLowering::Custom:
1299 Tmp1 = TLI.LowerOperation(Result, DAG);
1300 if (Tmp1.Val) Result = Tmp1;
1301 break;
1302 case TargetLowering::Expand: {
1303 SDOperand Chain = Result.getOperand(0);
1304 SDOperand Table = Result.getOperand(1);
1305 SDOperand Index = Result.getOperand(2);
1306
1307 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001308 MachineFunction &MF = DAG.getMachineFunction();
1309 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001310 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1311 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001312
1313 SDOperand LD;
1314 switch (EntrySize) {
1315 default: assert(0 && "Size of jump table not supported yet."); break;
1316 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1317 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1318 }
1319
1320 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001321 // For PIC, the sequence is:
1322 // BRIND(load(Jumptable + index) + RelocBase)
1323 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1324 SDOperand Reloc;
1325 if (TLI.usesGlobalOffsetTable())
1326 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1327 else
1328 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001329 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001330 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1331 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1332 } else {
1333 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1334 }
1335 }
1336 }
1337 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001338 case ISD::BRCOND:
1339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001340 // Ensure that libcalls are emitted before a return.
1341 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1342 Tmp1 = LegalizeOp(Tmp1);
1343 LastCALLSEQ_END = DAG.getEntryNode();
1344
Chris Lattner47e92232005-01-18 19:27:06 +00001345 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1346 case Expand: assert(0 && "It's impossible to expand bools");
1347 case Legal:
1348 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1349 break;
1350 case Promote:
1351 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001352
1353 // The top bits of the promoted condition are not necessarily zero, ensure
1354 // that the value is properly zero extended.
1355 if (!TLI.MaskedValueIsZero(Tmp2,
1356 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1357 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001358 break;
1359 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001360
1361 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001363
1364 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1365 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001366 case TargetLowering::Legal: break;
1367 case TargetLowering::Custom:
1368 Tmp1 = TLI.LowerOperation(Result, DAG);
1369 if (Tmp1.Val) Result = Tmp1;
1370 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001371 case TargetLowering::Expand:
1372 // Expand brcond's setcc into its constituent parts and create a BR_CC
1373 // Node.
1374 if (Tmp2.getOpcode() == ISD::SETCC) {
1375 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1376 Tmp2.getOperand(0), Tmp2.getOperand(1),
1377 Node->getOperand(2));
1378 } else {
1379 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1380 DAG.getCondCode(ISD::SETNE), Tmp2,
1381 DAG.getConstant(0, Tmp2.getValueType()),
1382 Node->getOperand(2));
1383 }
1384 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001385 }
1386 break;
1387 case ISD::BR_CC:
1388 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001389 // Ensure that libcalls are emitted before a branch.
1390 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1391 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001392 Tmp2 = Node->getOperand(2); // LHS
1393 Tmp3 = Node->getOperand(3); // RHS
1394 Tmp4 = Node->getOperand(1); // CC
1395
1396 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001397 LastCALLSEQ_END = DAG.getEntryNode();
1398
Nate Begeman750ac1b2006-02-01 07:19:44 +00001399 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1400 // the LHS is a legal SETCC itself. In this case, we need to compare
1401 // the result against zero to select between true and false values.
1402 if (Tmp3.Val == 0) {
1403 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1404 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001405 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001406
1407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1408 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001409
Chris Lattner181b7a32005-12-17 23:46:46 +00001410 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1411 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001412 case TargetLowering::Legal: break;
1413 case TargetLowering::Custom:
1414 Tmp4 = TLI.LowerOperation(Result, DAG);
1415 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001416 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001417 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001418 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001419 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001420 LoadSDNode *LD = cast<LoadSDNode>(Node);
1421 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1422 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001423
Evan Cheng466685d2006-10-09 20:57:25 +00001424 ISD::LoadExtType ExtType = LD->getExtensionType();
1425 if (ExtType == ISD::NON_EXTLOAD) {
1426 MVT::ValueType VT = Node->getValueType(0);
1427 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1428 Tmp3 = Result.getValue(0);
1429 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001430
Evan Cheng466685d2006-10-09 20:57:25 +00001431 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1432 default: assert(0 && "This action is not supported yet!");
1433 case TargetLowering::Legal: break;
1434 case TargetLowering::Custom:
1435 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1436 if (Tmp1.Val) {
1437 Tmp3 = LegalizeOp(Tmp1);
1438 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001439 }
Evan Cheng466685d2006-10-09 20:57:25 +00001440 break;
1441 case TargetLowering::Promote: {
1442 // Only promote a load of vector type to another.
1443 assert(MVT::isVector(VT) && "Cannot promote this load!");
1444 // Change base type to a different vector type.
1445 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1446
1447 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1448 LD->getSrcValueOffset());
1449 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1450 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001451 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001452 }
Evan Cheng466685d2006-10-09 20:57:25 +00001453 }
1454 // Since loads produce two values, make sure to remember that we
1455 // legalized both of them.
1456 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1457 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1458 return Op.ResNo ? Tmp4 : Tmp3;
1459 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001460 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001461 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1462 default: assert(0 && "This action is not supported yet!");
1463 case TargetLowering::Promote:
1464 assert(SrcVT == MVT::i1 &&
1465 "Can only promote extending LOAD from i1 -> i8!");
1466 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1467 LD->getSrcValue(), LD->getSrcValueOffset(),
1468 MVT::i8);
1469 Tmp1 = Result.getValue(0);
1470 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001471 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001472 case TargetLowering::Custom:
1473 isCustom = true;
1474 // FALLTHROUGH
1475 case TargetLowering::Legal:
1476 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1477 Tmp1 = Result.getValue(0);
1478 Tmp2 = Result.getValue(1);
1479
1480 if (isCustom) {
1481 Tmp3 = TLI.LowerOperation(Result, DAG);
1482 if (Tmp3.Val) {
1483 Tmp1 = LegalizeOp(Tmp3);
1484 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1485 }
1486 }
1487 break;
1488 case TargetLowering::Expand:
1489 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1490 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1491 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1492 LD->getSrcValueOffset());
1493 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1494 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1495 Tmp2 = LegalizeOp(Load.getValue(1));
1496 break;
1497 }
1498 assert(ExtType != ISD::EXTLOAD && "EXTLOAD should always be supported!");
1499 // Turn the unsupported load into an EXTLOAD followed by an explicit
1500 // zero/sign extend inreg.
1501 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1502 Tmp1, Tmp2, LD->getSrcValue(),
1503 LD->getSrcValueOffset(), SrcVT);
1504 SDOperand ValRes;
1505 if (ExtType == ISD::SEXTLOAD)
1506 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1507 Result, DAG.getValueType(SrcVT));
1508 else
1509 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1510 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1511 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1512 break;
1513 }
1514 // Since loads produce two values, make sure to remember that we legalized
1515 // both of them.
1516 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1517 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1518 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001519 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001520 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001521 case ISD::EXTRACT_ELEMENT: {
1522 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1523 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001524 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001525 case Legal:
1526 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1527 // 1 -> Hi
1528 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1529 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1530 TLI.getShiftAmountTy()));
1531 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1532 } else {
1533 // 0 -> Lo
1534 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1535 Node->getOperand(0));
1536 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001537 break;
1538 case Expand:
1539 // Get both the low and high parts.
1540 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1541 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1542 Result = Tmp2; // 1 -> Hi
1543 else
1544 Result = Tmp1; // 0 -> Lo
1545 break;
1546 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001547 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001548 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001549
1550 case ISD::CopyToReg:
1551 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001552
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001553 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001554 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001555 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001556 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001557 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001558 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001559 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001560 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001561 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001562 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1564 Tmp3);
1565 } else {
1566 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001567 }
1568
1569 // Since this produces two values, make sure to remember that we legalized
1570 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001571 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001572 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001573 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001574 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001575 break;
1576
1577 case ISD::RET:
1578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001579
1580 // Ensure that libcalls are emitted before a return.
1581 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1582 Tmp1 = LegalizeOp(Tmp1);
1583 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001584
Chris Lattner3e928bb2005-01-07 07:47:09 +00001585 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001586 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001587 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001588 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001589 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001590 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001591 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001592 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001593 case Expand:
1594 if (Tmp2.getValueType() != MVT::Vector) {
1595 SDOperand Lo, Hi;
1596 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng13acce32006-12-11 19:27:14 +00001597 if (Hi.Val)
1598 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1599 else
1600 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001601 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001602 } else {
1603 SDNode *InVal = Tmp2.Val;
1604 unsigned NumElems =
1605 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1606 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1607
1608 // Figure out if there is a Packed type corresponding to this Vector
1609 // type. If so, convert to the packed type.
1610 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1611 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1612 // Turn this into a return of the packed type.
1613 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001614 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001615 } else if (NumElems == 1) {
1616 // Turn this into a return of the scalar type.
1617 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001618 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001619
1620 // FIXME: Returns of gcc generic vectors smaller than a legal type
1621 // should be returned in integer registers!
1622
Chris Lattnerf87324e2006-04-11 01:31:51 +00001623 // The scalarized value type may not be legal, e.g. it might require
1624 // promotion or expansion. Relegalize the return.
1625 Result = LegalizeOp(Result);
1626 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001627 // FIXME: Returns of gcc generic vectors larger than a legal vector
1628 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001629 SDOperand Lo, Hi;
1630 SplitVectorOp(Tmp2, Lo, Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001631 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001632 Result = LegalizeOp(Result);
1633 }
1634 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001635 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001636 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001637 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001639 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001640 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001641 }
1642 break;
1643 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001644 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001645 break;
1646 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001647 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001648 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001649 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001650 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1651 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001652 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001653 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001654 break;
1655 case Expand: {
1656 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001657 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1658 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001659 ExpandOp(Node->getOperand(i), Lo, Hi);
1660 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001661 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001662 if (Hi.Val) {
1663 NewValues.push_back(Hi);
1664 NewValues.push_back(Node->getOperand(i+1));
1665 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001666 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001667 }
1668 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001669 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001670 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001671
1672 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001673 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001674 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001675 Result = DAG.getNode(ISD::RET, MVT::Other,
1676 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001677 break;
1678 }
1679 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001680
Chris Lattner6862dbc2006-01-29 21:02:23 +00001681 if (Result.getOpcode() == ISD::RET) {
1682 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1683 default: assert(0 && "This action is not supported yet!");
1684 case TargetLowering::Legal: break;
1685 case TargetLowering::Custom:
1686 Tmp1 = TLI.LowerOperation(Result, DAG);
1687 if (Tmp1.Val) Result = Tmp1;
1688 break;
1689 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001690 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001691 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001692 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001693 StoreSDNode *ST = cast<StoreSDNode>(Node);
1694 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1695 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001696
Evan Cheng8b2794a2006-10-13 21:14:26 +00001697 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001698 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1699 // FIXME: We shouldn't do this for TargetConstantFP's.
1700 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1701 // to phase ordering between legalized code and the dag combiner. This
1702 // probably means that we need to integrate dag combiner and legalizer
1703 // together.
1704 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1705 if (CFP->getValueType(0) == MVT::f32) {
1706 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1707 } else {
1708 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1709 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1710 }
1711 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1712 ST->getSrcValueOffset());
1713 break;
1714 }
1715
Evan Cheng8b2794a2006-10-13 21:14:26 +00001716 switch (getTypeAction(ST->getStoredVT())) {
1717 case Legal: {
1718 Tmp3 = LegalizeOp(ST->getValue());
1719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1720 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001721
Evan Cheng8b2794a2006-10-13 21:14:26 +00001722 MVT::ValueType VT = Tmp3.getValueType();
1723 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1724 default: assert(0 && "This action is not supported yet!");
1725 case TargetLowering::Legal: break;
1726 case TargetLowering::Custom:
1727 Tmp1 = TLI.LowerOperation(Result, DAG);
1728 if (Tmp1.Val) Result = Tmp1;
1729 break;
1730 case TargetLowering::Promote:
1731 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1732 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1733 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1734 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1735 ST->getSrcValue(), ST->getSrcValueOffset());
1736 break;
1737 }
1738 break;
1739 }
1740 case Promote:
1741 // Truncate the value and store the result.
1742 Tmp3 = PromoteOp(ST->getValue());
1743 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1744 ST->getSrcValueOffset(), ST->getStoredVT());
1745 break;
1746
1747 case Expand:
1748 unsigned IncrementSize = 0;
1749 SDOperand Lo, Hi;
1750
1751 // If this is a vector type, then we have to calculate the increment as
1752 // the product of the element size in bytes, and the number of elements
1753 // in the high half of the vector.
1754 if (ST->getValue().getValueType() == MVT::Vector) {
1755 SDNode *InVal = ST->getValue().Val;
1756 unsigned NumElems =
1757 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1758 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1759
1760 // Figure out if there is a Packed type corresponding to this Vector
1761 // type. If so, convert to the packed type.
1762 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1763 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1764 // Turn this into a normal store of the packed type.
1765 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1766 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1767 ST->getSrcValueOffset());
1768 Result = LegalizeOp(Result);
1769 break;
1770 } else if (NumElems == 1) {
1771 // Turn this into a normal store of the scalar type.
1772 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1773 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1774 ST->getSrcValueOffset());
1775 // The scalarized value type may not be legal, e.g. it might require
1776 // promotion or expansion. Relegalize the scalar store.
1777 Result = LegalizeOp(Result);
1778 break;
1779 } else {
1780 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1781 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1782 }
1783 } else {
1784 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001785 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001786
1787 if (!TLI.isLittleEndian())
1788 std::swap(Lo, Hi);
1789 }
1790
1791 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1792 ST->getSrcValueOffset());
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001793
1794 if (Hi.Val == NULL) {
1795 // Must be int <-> float one-to-one expansion.
1796 Result = Lo;
1797 break;
1798 }
1799
Evan Cheng8b2794a2006-10-13 21:14:26 +00001800 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1801 getIntPtrConstant(IncrementSize));
1802 assert(isTypeLegal(Tmp2.getValueType()) &&
1803 "Pointers must be legal!");
1804 // FIXME: This sets the srcvalue of both halves to be the same, which is
1805 // wrong.
1806 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1807 ST->getSrcValueOffset());
1808 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1809 break;
1810 }
1811 } else {
1812 // Truncating store
1813 assert(isTypeLegal(ST->getValue().getValueType()) &&
1814 "Cannot handle illegal TRUNCSTORE yet!");
1815 Tmp3 = LegalizeOp(ST->getValue());
1816
1817 // The only promote case we handle is TRUNCSTORE:i1 X into
1818 // -> TRUNCSTORE:i8 (and X, 1)
1819 if (ST->getStoredVT() == MVT::i1 &&
1820 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1821 // Promote the bool to a mask then store.
1822 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1823 DAG.getConstant(1, Tmp3.getValueType()));
1824 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1825 ST->getSrcValueOffset(), MVT::i8);
1826 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1827 Tmp2 != ST->getBasePtr()) {
1828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1829 ST->getOffset());
1830 }
1831
1832 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1833 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001834 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001835 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001836 case TargetLowering::Custom:
1837 Tmp1 = TLI.LowerOperation(Result, DAG);
1838 if (Tmp1.Val) Result = Tmp1;
1839 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001840 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001841 }
1842 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001843 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001844 case ISD::PCMARKER:
1845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001846 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001847 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001848 case ISD::STACKSAVE:
1849 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001850 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1851 Tmp1 = Result.getValue(0);
1852 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001853
Chris Lattner140d53c2006-01-13 02:50:02 +00001854 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1855 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001856 case TargetLowering::Legal: break;
1857 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001858 Tmp3 = TLI.LowerOperation(Result, DAG);
1859 if (Tmp3.Val) {
1860 Tmp1 = LegalizeOp(Tmp3);
1861 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001862 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001863 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001864 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001865 // Expand to CopyFromReg if the target set
1866 // StackPointerRegisterToSaveRestore.
1867 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001868 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001869 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001870 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001871 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001872 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1873 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001874 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001875 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001876 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001877
1878 // Since stacksave produce two values, make sure to remember that we
1879 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001880 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1881 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1882 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001883
Chris Lattner140d53c2006-01-13 02:50:02 +00001884 case ISD::STACKRESTORE:
1885 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1886 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001887 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001888
1889 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1890 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001891 case TargetLowering::Legal: break;
1892 case TargetLowering::Custom:
1893 Tmp1 = TLI.LowerOperation(Result, DAG);
1894 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001895 break;
1896 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001897 // Expand to CopyToReg if the target set
1898 // StackPointerRegisterToSaveRestore.
1899 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1900 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1901 } else {
1902 Result = Tmp1;
1903 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001904 break;
1905 }
1906 break;
1907
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001908 case ISD::READCYCLECOUNTER:
1909 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001911 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1912 Node->getValueType(0))) {
1913 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001914 case TargetLowering::Legal:
1915 Tmp1 = Result.getValue(0);
1916 Tmp2 = Result.getValue(1);
1917 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001918 case TargetLowering::Custom:
1919 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001920 Tmp1 = LegalizeOp(Result.getValue(0));
1921 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001922 break;
1923 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001924
1925 // Since rdcc produce two values, make sure to remember that we legalized
1926 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001927 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1928 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001929 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001930
Chris Lattner2ee743f2005-01-14 22:08:15 +00001931 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001932 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1933 case Expand: assert(0 && "It's impossible to expand bools");
1934 case Legal:
1935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1936 break;
1937 case Promote:
1938 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00001939 // Make sure the condition is either zero or one.
1940 if (!TLI.MaskedValueIsZero(Tmp1,
1941 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1942 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001943 break;
1944 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001945 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001946 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001947
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001949
Nate Begemanb942a3d2005-08-23 04:29:48 +00001950 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001951 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001952 case TargetLowering::Legal: break;
1953 case TargetLowering::Custom: {
1954 Tmp1 = TLI.LowerOperation(Result, DAG);
1955 if (Tmp1.Val) Result = Tmp1;
1956 break;
1957 }
Nate Begeman9373a812005-08-10 20:51:12 +00001958 case TargetLowering::Expand:
1959 if (Tmp1.getOpcode() == ISD::SETCC) {
1960 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1961 Tmp2, Tmp3,
1962 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1963 } else {
1964 Result = DAG.getSelectCC(Tmp1,
1965 DAG.getConstant(0, Tmp1.getValueType()),
1966 Tmp2, Tmp3, ISD::SETNE);
1967 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001968 break;
1969 case TargetLowering::Promote: {
1970 MVT::ValueType NVT =
1971 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1972 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001973 if (MVT::isVector(Tmp2.getValueType())) {
1974 ExtOp = ISD::BIT_CONVERT;
1975 TruncOp = ISD::BIT_CONVERT;
1976 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001977 ExtOp = ISD::ANY_EXTEND;
1978 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001979 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001980 ExtOp = ISD::FP_EXTEND;
1981 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001982 }
1983 // Promote each of the values to the new type.
1984 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1985 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1986 // Perform the larger operation, then round down.
1987 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1988 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1989 break;
1990 }
1991 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001992 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001993 case ISD::SELECT_CC: {
1994 Tmp1 = Node->getOperand(0); // LHS
1995 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001996 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1997 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001998 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001999
Nate Begeman750ac1b2006-02-01 07:19:44 +00002000 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2001
2002 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2003 // the LHS is a legal SETCC itself. In this case, we need to compare
2004 // the result against zero to select between true and false values.
2005 if (Tmp2.Val == 0) {
2006 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2007 CC = DAG.getCondCode(ISD::SETNE);
2008 }
2009 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2010
2011 // Everything is legal, see if we should expand this op or something.
2012 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2013 default: assert(0 && "This action is not supported yet!");
2014 case TargetLowering::Legal: break;
2015 case TargetLowering::Custom:
2016 Tmp1 = TLI.LowerOperation(Result, DAG);
2017 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002018 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002019 }
2020 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002021 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002022 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002023 Tmp1 = Node->getOperand(0);
2024 Tmp2 = Node->getOperand(1);
2025 Tmp3 = Node->getOperand(2);
2026 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2027
2028 // If we had to Expand the SetCC operands into a SELECT node, then it may
2029 // not always be possible to return a true LHS & RHS. In this case, just
2030 // return the value we legalized, returned in the LHS
2031 if (Tmp2.Val == 0) {
2032 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002033 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002034 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002035
Chris Lattner73e142f2006-01-30 22:43:50 +00002036 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002037 default: assert(0 && "Cannot handle this action for SETCC yet!");
2038 case TargetLowering::Custom:
2039 isCustom = true;
2040 // FALLTHROUGH.
2041 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002043 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002044 Tmp4 = TLI.LowerOperation(Result, DAG);
2045 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002046 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002047 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002048 case TargetLowering::Promote: {
2049 // First step, figure out the appropriate operation to use.
2050 // Allow SETCC to not be supported for all legal data types
2051 // Mostly this targets FP
2052 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2053 MVT::ValueType OldVT = NewInTy;
2054
2055 // Scan for the appropriate larger type to use.
2056 while (1) {
2057 NewInTy = (MVT::ValueType)(NewInTy+1);
2058
2059 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2060 "Fell off of the edge of the integer world");
2061 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2062 "Fell off of the edge of the floating point world");
2063
2064 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002065 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002066 break;
2067 }
2068 if (MVT::isInteger(NewInTy))
2069 assert(0 && "Cannot promote Legal Integer SETCC yet");
2070 else {
2071 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2072 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2073 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002074 Tmp1 = LegalizeOp(Tmp1);
2075 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002077 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002078 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002079 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002080 case TargetLowering::Expand:
2081 // Expand a setcc node into a select_cc of the same condition, lhs, and
2082 // rhs that selects between const 1 (true) and const 0 (false).
2083 MVT::ValueType VT = Node->getValueType(0);
2084 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2085 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002086 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002087 break;
2088 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002089 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002090 case ISD::MEMSET:
2091 case ISD::MEMCPY:
2092 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002094 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2095
2096 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2097 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2098 case Expand: assert(0 && "Cannot expand a byte!");
2099 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002100 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002101 break;
2102 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002103 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002104 break;
2105 }
2106 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002107 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002108 }
Chris Lattner272455b2005-02-02 03:44:41 +00002109
2110 SDOperand Tmp4;
2111 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002112 case Expand: {
2113 // Length is too big, just take the lo-part of the length.
2114 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002115 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002116 break;
2117 }
Chris Lattnere5605212005-01-28 22:29:18 +00002118 case Legal:
2119 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002120 break;
2121 case Promote:
2122 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002123 break;
2124 }
2125
2126 SDOperand Tmp5;
2127 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2128 case Expand: assert(0 && "Cannot expand this yet!");
2129 case Legal:
2130 Tmp5 = LegalizeOp(Node->getOperand(4));
2131 break;
2132 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002133 Tmp5 = PromoteOp(Node->getOperand(4));
2134 break;
2135 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002136
2137 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2138 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002139 case TargetLowering::Custom:
2140 isCustom = true;
2141 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002142 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002144 if (isCustom) {
2145 Tmp1 = TLI.LowerOperation(Result, DAG);
2146 if (Tmp1.Val) Result = Tmp1;
2147 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002148 break;
2149 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002150 // Otherwise, the target does not support this operation. Lower the
2151 // operation to an explicit libcall as appropriate.
2152 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002153 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002154 TargetLowering::ArgListTy Args;
2155 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002156
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002157 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002158 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002159 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002160 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002161 // Extend the (previously legalized) ubyte argument to be an int value
2162 // for the call.
2163 if (Tmp3.getValueType() > MVT::i32)
2164 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2165 else
2166 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencer47857812006-12-31 05:55:36 +00002167 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
2168 Args.push_back(Entry);
2169 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2170 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002171
2172 FnName = "memset";
2173 } else if (Node->getOpcode() == ISD::MEMCPY ||
2174 Node->getOpcode() == ISD::MEMMOVE) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002175 Entry.Ty = IntPtrTy; Entry.isSigned = false;
2176 Entry.Node = Tmp2; Args.push_back(Entry);
2177 Entry.Node = Tmp3; Args.push_back(Entry);
2178 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002179 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2180 } else {
2181 assert(0 && "Unknown op!");
2182 }
Chris Lattner45982da2005-05-12 16:53:42 +00002183
Chris Lattnere1bd8222005-01-11 05:57:22 +00002184 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002185 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002186 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002187 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002188 break;
2189 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002190 }
2191 break;
2192 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002193
Chris Lattner5b359c62005-04-02 04:00:59 +00002194 case ISD::SHL_PARTS:
2195 case ISD::SRA_PARTS:
2196 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002197 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002198 bool Changed = false;
2199 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2200 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2201 Changed |= Ops.back() != Node->getOperand(i);
2202 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002203 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002204 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002205
Evan Cheng05a2d562006-01-09 18:31:59 +00002206 switch (TLI.getOperationAction(Node->getOpcode(),
2207 Node->getValueType(0))) {
2208 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002209 case TargetLowering::Legal: break;
2210 case TargetLowering::Custom:
2211 Tmp1 = TLI.LowerOperation(Result, DAG);
2212 if (Tmp1.Val) {
2213 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002214 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002215 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002216 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2217 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002218 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002219 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002220 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002221 return RetVal;
2222 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002223 break;
2224 }
2225
Chris Lattner2c8086f2005-04-02 05:00:07 +00002226 // Since these produce multiple values, make sure to remember that we
2227 // legalized all of them.
2228 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2229 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2230 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002231 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002232
2233 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002234 case ISD::ADD:
2235 case ISD::SUB:
2236 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002237 case ISD::MULHS:
2238 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002239 case ISD::UDIV:
2240 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002241 case ISD::AND:
2242 case ISD::OR:
2243 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002244 case ISD::SHL:
2245 case ISD::SRL:
2246 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002247 case ISD::FADD:
2248 case ISD::FSUB:
2249 case ISD::FMUL:
2250 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002251 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002252 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2253 case Expand: assert(0 && "Not possible");
2254 case Legal:
2255 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2256 break;
2257 case Promote:
2258 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2259 break;
2260 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002261
2262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002263
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002264 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002265 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002266 case TargetLowering::Legal: break;
2267 case TargetLowering::Custom:
2268 Tmp1 = TLI.LowerOperation(Result, DAG);
2269 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002270 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002271 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002272 if (Node->getValueType(0) == MVT::i32) {
2273 switch (Node->getOpcode()) {
2274 default: assert(0 && "Do not know how to expand this integer BinOp!");
2275 case ISD::UDIV:
2276 case ISD::SDIV:
2277 const char *FnName = Node->getOpcode() == ISD::UDIV
2278 ? "__udivsi3" : "__divsi3";
2279 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002280 bool isSigned = Node->getOpcode() == ISD::SDIV;
2281 Result = ExpandLibCall(FnName, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002282 };
2283 break;
2284 }
2285
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002286 assert(MVT::isVector(Node->getValueType(0)) &&
2287 "Cannot expand this binary operator!");
2288 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002289 SmallVector<SDOperand, 8> Ops;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002290 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2291 MVT::ValueType PtrVT = TLI.getPointerTy();
2292 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2293 i != e; ++i) {
2294 SDOperand Idx = DAG.getConstant(i, PtrVT);
2295 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2296 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2297 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2298 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002299 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2300 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002301 break;
2302 }
Evan Chengcc987612006-04-12 21:20:24 +00002303 case TargetLowering::Promote: {
2304 switch (Node->getOpcode()) {
2305 default: assert(0 && "Do not know how to promote this BinOp!");
2306 case ISD::AND:
2307 case ISD::OR:
2308 case ISD::XOR: {
2309 MVT::ValueType OVT = Node->getValueType(0);
2310 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2311 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2312 // Bit convert each of the values to the new type.
2313 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2314 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2315 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2316 // Bit convert the result back the original type.
2317 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2318 break;
2319 }
2320 }
2321 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002322 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002323 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002324
2325 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2326 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2327 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2328 case Expand: assert(0 && "Not possible");
2329 case Legal:
2330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2331 break;
2332 case Promote:
2333 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2334 break;
2335 }
2336
2337 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2338
2339 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2340 default: assert(0 && "Operation not supported");
2341 case TargetLowering::Custom:
2342 Tmp1 = TLI.LowerOperation(Result, DAG);
2343 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002344 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002345 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002346 case TargetLowering::Expand: {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002347 // If this target supports fabs/fneg natively, do this efficiently.
Evan Cheng912095b2007-01-04 21:56:39 +00002348 if (TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2349 TargetLowering::Legal &&
2350 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
2351 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002352 // Get the sign bit of the RHS.
2353 MVT::ValueType IVT =
2354 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2355 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2356 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2357 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2358 // Get the absolute value of the result.
2359 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2360 // Select between the nabs and abs value based on the sign bit of
2361 // the input.
2362 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2363 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2364 AbsVal),
2365 AbsVal);
2366 Result = LegalizeOp(Result);
2367 break;
2368 }
2369
2370 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002371 MVT::ValueType NVT =
2372 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2373 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2374 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2375 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002376 break;
2377 }
Evan Cheng912095b2007-01-04 21:56:39 +00002378 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002379 break;
2380
Nate Begeman551bf3f2006-02-17 05:43:56 +00002381 case ISD::ADDC:
2382 case ISD::SUBC:
2383 Tmp1 = LegalizeOp(Node->getOperand(0));
2384 Tmp2 = LegalizeOp(Node->getOperand(1));
2385 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2386 // Since this produces two values, make sure to remember that we legalized
2387 // both of them.
2388 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2389 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2390 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002391
Nate Begeman551bf3f2006-02-17 05:43:56 +00002392 case ISD::ADDE:
2393 case ISD::SUBE:
2394 Tmp1 = LegalizeOp(Node->getOperand(0));
2395 Tmp2 = LegalizeOp(Node->getOperand(1));
2396 Tmp3 = LegalizeOp(Node->getOperand(2));
2397 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2398 // Since this produces two values, make sure to remember that we legalized
2399 // both of them.
2400 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2401 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2402 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002403
Nate Begeman419f8b62005-10-18 00:27:41 +00002404 case ISD::BUILD_PAIR: {
2405 MVT::ValueType PairTy = Node->getValueType(0);
2406 // TODO: handle the case where the Lo and Hi operands are not of legal type
2407 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2408 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2409 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002410 case TargetLowering::Promote:
2411 case TargetLowering::Custom:
2412 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002413 case TargetLowering::Legal:
2414 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2415 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2416 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002417 case TargetLowering::Expand:
2418 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2419 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2420 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2421 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2422 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002423 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002424 break;
2425 }
2426 break;
2427 }
2428
Nate Begemanc105e192005-04-06 00:23:54 +00002429 case ISD::UREM:
2430 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002431 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002432 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2433 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002434
Nate Begemanc105e192005-04-06 00:23:54 +00002435 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002436 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2437 case TargetLowering::Custom:
2438 isCustom = true;
2439 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002440 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002441 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002442 if (isCustom) {
2443 Tmp1 = TLI.LowerOperation(Result, DAG);
2444 if (Tmp1.Val) Result = Tmp1;
2445 }
Nate Begemanc105e192005-04-06 00:23:54 +00002446 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002447 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002448 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002449 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002450 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002451 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2452 TargetLowering::Legal) {
2453 // X % Y -> X-X/Y*Y
2454 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002455 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002456 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2457 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2458 } else {
2459 assert(Node->getValueType(0) == MVT::i32 &&
2460 "Cannot expand this binary operator!");
2461 const char *FnName = Node->getOpcode() == ISD::UREM
2462 ? "__umodsi3" : "__modsi3";
2463 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002464 Result = ExpandLibCall(FnName, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002465 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002466 } else {
2467 // Floating point mod -> fmod libcall.
2468 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2469 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002470 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002471 }
2472 break;
2473 }
2474 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002475 case ISD::VAARG: {
2476 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2477 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2478
Chris Lattner5c62f332006-01-28 07:42:08 +00002479 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002480 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2481 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002482 case TargetLowering::Custom:
2483 isCustom = true;
2484 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002485 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002486 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2487 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002488 Tmp1 = Result.getValue(1);
2489
2490 if (isCustom) {
2491 Tmp2 = TLI.LowerOperation(Result, DAG);
2492 if (Tmp2.Val) {
2493 Result = LegalizeOp(Tmp2);
2494 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2495 }
2496 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002497 break;
2498 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002499 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002500 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002501 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002502 // Increment the pointer, VAList, to the next vaarg
2503 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2504 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2505 TLI.getPointerTy()));
2506 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002507 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2508 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002509 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002510 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002511 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002512 Result = LegalizeOp(Result);
2513 break;
2514 }
2515 }
2516 // Since VAARG produces two values, make sure to remember that we
2517 // legalized both of them.
2518 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002519 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2520 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002521 }
2522
2523 case ISD::VACOPY:
2524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2525 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2526 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2527
2528 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2529 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002530 case TargetLowering::Custom:
2531 isCustom = true;
2532 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002533 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002534 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2535 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002536 if (isCustom) {
2537 Tmp1 = TLI.LowerOperation(Result, DAG);
2538 if (Tmp1.Val) Result = Tmp1;
2539 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002540 break;
2541 case TargetLowering::Expand:
2542 // This defaults to loading a pointer from the input and storing it to the
2543 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002544 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002545 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002546 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2547 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002548 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2549 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002550 break;
2551 }
2552 break;
2553
2554 case ISD::VAEND:
2555 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2556 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2557
2558 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2559 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002560 case TargetLowering::Custom:
2561 isCustom = true;
2562 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002563 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002564 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002565 if (isCustom) {
2566 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2567 if (Tmp1.Val) Result = Tmp1;
2568 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002569 break;
2570 case TargetLowering::Expand:
2571 Result = Tmp1; // Default to a no-op, return the chain
2572 break;
2573 }
2574 break;
2575
2576 case ISD::VASTART:
2577 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2578 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2579
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2581
Nate Begemanacc398c2006-01-25 18:21:52 +00002582 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2583 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002584 case TargetLowering::Legal: break;
2585 case TargetLowering::Custom:
2586 Tmp1 = TLI.LowerOperation(Result, DAG);
2587 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002588 break;
2589 }
2590 break;
2591
Nate Begeman35ef9132006-01-11 21:21:00 +00002592 case ISD::ROTL:
2593 case ISD::ROTR:
2594 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2595 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002596
2597 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2598 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002599 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002600 break;
2601
Nate Begemand88fc032006-01-14 03:14:10 +00002602 case ISD::BSWAP:
2603 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2604 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002605 case TargetLowering::Custom:
2606 assert(0 && "Cannot custom legalize this yet!");
2607 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002608 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002609 break;
2610 case TargetLowering::Promote: {
2611 MVT::ValueType OVT = Tmp1.getValueType();
2612 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2613 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002614
Chris Lattner456a93a2006-01-28 07:39:30 +00002615 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2616 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2617 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2618 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2619 break;
2620 }
2621 case TargetLowering::Expand:
2622 Result = ExpandBSWAP(Tmp1);
2623 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002624 }
2625 break;
2626
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002627 case ISD::CTPOP:
2628 case ISD::CTTZ:
2629 case ISD::CTLZ:
2630 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2631 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002632 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002633 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002634 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002635 break;
2636 case TargetLowering::Promote: {
2637 MVT::ValueType OVT = Tmp1.getValueType();
2638 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002639
2640 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002641 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2642 // Perform the larger operation, then subtract if needed.
2643 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002644 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002645 case ISD::CTPOP:
2646 Result = Tmp1;
2647 break;
2648 case ISD::CTTZ:
2649 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002650 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2651 DAG.getConstant(getSizeInBits(NVT), NVT),
2652 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002653 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002654 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2655 break;
2656 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002657 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002658 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2659 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002660 getSizeInBits(OVT), NVT));
2661 break;
2662 }
2663 break;
2664 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002665 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002666 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002667 break;
2668 }
2669 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002670
Chris Lattner2c8086f2005-04-02 05:00:07 +00002671 // Unary operators
2672 case ISD::FABS:
2673 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002674 case ISD::FSQRT:
2675 case ISD::FSIN:
2676 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002677 Tmp1 = LegalizeOp(Node->getOperand(0));
2678 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002679 case TargetLowering::Promote:
2680 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002681 isCustom = true;
2682 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002683 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002684 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002685 if (isCustom) {
2686 Tmp1 = TLI.LowerOperation(Result, DAG);
2687 if (Tmp1.Val) Result = Tmp1;
2688 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002689 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002690 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002691 switch (Node->getOpcode()) {
2692 default: assert(0 && "Unreachable!");
2693 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002694 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2695 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002696 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002697 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002698 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002699 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2700 MVT::ValueType VT = Node->getValueType(0);
2701 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002702 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002703 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2704 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002705 break;
2706 }
2707 case ISD::FSQRT:
2708 case ISD::FSIN:
2709 case ISD::FCOS: {
2710 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002711 const char *FnName = 0;
2712 switch(Node->getOpcode()) {
2713 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2714 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2715 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2716 default: assert(0 && "Unreachable!");
2717 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002718 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002719 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002720 break;
2721 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002722 }
2723 break;
2724 }
2725 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002726 case ISD::FPOWI: {
2727 // We always lower FPOWI into a libcall. No target support it yet.
2728 const char *FnName = Node->getValueType(0) == MVT::f32
2729 ? "__powisf2" : "__powidf2";
2730 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002731 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002732 break;
2733 }
Chris Lattner35481892005-12-23 00:16:34 +00002734 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002735 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002736 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002737 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002738 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2739 Node->getOperand(0).getValueType())) {
2740 default: assert(0 && "Unknown operation action!");
2741 case TargetLowering::Expand:
2742 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2743 break;
2744 case TargetLowering::Legal:
2745 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002746 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002747 break;
2748 }
2749 }
2750 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002751 case ISD::VBIT_CONVERT: {
2752 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2753 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2754
2755 // The input has to be a vector type, we have to either scalarize it, pack
2756 // it, or convert it based on whether the input vector type is legal.
2757 SDNode *InVal = Node->getOperand(0).Val;
2758 unsigned NumElems =
2759 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2760 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2761
2762 // Figure out if there is a Packed type corresponding to this Vector
2763 // type. If so, convert to the packed type.
2764 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2765 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2766 // Turn this into a bit convert of the packed input.
2767 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2768 PackVectorOp(Node->getOperand(0), TVT));
2769 break;
2770 } else if (NumElems == 1) {
2771 // Turn this into a bit convert of the scalar input.
2772 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2773 PackVectorOp(Node->getOperand(0), EVT));
2774 break;
2775 } else {
2776 // FIXME: UNIMP! Store then reload
2777 assert(0 && "Cast from unsupported vector type not implemented yet!");
2778 }
2779 }
2780
Chris Lattner2c8086f2005-04-02 05:00:07 +00002781 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002782 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002783 case ISD::UINT_TO_FP: {
2784 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002785 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2786 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002787 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002788 Node->getOperand(0).getValueType())) {
2789 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002790 case TargetLowering::Custom:
2791 isCustom = true;
2792 // FALLTHROUGH
2793 case TargetLowering::Legal:
2794 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002795 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002796 if (isCustom) {
2797 Tmp1 = TLI.LowerOperation(Result, DAG);
2798 if (Tmp1.Val) Result = Tmp1;
2799 }
2800 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002801 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002802 Result = ExpandLegalINT_TO_FP(isSigned,
2803 LegalizeOp(Node->getOperand(0)),
2804 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002805 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002806 case TargetLowering::Promote:
2807 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2808 Node->getValueType(0),
2809 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002810 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002811 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002812 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002813 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002814 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2815 Node->getValueType(0), Node->getOperand(0));
2816 break;
2817 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002818 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002819 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002820 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2821 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002822 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002823 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2824 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002825 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002826 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2827 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002828 break;
2829 }
2830 break;
2831 }
2832 case ISD::TRUNCATE:
2833 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2834 case Legal:
2835 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002836 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002837 break;
2838 case Expand:
2839 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2840
2841 // Since the result is legal, we should just be able to truncate the low
2842 // part of the source.
2843 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2844 break;
2845 case Promote:
2846 Result = PromoteOp(Node->getOperand(0));
2847 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2848 break;
2849 }
2850 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002851
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002852 case ISD::FP_TO_SINT:
2853 case ISD::FP_TO_UINT:
2854 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2855 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002856 Tmp1 = LegalizeOp(Node->getOperand(0));
2857
Chris Lattner1618beb2005-07-29 00:11:56 +00002858 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2859 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002860 case TargetLowering::Custom:
2861 isCustom = true;
2862 // FALLTHROUGH
2863 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002864 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002865 if (isCustom) {
2866 Tmp1 = TLI.LowerOperation(Result, DAG);
2867 if (Tmp1.Val) Result = Tmp1;
2868 }
2869 break;
2870 case TargetLowering::Promote:
2871 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2872 Node->getOpcode() == ISD::FP_TO_SINT);
2873 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002874 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002875 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2876 SDOperand True, False;
2877 MVT::ValueType VT = Node->getOperand(0).getValueType();
2878 MVT::ValueType NVT = Node->getValueType(0);
2879 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2880 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2881 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2882 Node->getOperand(0), Tmp2, ISD::SETLT);
2883 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2884 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002885 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002886 Tmp2));
2887 False = DAG.getNode(ISD::XOR, NVT, False,
2888 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002889 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2890 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002891 } else {
2892 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2893 }
2894 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002895 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002896 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002897 case Expand: {
2898 // Convert f32 / f64 to i32 / i64.
2899 MVT::ValueType VT = Op.getValueType();
2900 const char *FnName = 0;
2901 switch (Node->getOpcode()) {
2902 case ISD::FP_TO_SINT:
2903 if (Node->getOperand(0).getValueType() == MVT::f32)
2904 FnName = (VT == MVT::i32) ? "__fixsfsi" : "__fixsfdi";
2905 else
2906 FnName = (VT == MVT::i32) ? "__fixdfsi" : "__fixdfdi";
2907 break;
2908 case ISD::FP_TO_UINT:
2909 if (Node->getOperand(0).getValueType() == MVT::f32)
2910 FnName = (VT == MVT::i32) ? "__fixunssfsi" : "__fixunssfdi";
2911 else
2912 FnName = (VT == MVT::i32) ? "__fixunsdfsi" : "__fixunsdfdi";
2913 break;
2914 default: assert(0 && "Unreachable!");
2915 }
2916 SDOperand Dummy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002917 Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00002918 break;
2919 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002920 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002921 Tmp1 = PromoteOp(Node->getOperand(0));
2922 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2923 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002924 break;
2925 }
2926 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002927
Chris Lattner13c78e22005-09-02 00:18:10 +00002928 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002929 case ISD::ZERO_EXTEND:
2930 case ISD::SIGN_EXTEND:
2931 case ISD::FP_EXTEND:
2932 case ISD::FP_ROUND:
2933 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002934 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002935 case Legal:
2936 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002937 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002938 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002939 case Promote:
2940 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002941 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002942 Tmp1 = PromoteOp(Node->getOperand(0));
2943 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002944 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002945 case ISD::ZERO_EXTEND:
2946 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002947 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002948 Result = DAG.getZeroExtendInReg(Result,
2949 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002950 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002951 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002952 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002953 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002954 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002955 Result,
2956 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002957 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002958 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002959 Result = PromoteOp(Node->getOperand(0));
2960 if (Result.getValueType() != Op.getValueType())
2961 // Dynamically dead while we have only 2 FP types.
2962 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2963 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002964 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002965 Result = PromoteOp(Node->getOperand(0));
2966 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2967 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002968 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002969 }
2970 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002971 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002972 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002973 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002974 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002975
2976 // If this operation is not supported, convert it to a shl/shr or load/store
2977 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002978 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2979 default: assert(0 && "This action not supported for this op yet!");
2980 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002981 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002982 break;
2983 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002984 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002985 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002986 // NOTE: we could fall back on load/store here too for targets without
2987 // SAR. However, it is doubtful that any exist.
2988 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2989 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002990 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002991 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2992 Node->getOperand(0), ShiftCst);
2993 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2994 Result, ShiftCst);
2995 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00002996 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00002997 // EXTLOAD pair, targetting a temporary location (a stack slot).
2998
2999 // NOTE: there is a choice here between constantly creating new stack
3000 // slots and always reusing the same one. We currently always create
3001 // new ones, as reuse may inhibit scheduling.
3002 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00003003 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
3004 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003005 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003006 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00003007 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3008 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003009 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3010 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003011 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003012 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003013 } else {
3014 assert(0 && "Unknown op");
3015 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003016 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003017 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003018 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003019 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003020 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003021
Chris Lattner4ddd2832006-04-08 04:13:17 +00003022 assert(Result.getValueType() == Op.getValueType() &&
3023 "Bad legalization!");
3024
Chris Lattner456a93a2006-01-28 07:39:30 +00003025 // Make sure that the generated code is itself legal.
3026 if (Result != Op)
3027 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003028
Chris Lattner45982da2005-05-12 16:53:42 +00003029 // Note that LegalizeOp may be reentered even from single-use nodes, which
3030 // means that we always must cache transformed nodes.
3031 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003032 return Result;
3033}
3034
Chris Lattner8b6fa222005-01-15 22:16:26 +00003035/// PromoteOp - Given an operation that produces a value in an invalid type,
3036/// promote it to compute the value into a larger type. The produced value will
3037/// have the correct bits for the low portion of the register, but no guarantee
3038/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003039SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3040 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003041 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003042 assert(getTypeAction(VT) == Promote &&
3043 "Caller should expand or legalize operands that are not promotable!");
3044 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3045 "Cannot promote to smaller type!");
3046
Chris Lattner03c85462005-01-15 05:21:40 +00003047 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003048 SDOperand Result;
3049 SDNode *Node = Op.Val;
3050
Chris Lattner6fdcb252005-09-02 20:32:45 +00003051 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3052 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003053
Chris Lattner03c85462005-01-15 05:21:40 +00003054 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003055 case ISD::CopyFromReg:
3056 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003057 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003058#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00003059 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003060#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003061 assert(0 && "Do not know how to promote this operator!");
3062 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003063 case ISD::UNDEF:
3064 Result = DAG.getNode(ISD::UNDEF, NVT);
3065 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003066 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003067 if (VT != MVT::i1)
3068 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3069 else
3070 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003071 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3072 break;
3073 case ISD::ConstantFP:
3074 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3075 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3076 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003077
Chris Lattner82fbfb62005-01-18 02:59:52 +00003078 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003079 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003080 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3081 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003082 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003083
Chris Lattner03c85462005-01-15 05:21:40 +00003084 case ISD::TRUNCATE:
3085 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3086 case Legal:
3087 Result = LegalizeOp(Node->getOperand(0));
3088 assert(Result.getValueType() >= NVT &&
3089 "This truncation doesn't make sense!");
3090 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3091 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3092 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003093 case Promote:
3094 // The truncation is not required, because we don't guarantee anything
3095 // about high bits anyway.
3096 Result = PromoteOp(Node->getOperand(0));
3097 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003098 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003099 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3100 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003101 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003102 }
3103 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003104 case ISD::SIGN_EXTEND:
3105 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003106 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003107 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3108 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3109 case Legal:
3110 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003111 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003112 break;
3113 case Promote:
3114 // Promote the reg if it's smaller.
3115 Result = PromoteOp(Node->getOperand(0));
3116 // The high bits are not guaranteed to be anything. Insert an extend.
3117 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003118 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003119 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003120 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003121 Result = DAG.getZeroExtendInReg(Result,
3122 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003123 break;
3124 }
3125 break;
Chris Lattner35481892005-12-23 00:16:34 +00003126 case ISD::BIT_CONVERT:
3127 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3128 Result = PromoteOp(Result);
3129 break;
3130
Chris Lattner8b6fa222005-01-15 22:16:26 +00003131 case ISD::FP_EXTEND:
3132 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3133 case ISD::FP_ROUND:
3134 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3135 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3136 case Promote: assert(0 && "Unreachable with 2 FP types!");
3137 case Legal:
3138 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003139 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003140 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003141 break;
3142 }
3143 break;
3144
3145 case ISD::SINT_TO_FP:
3146 case ISD::UINT_TO_FP:
3147 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3148 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003149 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003150 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003151 break;
3152
3153 case Promote:
3154 Result = PromoteOp(Node->getOperand(0));
3155 if (Node->getOpcode() == ISD::SINT_TO_FP)
3156 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003157 Result,
3158 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003159 else
Chris Lattner23993562005-04-13 02:38:47 +00003160 Result = DAG.getZeroExtendInReg(Result,
3161 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003162 // No extra round required here.
3163 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003164 break;
3165 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003166 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3167 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003168 // Round if we cannot tolerate excess precision.
3169 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003170 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3171 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003172 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003173 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003174 break;
3175
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003176 case ISD::SIGN_EXTEND_INREG:
3177 Result = PromoteOp(Node->getOperand(0));
3178 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3179 Node->getOperand(1));
3180 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003181 case ISD::FP_TO_SINT:
3182 case ISD::FP_TO_UINT:
3183 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3184 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003185 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003186 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003187 break;
3188 case Promote:
3189 // The input result is prerounded, so we don't have to do anything
3190 // special.
3191 Tmp1 = PromoteOp(Node->getOperand(0));
3192 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003193 }
Nate Begemand2558e32005-08-14 01:20:53 +00003194 // If we're promoting a UINT to a larger size, check to see if the new node
3195 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3196 // we can use that instead. This allows us to generate better code for
3197 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3198 // legal, such as PowerPC.
3199 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003200 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003201 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3202 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003203 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3204 } else {
3205 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3206 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003207 break;
3208
Chris Lattner2c8086f2005-04-02 05:00:07 +00003209 case ISD::FABS:
3210 case ISD::FNEG:
3211 Tmp1 = PromoteOp(Node->getOperand(0));
3212 assert(Tmp1.getValueType() == NVT);
3213 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3214 // NOTE: we do not have to do any extra rounding here for
3215 // NoExcessFPPrecision, because we know the input will have the appropriate
3216 // precision, and these operations don't modify precision at all.
3217 break;
3218
Chris Lattnerda6ba872005-04-28 21:44:33 +00003219 case ISD::FSQRT:
3220 case ISD::FSIN:
3221 case ISD::FCOS:
3222 Tmp1 = PromoteOp(Node->getOperand(0));
3223 assert(Tmp1.getValueType() == NVT);
3224 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003225 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003226 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3227 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003228 break;
3229
Chris Lattner03c85462005-01-15 05:21:40 +00003230 case ISD::AND:
3231 case ISD::OR:
3232 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003233 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003234 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003235 case ISD::MUL:
3236 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003237 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003238 // that too is okay if they are integer operations.
3239 Tmp1 = PromoteOp(Node->getOperand(0));
3240 Tmp2 = PromoteOp(Node->getOperand(1));
3241 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3242 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003243 break;
3244 case ISD::FADD:
3245 case ISD::FSUB:
3246 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003247 Tmp1 = PromoteOp(Node->getOperand(0));
3248 Tmp2 = PromoteOp(Node->getOperand(1));
3249 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3250 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3251
3252 // Floating point operations will give excess precision that we may not be
3253 // able to tolerate. If we DO allow excess precision, just leave it,
3254 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003255 // FIXME: Why would we need to round FP ops more than integer ones?
3256 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003257 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003258 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3259 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003260 break;
3261
Chris Lattner8b6fa222005-01-15 22:16:26 +00003262 case ISD::SDIV:
3263 case ISD::SREM:
3264 // These operators require that their input be sign extended.
3265 Tmp1 = PromoteOp(Node->getOperand(0));
3266 Tmp2 = PromoteOp(Node->getOperand(1));
3267 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003268 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3269 DAG.getValueType(VT));
3270 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3271 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003272 }
3273 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3274
3275 // Perform FP_ROUND: this is probably overly pessimistic.
3276 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003277 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3278 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003279 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003280 case ISD::FDIV:
3281 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003282 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003283 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003284 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3285 case Legal:
3286 Tmp1 = LegalizeOp(Node->getOperand(0));
3287 break;
3288 case Promote:
3289 Tmp1 = PromoteOp(Node->getOperand(0));
3290 break;
3291 case Expand:
3292 assert(0 && "not implemented");
3293 }
3294 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3295 case Legal:
3296 Tmp2 = LegalizeOp(Node->getOperand(1));
3297 break;
3298 case Promote:
3299 Tmp2 = PromoteOp(Node->getOperand(1));
3300 break;
3301 case Expand:
3302 assert(0 && "not implemented");
3303 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003304 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3305
3306 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003307 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003308 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3309 DAG.getValueType(VT));
3310 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003311
3312 case ISD::UDIV:
3313 case ISD::UREM:
3314 // These operators require that their input be zero extended.
3315 Tmp1 = PromoteOp(Node->getOperand(0));
3316 Tmp2 = PromoteOp(Node->getOperand(1));
3317 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003318 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3319 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003320 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3321 break;
3322
3323 case ISD::SHL:
3324 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003325 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003326 break;
3327 case ISD::SRA:
3328 // The input value must be properly sign extended.
3329 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003330 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3331 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003332 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003333 break;
3334 case ISD::SRL:
3335 // The input value must be properly zero extended.
3336 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003337 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003338 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003339 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003340
3341 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003342 Tmp1 = Node->getOperand(0); // Get the chain.
3343 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003344 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3345 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3346 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3347 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003348 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003349 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003350 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003351 // Increment the pointer, VAList, to the next vaarg
3352 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3353 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3354 TLI.getPointerTy()));
3355 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003356 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3357 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003358 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003359 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003360 }
3361 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003362 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003363 break;
3364
Evan Cheng466685d2006-10-09 20:57:25 +00003365 case ISD::LOAD: {
3366 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003367 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3368 ? ISD::EXTLOAD : LD->getExtensionType();
3369 Result = DAG.getExtLoad(ExtType, NVT,
3370 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003371 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003372 LD->getLoadedVT());
Chris Lattner03c85462005-01-15 05:21:40 +00003373 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003374 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003375 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003376 }
Chris Lattner03c85462005-01-15 05:21:40 +00003377 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003378 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3379 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003380 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003381 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003382 case ISD::SELECT_CC:
3383 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3384 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3385 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003386 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003387 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003388 case ISD::BSWAP:
3389 Tmp1 = Node->getOperand(0);
3390 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3391 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3392 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3393 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3394 TLI.getShiftAmountTy()));
3395 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003396 case ISD::CTPOP:
3397 case ISD::CTTZ:
3398 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003399 // Zero extend the argument
3400 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003401 // Perform the larger operation, then subtract if needed.
3402 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003403 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003404 case ISD::CTPOP:
3405 Result = Tmp1;
3406 break;
3407 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003408 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003409 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003410 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003411 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003412 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003413 break;
3414 case ISD::CTLZ:
3415 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003416 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3417 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003418 getSizeInBits(VT), NVT));
3419 break;
3420 }
3421 break;
Chris Lattner15972212006-03-31 17:55:51 +00003422 case ISD::VEXTRACT_VECTOR_ELT:
3423 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3424 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003425 case ISD::EXTRACT_VECTOR_ELT:
3426 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3427 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003428 }
3429
3430 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003431
3432 // Make sure the result is itself legal.
3433 Result = LegalizeOp(Result);
3434
3435 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003436 AddPromotedOperand(Op, Result);
3437 return Result;
3438}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003439
Chris Lattner15972212006-03-31 17:55:51 +00003440/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3441/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3442/// on the vector type. The return type of this matches the element type of the
3443/// vector, which may not be legal for the target.
3444SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3445 // We know that operand #0 is the Vec vector. If the index is a constant
3446 // or if the invec is a supported hardware type, we can use it. Otherwise,
3447 // lower to a store then an indexed load.
3448 SDOperand Vec = Op.getOperand(0);
3449 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3450
3451 SDNode *InVal = Vec.Val;
3452 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3453 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3454
3455 // Figure out if there is a Packed type corresponding to this Vector
3456 // type. If so, convert to the packed type.
3457 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3458 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3459 // Turn this into a packed extract_vector_elt operation.
3460 Vec = PackVectorOp(Vec, TVT);
3461 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3462 } else if (NumElems == 1) {
3463 // This must be an access of the only element. Return it.
3464 return PackVectorOp(Vec, EVT);
3465 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3466 SDOperand Lo, Hi;
3467 SplitVectorOp(Vec, Lo, Hi);
3468 if (CIdx->getValue() < NumElems/2) {
3469 Vec = Lo;
3470 } else {
3471 Vec = Hi;
3472 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3473 }
3474
3475 // It's now an extract from the appropriate high or low part. Recurse.
3476 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3477 return LowerVEXTRACT_VECTOR_ELT(Op);
3478 } else {
3479 // Variable index case for extract element.
3480 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3481 assert(0 && "unimp!");
3482 return SDOperand();
3483 }
3484}
3485
Chris Lattner4aab2f42006-04-02 05:06:04 +00003486/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3487/// memory traffic.
3488SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3489 SDOperand Vector = Op.getOperand(0);
3490 SDOperand Idx = Op.getOperand(1);
3491
3492 // If the target doesn't support this, store the value to a temporary
3493 // stack slot, then LOAD the scalar element back out.
3494 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003495 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003496
3497 // Add the offset to the index.
3498 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3499 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3500 DAG.getConstant(EltSize, Idx.getValueType()));
3501 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3502
Evan Cheng466685d2006-10-09 20:57:25 +00003503 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003504}
3505
Chris Lattner15972212006-03-31 17:55:51 +00003506
Nate Begeman750ac1b2006-02-01 07:19:44 +00003507/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3508/// with condition CC on the current target. This usually involves legalizing
3509/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3510/// there may be no choice but to create a new SetCC node to represent the
3511/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3512/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3513void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3514 SDOperand &RHS,
3515 SDOperand &CC) {
3516 SDOperand Tmp1, Tmp2, Result;
3517
3518 switch (getTypeAction(LHS.getValueType())) {
3519 case Legal:
3520 Tmp1 = LegalizeOp(LHS); // LHS
3521 Tmp2 = LegalizeOp(RHS); // RHS
3522 break;
3523 case Promote:
3524 Tmp1 = PromoteOp(LHS); // LHS
3525 Tmp2 = PromoteOp(RHS); // RHS
3526
3527 // If this is an FP compare, the operands have already been extended.
3528 if (MVT::isInteger(LHS.getValueType())) {
3529 MVT::ValueType VT = LHS.getValueType();
3530 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3531
3532 // Otherwise, we have to insert explicit sign or zero extends. Note
3533 // that we could insert sign extends for ALL conditions, but zero extend
3534 // is cheaper on many machines (an AND instead of two shifts), so prefer
3535 // it.
3536 switch (cast<CondCodeSDNode>(CC)->get()) {
3537 default: assert(0 && "Unknown integer comparison!");
3538 case ISD::SETEQ:
3539 case ISD::SETNE:
3540 case ISD::SETUGE:
3541 case ISD::SETUGT:
3542 case ISD::SETULE:
3543 case ISD::SETULT:
3544 // ALL of these operations will work if we either sign or zero extend
3545 // the operands (including the unsigned comparisons!). Zero extend is
3546 // usually a simpler/cheaper operation, so prefer it.
3547 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3548 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3549 break;
3550 case ISD::SETGE:
3551 case ISD::SETGT:
3552 case ISD::SETLT:
3553 case ISD::SETLE:
3554 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3555 DAG.getValueType(VT));
3556 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3557 DAG.getValueType(VT));
3558 break;
3559 }
3560 }
3561 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003562 case Expand: {
3563 MVT::ValueType VT = LHS.getValueType();
3564 if (VT == MVT::f32 || VT == MVT::f64) {
3565 // Expand into one or more soft-fp libcall(s).
3566 const char *FnName1 = NULL, *FnName2 = NULL;
Chris Lattner2d53a322006-12-15 07:36:19 +00003567 ISD::CondCode CC1, CC2 = ISD::SETCC_INVALID;
Evan Cheng2b49c502006-12-15 02:59:56 +00003568 switch (cast<CondCodeSDNode>(CC)->get()) {
3569 case ISD::SETEQ:
3570 case ISD::SETOEQ:
3571 FnName1 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2";
3572 CC1 = ISD::SETEQ;
3573 break;
3574 case ISD::SETNE:
3575 case ISD::SETUNE:
3576 FnName1 = (VT == MVT::f32) ? "__nesf2" : "__nedf2";
3577 CC1 = ISD::SETNE;
3578 break;
3579 case ISD::SETGE:
3580 case ISD::SETOGE:
3581 FnName1 = (VT == MVT::f32) ? "__gesf2" : "__gedf2";
3582 CC1 = ISD::SETGE;
3583 break;
3584 case ISD::SETLT:
3585 case ISD::SETOLT:
3586 FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3587 CC1 = ISD::SETLT;
3588 break;
3589 case ISD::SETLE:
3590 case ISD::SETOLE:
3591 FnName1 = (VT == MVT::f32) ? "__lesf2" : "__ledf2";
3592 CC1 = ISD::SETLE;
3593 break;
3594 case ISD::SETGT:
3595 case ISD::SETOGT:
3596 FnName1 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2";
3597 CC1 = ISD::SETGT;
3598 break;
3599 case ISD::SETUO:
3600 case ISD::SETO:
3601 FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2";
3602 CC1 = cast<CondCodeSDNode>(CC)->get() == ISD::SETO
3603 ? ISD::SETEQ : ISD::SETNE;
3604 break;
3605 default:
3606 FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2";
3607 CC1 = ISD::SETNE;
3608 switch (cast<CondCodeSDNode>(CC)->get()) {
3609 case ISD::SETONE:
3610 // SETONE = SETOLT | SETOGT
3611 FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3612 CC1 = ISD::SETLT;
3613 // Fallthrough
3614 case ISD::SETUGT:
3615 FnName2 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2";
3616 CC2 = ISD::SETGT;
3617 break;
3618 case ISD::SETUGE:
3619 FnName2 = (VT == MVT::f32) ? "__gesf2" : "__gedf2";
3620 CC2 = ISD::SETGE;
3621 break;
3622 case ISD::SETULT:
3623 FnName2 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2";
3624 CC2 = ISD::SETLT;
3625 break;
3626 case ISD::SETULE:
3627 FnName2 = (VT == MVT::f32) ? "__lesf2" : "__ledf2";
3628 CC2 = ISD::SETLE;
3629 break;
3630 case ISD::SETUEQ:
3631 FnName2 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2";
3632 CC2 = ISD::SETEQ;
3633 break;
3634 default: assert(0 && "Unsupported FP setcc!");
3635 }
3636 }
3637
3638 SDOperand Dummy;
3639 Tmp1 = ExpandLibCall(FnName1,
Reid Spencer47857812006-12-31 05:55:36 +00003640 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003641 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003642 Tmp2 = DAG.getConstant(0, MVT::i32);
3643 CC = DAG.getCondCode(CC1);
3644 if (FnName2) {
3645 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
3646 LHS = ExpandLibCall(FnName2,
Reid Spencer47857812006-12-31 05:55:36 +00003647 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003648 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003649 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
3650 DAG.getCondCode(CC2));
3651 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3652 Tmp2 = SDOperand();
3653 }
3654 LHS = Tmp1;
3655 RHS = Tmp2;
3656 return;
3657 }
3658
Nate Begeman750ac1b2006-02-01 07:19:44 +00003659 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3660 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003661 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003662 switch (cast<CondCodeSDNode>(CC)->get()) {
3663 case ISD::SETEQ:
3664 case ISD::SETNE:
3665 if (RHSLo == RHSHi)
3666 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3667 if (RHSCST->isAllOnesValue()) {
3668 // Comparison to -1.
3669 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3670 Tmp2 = RHSLo;
3671 break;
3672 }
3673
3674 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3675 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3676 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3677 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3678 break;
3679 default:
3680 // If this is a comparison of the sign bit, just look at the top part.
3681 // X > -1, x < 0
3682 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3683 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3684 CST->getValue() == 0) || // X < 0
3685 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3686 CST->isAllOnesValue())) { // X > -1
3687 Tmp1 = LHSHi;
3688 Tmp2 = RHSHi;
3689 break;
3690 }
3691
3692 // FIXME: This generated code sucks.
3693 ISD::CondCode LowCC;
3694 switch (cast<CondCodeSDNode>(CC)->get()) {
3695 default: assert(0 && "Unknown integer setcc!");
3696 case ISD::SETLT:
3697 case ISD::SETULT: LowCC = ISD::SETULT; break;
3698 case ISD::SETGT:
3699 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3700 case ISD::SETLE:
3701 case ISD::SETULE: LowCC = ISD::SETULE; break;
3702 case ISD::SETGE:
3703 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3704 }
3705
3706 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3707 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3708 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3709
3710 // NOTE: on targets without efficient SELECT of bools, we can always use
3711 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3712 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3713 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3714 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3715 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3716 Result, Tmp1, Tmp2));
3717 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003718 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003719 }
3720 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003721 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003722 LHS = Tmp1;
3723 RHS = Tmp2;
3724}
3725
Chris Lattner35481892005-12-23 00:16:34 +00003726/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003727/// The resultant code need not be legal. Note that SrcOp is the input operand
3728/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003729SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3730 SDOperand SrcOp) {
3731 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003732 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003733
3734 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003735 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003736 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003737 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003738}
3739
Chris Lattner4352cc92006-04-04 17:23:26 +00003740SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3741 // Create a vector sized/aligned stack slot, store the value to element #0,
3742 // then load the whole vector back out.
3743 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003744 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003745 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003746 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003747}
3748
3749
Chris Lattnerce872152006-03-19 06:31:19 +00003750/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3751/// support the operation, but do support the resultant packed vector type.
3752SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3753
3754 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003755 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003756 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003757 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003758 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003759 std::map<SDOperand, std::vector<unsigned> > Values;
3760 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003761 bool isConstant = true;
3762 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3763 SplatValue.getOpcode() != ISD::UNDEF)
3764 isConstant = false;
3765
Evan Cheng033e6812006-03-24 01:17:21 +00003766 for (unsigned i = 1; i < NumElems; ++i) {
3767 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003768 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003769 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003770 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003771 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003772 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003773
3774 // If this isn't a constant element or an undef, we can't use a constant
3775 // pool load.
3776 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3777 V.getOpcode() != ISD::UNDEF)
3778 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003779 }
3780
3781 if (isOnlyLowElement) {
3782 // If the low element is an undef too, then this whole things is an undef.
3783 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3784 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3785 // Otherwise, turn this into a scalar_to_vector node.
3786 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3787 Node->getOperand(0));
3788 }
3789
Chris Lattner2eb86532006-03-24 07:29:17 +00003790 // If all elements are constants, create a load from the constant pool.
3791 if (isConstant) {
3792 MVT::ValueType VT = Node->getValueType(0);
3793 const Type *OpNTy =
3794 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3795 std::vector<Constant*> CV;
3796 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3797 if (ConstantFPSDNode *V =
3798 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3799 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3800 } else if (ConstantSDNode *V =
3801 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003802 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003803 } else {
3804 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3805 CV.push_back(UndefValue::get(OpNTy));
3806 }
3807 }
3808 Constant *CP = ConstantPacked::get(CV);
3809 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003810 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003811 }
3812
Chris Lattner87100e02006-03-20 01:52:29 +00003813 if (SplatValue.Val) { // Splat of one value?
3814 // Build the shuffle constant vector: <0, 0, 0, 0>
3815 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003816 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003817 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003818 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003819 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3820 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00003821
3822 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003823 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003824 // Get the splatted value into the low element of a vector register.
3825 SDOperand LowValVec =
3826 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3827
3828 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3829 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3830 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3831 SplatMask);
3832 }
3833 }
3834
Evan Cheng033e6812006-03-24 01:17:21 +00003835 // If there are only two unique elements, we may be able to turn this into a
3836 // vector shuffle.
3837 if (Values.size() == 2) {
3838 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3839 MVT::ValueType MaskVT =
3840 MVT::getIntVectorWithNumElements(NumElems);
3841 std::vector<SDOperand> MaskVec(NumElems);
3842 unsigned i = 0;
3843 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3844 E = Values.end(); I != E; ++I) {
3845 for (std::vector<unsigned>::iterator II = I->second.begin(),
3846 EE = I->second.end(); II != EE; ++II)
3847 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3848 i += NumElems;
3849 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003850 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3851 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003852
3853 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003854 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3855 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003856 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00003857 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3858 E = Values.end(); I != E; ++I) {
3859 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3860 I->first);
3861 Ops.push_back(Op);
3862 }
3863 Ops.push_back(ShuffleMask);
3864
3865 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003866 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3867 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003868 }
3869 }
Chris Lattnerce872152006-03-19 06:31:19 +00003870
3871 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3872 // aligned object on the stack, store each element into it, then load
3873 // the result as a vector.
3874 MVT::ValueType VT = Node->getValueType(0);
3875 // Create the stack frame object.
3876 SDOperand FIPtr = CreateStackTemporary(VT);
3877
3878 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003879 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003880 unsigned TypeByteSize =
3881 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00003882 // Store (in the right endianness) the elements to memory.
3883 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3884 // Ignore undef elements.
3885 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3886
Chris Lattner841c8822006-03-22 01:46:54 +00003887 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003888
3889 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3890 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3891
Evan Cheng786225a2006-10-05 23:01:46 +00003892 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003893 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00003894 }
3895
3896 SDOperand StoreChain;
3897 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003898 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3899 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00003900 else
3901 StoreChain = DAG.getEntryNode();
3902
3903 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003904 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00003905}
3906
3907/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3908/// specified value type.
3909SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3910 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3911 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3912 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3913 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3914}
3915
Chris Lattner5b359c62005-04-02 04:00:59 +00003916void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3917 SDOperand Op, SDOperand Amt,
3918 SDOperand &Lo, SDOperand &Hi) {
3919 // Expand the subcomponents.
3920 SDOperand LHSL, LHSH;
3921 ExpandOp(Op, LHSL, LHSH);
3922
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003923 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003924 MVT::ValueType VT = LHSL.getValueType();
3925 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00003926 Hi = Lo.getValue(1);
3927}
3928
3929
Chris Lattnere34b3962005-01-19 04:19:40 +00003930/// ExpandShift - Try to find a clever way to expand this shift operation out to
3931/// smaller elements. If we can't find a way that is more efficient than a
3932/// libcall on this target, return false. Otherwise, return true with the
3933/// low-parts expanded into Lo and Hi.
3934bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3935 SDOperand &Lo, SDOperand &Hi) {
3936 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3937 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003938
Chris Lattnere34b3962005-01-19 04:19:40 +00003939 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003940 SDOperand ShAmt = LegalizeOp(Amt);
3941 MVT::ValueType ShTy = ShAmt.getValueType();
3942 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3943 unsigned NVTBits = MVT::getSizeInBits(NVT);
3944
3945 // Handle the case when Amt is an immediate. Other cases are currently broken
3946 // and are disabled.
3947 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3948 unsigned Cst = CN->getValue();
3949 // Expand the incoming operand to be shifted, so that we have its parts
3950 SDOperand InL, InH;
3951 ExpandOp(Op, InL, InH);
3952 switch(Opc) {
3953 case ISD::SHL:
3954 if (Cst > VTBits) {
3955 Lo = DAG.getConstant(0, NVT);
3956 Hi = DAG.getConstant(0, NVT);
3957 } else if (Cst > NVTBits) {
3958 Lo = DAG.getConstant(0, NVT);
3959 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003960 } else if (Cst == NVTBits) {
3961 Lo = DAG.getConstant(0, NVT);
3962 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003963 } else {
3964 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3965 Hi = DAG.getNode(ISD::OR, NVT,
3966 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3967 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3968 }
3969 return true;
3970 case ISD::SRL:
3971 if (Cst > VTBits) {
3972 Lo = DAG.getConstant(0, NVT);
3973 Hi = DAG.getConstant(0, NVT);
3974 } else if (Cst > NVTBits) {
3975 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3976 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003977 } else if (Cst == NVTBits) {
3978 Lo = InH;
3979 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003980 } else {
3981 Lo = DAG.getNode(ISD::OR, NVT,
3982 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3983 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3984 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3985 }
3986 return true;
3987 case ISD::SRA:
3988 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003989 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003990 DAG.getConstant(NVTBits-1, ShTy));
3991 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003992 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003993 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003994 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003995 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003996 } else if (Cst == NVTBits) {
3997 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003998 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003999 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004000 } else {
4001 Lo = DAG.getNode(ISD::OR, NVT,
4002 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4003 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4004 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4005 }
4006 return true;
4007 }
4008 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004009
4010 // Okay, the shift amount isn't constant. However, if we can tell that it is
4011 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4012 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4013 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4014
4015 // If we know that the high bit of the shift amount is one, then we can do
4016 // this as a couple of simple shifts.
4017 if (KnownOne & Mask) {
4018 // Mask out the high bit, which we know is set.
4019 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4020 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4021
4022 // Expand the incoming operand to be shifted, so that we have its parts
4023 SDOperand InL, InH;
4024 ExpandOp(Op, InL, InH);
4025 switch(Opc) {
4026 case ISD::SHL:
4027 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4028 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4029 return true;
4030 case ISD::SRL:
4031 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4032 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4033 return true;
4034 case ISD::SRA:
4035 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4036 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4037 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4038 return true;
4039 }
4040 }
4041
4042 // If we know that the high bit of the shift amount is zero, then we can do
4043 // this as a couple of simple shifts.
4044 if (KnownZero & Mask) {
4045 // Compute 32-amt.
4046 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4047 DAG.getConstant(NVTBits, Amt.getValueType()),
4048 Amt);
4049
4050 // Expand the incoming operand to be shifted, so that we have its parts
4051 SDOperand InL, InH;
4052 ExpandOp(Op, InL, InH);
4053 switch(Opc) {
4054 case ISD::SHL:
4055 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4056 Hi = DAG.getNode(ISD::OR, NVT,
4057 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4058 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4059 return true;
4060 case ISD::SRL:
4061 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4062 Lo = DAG.getNode(ISD::OR, NVT,
4063 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4064 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4065 return true;
4066 case ISD::SRA:
4067 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4068 Lo = DAG.getNode(ISD::OR, NVT,
4069 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4070 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4071 return true;
4072 }
4073 }
4074
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004075 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004076}
Chris Lattner77e77a62005-01-21 06:05:23 +00004077
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004078
Chris Lattner77e77a62005-01-21 06:05:23 +00004079// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4080// does not fit into a register, return the lo part and set the hi part to the
4081// by-reg argument. If it does fit into a single register, return the result
4082// and leave the Hi part unset.
4083SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004084 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004085 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4086 // The input chain to this libcall is the entry node of the function.
4087 // Legalizing the call will automatically add the previous call to the
4088 // dependence.
4089 SDOperand InChain = DAG.getEntryNode();
4090
Chris Lattner77e77a62005-01-21 06:05:23 +00004091 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004092 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004093 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4094 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4095 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004096 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4097 Entry.isSigned = isSigned;
4098 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004099 }
4100 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004101
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004102 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004103 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004104 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004105 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004106 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004107
Chris Lattner6831a812006-02-13 09:18:02 +00004108 // Legalize the call sequence, starting with the chain. This will advance
4109 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4110 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4111 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004112 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004113 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004114 default: assert(0 && "Unknown thing");
4115 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004116 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004117 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004118 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004119 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004120 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004121 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004122 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004123}
4124
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004125
Chris Lattner77e77a62005-01-21 06:05:23 +00004126/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4127/// destination type is legal.
4128SDOperand SelectionDAGLegalize::
4129ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004130 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00004131 assert(getTypeAction(Source.getValueType()) == Expand &&
4132 "This is not an expansion!");
4133 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4134
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004135 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004136 assert(Source.getValueType() == MVT::i64 &&
4137 "This only works for 64-bit -> FP");
4138 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4139 // incoming integer is set. To handle this, we dynamically test to see if
4140 // it is set, and, if so, add a fudge factor.
4141 SDOperand Lo, Hi;
4142 ExpandOp(Source, Lo, Hi);
4143
Chris Lattner66de05b2005-05-13 04:45:13 +00004144 // If this is unsigned, and not supported, first perform the conversion to
4145 // signed, then adjust the result if the sign bit is set.
4146 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4147 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4148
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004149 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4150 DAG.getConstant(0, Hi.getValueType()),
4151 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004152 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4153 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4154 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004155 uint64_t FF = 0x5f800000ULL;
4156 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004157 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004158
Chris Lattner5839bf22005-08-26 17:15:30 +00004159 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004160 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4161 SDOperand FudgeInReg;
4162 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004163 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004164 else {
4165 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00004166 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004167 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004168 }
Chris Lattner473a9902005-09-29 06:44:39 +00004169 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004170 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004171
Chris Lattnera88a2602005-05-14 05:33:54 +00004172 // Check to see if the target has a custom way to lower this. If so, use it.
4173 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4174 default: assert(0 && "This action not implemented for this operation!");
4175 case TargetLowering::Legal:
4176 case TargetLowering::Expand:
4177 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004178 case TargetLowering::Custom: {
4179 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4180 Source), DAG);
4181 if (NV.Val)
4182 return LegalizeOp(NV);
4183 break; // The target decided this was legal after all
4184 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004185 }
4186
Chris Lattner13689e22005-05-12 07:00:44 +00004187 // Expand the source, then glue it back together for the call. We must expand
4188 // the source in case it is shared (this pass of legalize must traverse it).
4189 SDOperand SrcLo, SrcHi;
4190 ExpandOp(Source, SrcLo, SrcHi);
4191 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4192
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004193 const char *FnName = 0;
4194 if (DestTy == MVT::f32)
4195 FnName = "__floatdisf";
4196 else {
4197 assert(DestTy == MVT::f64 && "Unknown fp value type!");
4198 FnName = "__floatdidf";
4199 }
Chris Lattner6831a812006-02-13 09:18:02 +00004200
4201 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4202 SDOperand UnusedHiPart;
Reid Spencer47857812006-12-31 05:55:36 +00004203 return ExpandLibCall(FnName, Source.Val, isSigned, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004204}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004205
Chris Lattner22cde6a2006-01-28 08:25:58 +00004206/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4207/// INT_TO_FP operation of the specified operand when the target requests that
4208/// we expand it. At this point, we know that the result and operand types are
4209/// legal for the target.
4210SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4211 SDOperand Op0,
4212 MVT::ValueType DestVT) {
4213 if (Op0.getValueType() == MVT::i32) {
4214 // simple 32-bit [signed|unsigned] integer to float/double expansion
4215
4216 // get the stack frame index of a 8 byte buffer
4217 MachineFunction &MF = DAG.getMachineFunction();
4218 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4219 // get address of 8 byte buffer
4220 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4221 // word offset constant for Hi/Lo address computation
4222 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4223 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004224 SDOperand Hi = StackSlot;
4225 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4226 if (TLI.isLittleEndian())
4227 std::swap(Hi, Lo);
4228
Chris Lattner22cde6a2006-01-28 08:25:58 +00004229 // if signed map to unsigned space
4230 SDOperand Op0Mapped;
4231 if (isSigned) {
4232 // constant used to invert sign bit (signed to unsigned mapping)
4233 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4234 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4235 } else {
4236 Op0Mapped = Op0;
4237 }
4238 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004239 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004240 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004241 // initial hi portion of constructed double
4242 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4243 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004244 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004245 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004246 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004247 // FP constant to bias correct the final result
4248 SDOperand Bias = DAG.getConstantFP(isSigned ?
4249 BitsToDouble(0x4330000080000000ULL)
4250 : BitsToDouble(0x4330000000000000ULL),
4251 MVT::f64);
4252 // subtract the bias
4253 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4254 // final result
4255 SDOperand Result;
4256 // handle final rounding
4257 if (DestVT == MVT::f64) {
4258 // do nothing
4259 Result = Sub;
4260 } else {
4261 // if f32 then cast to f32
4262 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4263 }
4264 return Result;
4265 }
4266 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4267 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4268
4269 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4270 DAG.getConstant(0, Op0.getValueType()),
4271 ISD::SETLT);
4272 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4273 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4274 SignSet, Four, Zero);
4275
4276 // If the sign bit of the integer is set, the large number will be treated
4277 // as a negative number. To counteract this, the dynamic code adds an
4278 // offset depending on the data type.
4279 uint64_t FF;
4280 switch (Op0.getValueType()) {
4281 default: assert(0 && "Unsupported integer type!");
4282 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4283 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4284 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4285 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4286 }
4287 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004288 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004289
4290 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4291 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4292 SDOperand FudgeInReg;
4293 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004294 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004295 else {
4296 assert(DestVT == MVT::f64 && "Unexpected conversion");
4297 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4298 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004299 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004300 }
4301
4302 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4303}
4304
4305/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4306/// *INT_TO_FP operation of the specified operand when the target requests that
4307/// we promote it. At this point, we know that the result and operand types are
4308/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4309/// operation that takes a larger input.
4310SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4311 MVT::ValueType DestVT,
4312 bool isSigned) {
4313 // First step, figure out the appropriate *INT_TO_FP operation to use.
4314 MVT::ValueType NewInTy = LegalOp.getValueType();
4315
4316 unsigned OpToUse = 0;
4317
4318 // Scan for the appropriate larger type to use.
4319 while (1) {
4320 NewInTy = (MVT::ValueType)(NewInTy+1);
4321 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4322
4323 // If the target supports SINT_TO_FP of this type, use it.
4324 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4325 default: break;
4326 case TargetLowering::Legal:
4327 if (!TLI.isTypeLegal(NewInTy))
4328 break; // Can't use this datatype.
4329 // FALL THROUGH.
4330 case TargetLowering::Custom:
4331 OpToUse = ISD::SINT_TO_FP;
4332 break;
4333 }
4334 if (OpToUse) break;
4335 if (isSigned) continue;
4336
4337 // If the target supports UINT_TO_FP of this type, use it.
4338 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4339 default: break;
4340 case TargetLowering::Legal:
4341 if (!TLI.isTypeLegal(NewInTy))
4342 break; // Can't use this datatype.
4343 // FALL THROUGH.
4344 case TargetLowering::Custom:
4345 OpToUse = ISD::UINT_TO_FP;
4346 break;
4347 }
4348 if (OpToUse) break;
4349
4350 // Otherwise, try a larger type.
4351 }
4352
4353 // Okay, we found the operation and type to use. Zero extend our input to the
4354 // desired type then run the operation on it.
4355 return DAG.getNode(OpToUse, DestVT,
4356 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4357 NewInTy, LegalOp));
4358}
4359
4360/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4361/// FP_TO_*INT operation of the specified operand when the target requests that
4362/// we promote it. At this point, we know that the result and operand types are
4363/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4364/// operation that returns a larger result.
4365SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4366 MVT::ValueType DestVT,
4367 bool isSigned) {
4368 // First step, figure out the appropriate FP_TO*INT operation to use.
4369 MVT::ValueType NewOutTy = DestVT;
4370
4371 unsigned OpToUse = 0;
4372
4373 // Scan for the appropriate larger type to use.
4374 while (1) {
4375 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4376 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4377
4378 // If the target supports FP_TO_SINT returning this type, use it.
4379 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4380 default: break;
4381 case TargetLowering::Legal:
4382 if (!TLI.isTypeLegal(NewOutTy))
4383 break; // Can't use this datatype.
4384 // FALL THROUGH.
4385 case TargetLowering::Custom:
4386 OpToUse = ISD::FP_TO_SINT;
4387 break;
4388 }
4389 if (OpToUse) break;
4390
4391 // If the target supports FP_TO_UINT of this type, use it.
4392 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4393 default: break;
4394 case TargetLowering::Legal:
4395 if (!TLI.isTypeLegal(NewOutTy))
4396 break; // Can't use this datatype.
4397 // FALL THROUGH.
4398 case TargetLowering::Custom:
4399 OpToUse = ISD::FP_TO_UINT;
4400 break;
4401 }
4402 if (OpToUse) break;
4403
4404 // Otherwise, try a larger type.
4405 }
4406
4407 // Okay, we found the operation and type to use. Truncate the result of the
4408 // extended FP_TO_*INT operation to the desired size.
4409 return DAG.getNode(ISD::TRUNCATE, DestVT,
4410 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4411}
4412
4413/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4414///
4415SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4416 MVT::ValueType VT = Op.getValueType();
4417 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4418 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4419 switch (VT) {
4420 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4421 case MVT::i16:
4422 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4423 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4424 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4425 case MVT::i32:
4426 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4427 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4428 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4429 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4430 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4431 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4432 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4433 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4434 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4435 case MVT::i64:
4436 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4437 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4438 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4439 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4440 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4441 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4442 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4443 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4444 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4445 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4446 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4447 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4448 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4449 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4450 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4451 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4452 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4453 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4454 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4455 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4456 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4457 }
4458}
4459
4460/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4461///
4462SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4463 switch (Opc) {
4464 default: assert(0 && "Cannot expand this yet!");
4465 case ISD::CTPOP: {
4466 static const uint64_t mask[6] = {
4467 0x5555555555555555ULL, 0x3333333333333333ULL,
4468 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4469 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4470 };
4471 MVT::ValueType VT = Op.getValueType();
4472 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4473 unsigned len = getSizeInBits(VT);
4474 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4475 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4476 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4477 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4478 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4479 DAG.getNode(ISD::AND, VT,
4480 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4481 }
4482 return Op;
4483 }
4484 case ISD::CTLZ: {
4485 // for now, we do this:
4486 // x = x | (x >> 1);
4487 // x = x | (x >> 2);
4488 // ...
4489 // x = x | (x >>16);
4490 // x = x | (x >>32); // for 64-bit input
4491 // return popcount(~x);
4492 //
4493 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4494 MVT::ValueType VT = Op.getValueType();
4495 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4496 unsigned len = getSizeInBits(VT);
4497 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4498 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4499 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4500 }
4501 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4502 return DAG.getNode(ISD::CTPOP, VT, Op);
4503 }
4504 case ISD::CTTZ: {
4505 // for now, we use: { return popcount(~x & (x - 1)); }
4506 // unless the target has ctlz but not ctpop, in which case we use:
4507 // { return 32 - nlz(~x & (x-1)); }
4508 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4509 MVT::ValueType VT = Op.getValueType();
4510 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4511 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4512 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4513 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4514 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4515 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4516 TLI.isOperationLegal(ISD::CTLZ, VT))
4517 return DAG.getNode(ISD::SUB, VT,
4518 DAG.getConstant(getSizeInBits(VT), VT),
4519 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4520 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4521 }
4522 }
4523}
Chris Lattnere34b3962005-01-19 04:19:40 +00004524
Chris Lattner3e928bb2005-01-07 07:47:09 +00004525/// ExpandOp - Expand the specified SDOperand into its two component pieces
4526/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4527/// LegalizeNodes map is filled in for any results that are not expanded, the
4528/// ExpandedNodes map is filled in for any results that are expanded, and the
4529/// Lo/Hi values are returned.
4530void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4531 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004532 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004533 SDNode *Node = Op.Val;
4534 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004535 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4536 VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004537 "Cannot expand to FP value or to larger int value!");
4538
Chris Lattner6fdcb252005-09-02 20:32:45 +00004539 // See if we already expanded it.
4540 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4541 = ExpandedNodes.find(Op);
4542 if (I != ExpandedNodes.end()) {
4543 Lo = I->second.first;
4544 Hi = I->second.second;
4545 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004546 }
4547
Chris Lattner3e928bb2005-01-07 07:47:09 +00004548 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004549 case ISD::CopyFromReg:
4550 assert(0 && "CopyFromReg must be legal!");
4551 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004552#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00004553 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004554#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004555 assert(0 && "Do not know how to expand this operator!");
4556 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004557 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004558 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004559 Lo = DAG.getNode(ISD::UNDEF, NVT);
4560 Hi = DAG.getNode(ISD::UNDEF, NVT);
4561 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004562 case ISD::Constant: {
4563 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4564 Lo = DAG.getConstant(Cst, NVT);
4565 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4566 break;
4567 }
Evan Cheng00495212006-12-12 21:32:44 +00004568 case ISD::ConstantFP: {
4569 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004570 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004571 if (getTypeAction(Lo.getValueType()) == Expand)
4572 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004573 break;
4574 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004575 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004576 // Return the operands.
4577 Lo = Node->getOperand(0);
4578 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004579 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004580
4581 case ISD::SIGN_EXTEND_INREG:
4582 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004583 // sext_inreg the low part if needed.
4584 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4585
4586 // The high part gets the sign extension from the lo-part. This handles
4587 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004588 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4589 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4590 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004591 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004592
Nate Begemand88fc032006-01-14 03:14:10 +00004593 case ISD::BSWAP: {
4594 ExpandOp(Node->getOperand(0), Lo, Hi);
4595 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4596 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4597 Lo = TempLo;
4598 break;
4599 }
4600
Chris Lattneredb1add2005-05-11 04:51:16 +00004601 case ISD::CTPOP:
4602 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004603 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4604 DAG.getNode(ISD::CTPOP, NVT, Lo),
4605 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004606 Hi = DAG.getConstant(0, NVT);
4607 break;
4608
Chris Lattner39a8f332005-05-12 19:05:01 +00004609 case ISD::CTLZ: {
4610 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004611 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004612 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4613 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004614 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4615 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004616 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4617 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4618
4619 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4620 Hi = DAG.getConstant(0, NVT);
4621 break;
4622 }
4623
4624 case ISD::CTTZ: {
4625 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004626 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004627 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4628 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004629 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4630 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004631 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4632 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4633
4634 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4635 Hi = DAG.getConstant(0, NVT);
4636 break;
4637 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004638
Nate Begemanacc398c2006-01-25 18:21:52 +00004639 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004640 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4641 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004642 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4643 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4644
4645 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004646 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004647 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4648 if (!TLI.isLittleEndian())
4649 std::swap(Lo, Hi);
4650 break;
4651 }
4652
Chris Lattner3e928bb2005-01-07 07:47:09 +00004653 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004654 LoadSDNode *LD = cast<LoadSDNode>(Node);
4655 SDOperand Ch = LD->getChain(); // Legalize the chain.
4656 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4657 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004658
Evan Cheng466685d2006-10-09 20:57:25 +00004659 if (ExtType == ISD::NON_EXTLOAD) {
4660 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng00495212006-12-12 21:32:44 +00004661 if (VT == MVT::f32 || VT == MVT::f64) {
4662 // f32->i32 or f64->i64 one to one expansion.
4663 // Remember that we legalized the chain.
4664 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004665 // Recursively expand the new load.
4666 if (getTypeAction(NVT) == Expand)
4667 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004668 break;
4669 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004670
Evan Cheng466685d2006-10-09 20:57:25 +00004671 // Increment the pointer to the other half.
4672 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4673 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4674 getIntPtrConstant(IncrementSize));
4675 // FIXME: This creates a bogus srcvalue!
4676 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004677
Evan Cheng466685d2006-10-09 20:57:25 +00004678 // Build a factor node to remember that this load is independent of the
4679 // other one.
4680 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4681 Hi.getValue(1));
4682
4683 // Remember that we legalized the chain.
4684 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4685 if (!TLI.isLittleEndian())
4686 std::swap(Lo, Hi);
4687 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004688 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004689
4690 if (VT == MVT::f64 && EVT == MVT::f32) {
4691 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4692 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4693 LD->getSrcValueOffset());
4694 // Remember that we legalized the chain.
4695 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4696 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4697 break;
4698 }
Evan Cheng466685d2006-10-09 20:57:25 +00004699
4700 if (EVT == NVT)
4701 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4702 LD->getSrcValueOffset());
4703 else
4704 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4705 LD->getSrcValueOffset(), EVT);
4706
4707 // Remember that we legalized the chain.
4708 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4709
4710 if (ExtType == ISD::SEXTLOAD) {
4711 // The high part is obtained by SRA'ing all but one of the bits of the
4712 // lo part.
4713 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4714 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4715 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4716 } else if (ExtType == ISD::ZEXTLOAD) {
4717 // The high part is just a zero.
4718 Hi = DAG.getConstant(0, NVT);
4719 } else /* if (ExtType == ISD::EXTLOAD) */ {
4720 // The high part is undefined.
4721 Hi = DAG.getNode(ISD::UNDEF, NVT);
4722 }
4723 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004724 break;
4725 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004726 case ISD::AND:
4727 case ISD::OR:
4728 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4729 SDOperand LL, LH, RL, RH;
4730 ExpandOp(Node->getOperand(0), LL, LH);
4731 ExpandOp(Node->getOperand(1), RL, RH);
4732 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4733 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4734 break;
4735 }
4736 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004737 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004738 ExpandOp(Node->getOperand(1), LL, LH);
4739 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004740 if (getTypeAction(NVT) == Expand)
4741 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004742 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004743 if (VT != MVT::f32)
4744 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004745 break;
4746 }
Nate Begeman9373a812005-08-10 20:51:12 +00004747 case ISD::SELECT_CC: {
4748 SDOperand TL, TH, FL, FH;
4749 ExpandOp(Node->getOperand(2), TL, TH);
4750 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004751 if (getTypeAction(NVT) == Expand)
4752 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004753 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4754 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004755 if (VT != MVT::f32)
4756 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4757 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004758 break;
4759 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004760 case ISD::ANY_EXTEND:
4761 // The low part is any extension of the input (which degenerates to a copy).
4762 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4763 // The high part is undefined.
4764 Hi = DAG.getNode(ISD::UNDEF, NVT);
4765 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004766 case ISD::SIGN_EXTEND: {
4767 // The low part is just a sign extension of the input (which degenerates to
4768 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004769 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004770
Chris Lattner3e928bb2005-01-07 07:47:09 +00004771 // The high part is obtained by SRA'ing all but one of the bits of the lo
4772 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004773 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004774 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4775 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004776 break;
4777 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004778 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004779 // The low part is just a zero extension of the input (which degenerates to
4780 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004781 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004782
Chris Lattner3e928bb2005-01-07 07:47:09 +00004783 // The high part is just a zero.
4784 Hi = DAG.getConstant(0, NVT);
4785 break;
Chris Lattner35481892005-12-23 00:16:34 +00004786
4787 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004788 SDOperand Tmp;
4789 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4790 // If the target wants to, allow it to lower this itself.
4791 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4792 case Expand: assert(0 && "cannot expand FP!");
4793 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4794 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4795 }
4796 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4797 }
4798
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004799 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00004800 if (VT == MVT::f32 || VT == MVT::f64) {
4801 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00004802 if (getTypeAction(NVT) == Expand)
4803 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00004804 break;
4805 }
4806
4807 // If source operand will be expanded to the same type as VT, i.e.
4808 // i64 <- f64, i32 <- f32, expand the source operand instead.
4809 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4810 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4811 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004812 break;
4813 }
4814
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004815 // Turn this into a load/store pair by default.
4816 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00004817 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004818
Chris Lattner35481892005-12-23 00:16:34 +00004819 ExpandOp(Tmp, Lo, Hi);
4820 break;
4821 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004822
Chris Lattner8137c9e2006-01-28 05:07:51 +00004823 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004824 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4825 TargetLowering::Custom &&
4826 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004827 Lo = TLI.LowerOperation(Op, DAG);
4828 assert(Lo.Val && "Node must be custom expanded!");
4829 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004830 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004831 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004832 break;
4833
Chris Lattner4e6c7462005-01-08 19:27:05 +00004834 // These operators cannot be expanded directly, emit them as calls to
4835 // library functions.
4836 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004837 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004838 SDOperand Op;
4839 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4840 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004841 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4842 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004843 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004844
Chris Lattnerf20d1832005-07-30 01:40:57 +00004845 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4846
Chris Lattner80a3e942005-07-29 00:33:32 +00004847 // Now that the custom expander is done, expand the result, which is still
4848 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004849 if (Op.Val) {
4850 ExpandOp(Op, Lo, Hi);
4851 break;
4852 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004853 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004854
Chris Lattner4e6c7462005-01-08 19:27:05 +00004855 if (Node->getOperand(0).getValueType() == MVT::f32)
Reid Spencerb47b25c2007-01-03 04:22:32 +00004856 Lo = ExpandLibCall("__fixsfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004857 else
Reid Spencerb47b25c2007-01-03 04:22:32 +00004858 Lo = ExpandLibCall("__fixdfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004859 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004860
Chris Lattner4e6c7462005-01-08 19:27:05 +00004861 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004862 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004863 SDOperand Op;
4864 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4865 case Expand: assert(0 && "cannot expand FP!");
4866 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4867 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4868 }
4869
4870 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4871
4872 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004873 if (Op.Val) {
4874 ExpandOp(Op, Lo, Hi);
4875 break;
4876 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004877 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004878
Chris Lattner4e6c7462005-01-08 19:27:05 +00004879 if (Node->getOperand(0).getValueType() == MVT::f32)
Reid Spencerb47b25c2007-01-03 04:22:32 +00004880 Lo = ExpandLibCall("__fixunssfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004881 else
Reid Spencerb47b25c2007-01-03 04:22:32 +00004882 Lo = ExpandLibCall("__fixunsdfdi", Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004883 break;
4884
Evan Cheng05a2d562006-01-09 18:31:59 +00004885 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004886 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004887 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004888 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004889 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004890 Op = TLI.LowerOperation(Op, DAG);
4891 if (Op.Val) {
4892 // Now that the custom expander is done, expand the result, which is
4893 // still VT.
4894 ExpandOp(Op, Lo, Hi);
4895 break;
4896 }
4897 }
4898
Chris Lattner79980b02006-09-13 03:50:39 +00004899 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4900 // this X << 1 as X+X.
4901 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4902 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4903 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4904 SDOperand LoOps[2], HiOps[3];
4905 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4906 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4907 LoOps[1] = LoOps[0];
4908 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4909
4910 HiOps[1] = HiOps[0];
4911 HiOps[2] = Lo.getValue(1);
4912 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4913 break;
4914 }
4915 }
4916
Chris Lattnere34b3962005-01-19 04:19:40 +00004917 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004918 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004919 break;
Chris Lattner47599822005-04-02 03:38:53 +00004920
4921 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004922 TargetLowering::LegalizeAction Action =
4923 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4924 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4925 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004926 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004927 break;
4928 }
4929
Chris Lattnere34b3962005-01-19 04:19:40 +00004930 // Otherwise, emit a libcall.
Reid Spencerb47b25c2007-01-03 04:22:32 +00004931 Lo = ExpandLibCall("__ashldi3", Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004932 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004933 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004934
Evan Cheng05a2d562006-01-09 18:31:59 +00004935 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004936 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004937 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004938 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004939 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004940 Op = TLI.LowerOperation(Op, DAG);
4941 if (Op.Val) {
4942 // Now that the custom expander is done, expand the result, which is
4943 // still VT.
4944 ExpandOp(Op, Lo, Hi);
4945 break;
4946 }
4947 }
4948
Chris Lattnere34b3962005-01-19 04:19:40 +00004949 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004950 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004951 break;
Chris Lattner47599822005-04-02 03:38:53 +00004952
4953 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004954 TargetLowering::LegalizeAction Action =
4955 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4956 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4957 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004958 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004959 break;
4960 }
4961
Chris Lattnere34b3962005-01-19 04:19:40 +00004962 // Otherwise, emit a libcall.
Reid Spencerb47b25c2007-01-03 04:22:32 +00004963 Lo = ExpandLibCall("__ashrdi3", Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004964 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004965 }
4966
4967 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004968 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004969 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004970 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004971 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004972 Op = TLI.LowerOperation(Op, DAG);
4973 if (Op.Val) {
4974 // Now that the custom expander is done, expand the result, which is
4975 // still VT.
4976 ExpandOp(Op, Lo, Hi);
4977 break;
4978 }
4979 }
4980
Chris Lattnere34b3962005-01-19 04:19:40 +00004981 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004982 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004983 break;
Chris Lattner47599822005-04-02 03:38:53 +00004984
4985 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004986 TargetLowering::LegalizeAction Action =
4987 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4988 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4989 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004990 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004991 break;
4992 }
4993
Chris Lattnere34b3962005-01-19 04:19:40 +00004994 // Otherwise, emit a libcall.
Reid Spencerb47b25c2007-01-03 04:22:32 +00004995 Lo = ExpandLibCall("__lshrdi3", Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004996 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004997 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004998
Misha Brukmanedf128a2005-04-21 22:36:52 +00004999 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005000 case ISD::SUB: {
5001 // If the target wants to custom expand this, let them.
5002 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5003 TargetLowering::Custom) {
5004 Op = TLI.LowerOperation(Op, DAG);
5005 if (Op.Val) {
5006 ExpandOp(Op, Lo, Hi);
5007 break;
5008 }
5009 }
5010
5011 // Expand the subcomponents.
5012 SDOperand LHSL, LHSH, RHSL, RHSH;
5013 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5014 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005015 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5016 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005017 LoOps[0] = LHSL;
5018 LoOps[1] = RHSL;
5019 HiOps[0] = LHSH;
5020 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005021 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005022 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005023 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005024 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005025 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005026 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005027 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005028 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005029 }
Chris Lattner84f67882005-01-20 18:52:28 +00005030 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005031 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005032 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005033 // If the target wants to custom expand this, let them.
5034 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005035 SDOperand New = TLI.LowerOperation(Op, DAG);
5036 if (New.Val) {
5037 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005038 break;
5039 }
5040 }
5041
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005042 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5043 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005044 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005045 SDOperand LL, LH, RL, RH;
5046 ExpandOp(Node->getOperand(0), LL, LH);
5047 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005048 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005049 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005050 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5051 // extended the sign bit of the low half through the upper half, and if so
5052 // emit a MULHS instead of the alternate sequence that is valid for any
5053 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005054 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005055 // is RH an extension of the sign bit of RL?
5056 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5057 RH.getOperand(1).getOpcode() == ISD::Constant &&
5058 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5059 // is LH an extension of the sign bit of LL?
5060 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5061 LH.getOperand(1).getOpcode() == ISD::Constant &&
5062 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005063 // Low part:
5064 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5065 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005066 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005067 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005068 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005069 // Low part:
5070 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5071
5072 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005073 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5074 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5075 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5076 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5077 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005078 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005079 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005080 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005081
Reid Spencerb47b25c2007-01-03 04:22:32 +00005082 Lo = ExpandLibCall("__muldi3" , Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005083 break;
5084 }
Reid Spencer47857812006-12-31 05:55:36 +00005085 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, true, Hi); break;
5086 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, false, Hi); break;
5087 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, true, Hi); break;
5088 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, false, Hi); break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005089
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005090 case ISD::FADD:
Reid Spencer47857812006-12-31 05:55:36 +00005091 Lo = ExpandLibCall(((VT == MVT::f32) ? "__addsf3" : "__adddf3"), Node,
5092 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005093 break;
5094 case ISD::FSUB:
Reid Spencer47857812006-12-31 05:55:36 +00005095 Lo = ExpandLibCall(((VT == MVT::f32) ? "__subsf3" : "__subdf3"), Node,
5096 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005097 break;
5098 case ISD::FMUL:
Reid Spencer47857812006-12-31 05:55:36 +00005099 Lo = ExpandLibCall(((VT == MVT::f32) ? "__mulsf3" : "__muldf3"), Node,
5100 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005101 break;
5102 case ISD::FDIV:
Reid Spencer47857812006-12-31 05:55:36 +00005103 Lo = ExpandLibCall(((VT == MVT::f32) ? "__divsf3" : "__divdf3"), Node,
5104 false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005105 break;
5106 case ISD::FP_EXTEND:
Reid Spencer47857812006-12-31 05:55:36 +00005107 Lo = ExpandLibCall("__extendsfdf2", Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005108 break;
5109 case ISD::FP_ROUND:
Reid Spencer47857812006-12-31 05:55:36 +00005110 Lo = ExpandLibCall("__truncdfsf2", Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005111 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005112 case ISD::FSQRT:
5113 case ISD::FSIN:
5114 case ISD::FCOS: {
5115 const char *FnName = 0;
5116 switch(Node->getOpcode()) {
5117 case ISD::FSQRT: FnName = (VT == MVT::f32) ? "sqrtf" : "sqrt"; break;
5118 case ISD::FSIN: FnName = (VT == MVT::f32) ? "sinf" : "sin"; break;
5119 case ISD::FCOS: FnName = (VT == MVT::f32) ? "cosf" : "cos"; break;
5120 default: assert(0 && "Unreachable!");
5121 }
Reid Spencer47857812006-12-31 05:55:36 +00005122 Lo = ExpandLibCall(FnName, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005123 break;
5124 }
Evan Cheng966bf242006-12-16 00:52:40 +00005125 case ISD::FABS: {
5126 SDOperand Mask = (VT == MVT::f64)
5127 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5128 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5129 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5130 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5131 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5132 if (getTypeAction(NVT) == Expand)
5133 ExpandOp(Lo, Lo, Hi);
5134 break;
5135 }
5136 case ISD::FNEG: {
5137 SDOperand Mask = (VT == MVT::f64)
5138 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5139 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5140 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5141 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5142 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5143 if (getTypeAction(NVT) == Expand)
5144 ExpandOp(Lo, Lo, Hi);
5145 break;
5146 }
Evan Cheng912095b2007-01-04 21:56:39 +00005147 case ISD::FCOPYSIGN: {
5148 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5149 if (getTypeAction(NVT) == Expand)
5150 ExpandOp(Lo, Lo, Hi);
5151 break;
5152 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005153 case ISD::SINT_TO_FP:
5154 case ISD::UINT_TO_FP: {
5155 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5156 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
5157 const char *FnName = 0;
5158 if (Node->getOperand(0).getValueType() == MVT::i64) {
5159 if (VT == MVT::f32)
5160 FnName = isSigned ? "__floatdisf" : "__floatundisf";
5161 else
5162 FnName = isSigned ? "__floatdidf" : "__floatundidf";
5163 } else {
5164 if (VT == MVT::f32)
5165 FnName = isSigned ? "__floatsisf" : "__floatunsisf";
5166 else
5167 FnName = isSigned ? "__floatsidf" : "__floatunsidf";
5168 }
5169
5170 // Promote the operand if needed.
5171 if (getTypeAction(SrcVT) == Promote) {
5172 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5173 Tmp = isSigned
5174 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5175 DAG.getValueType(SrcVT))
5176 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5177 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5178 }
Reid Spencer47857812006-12-31 05:55:36 +00005179 Lo = ExpandLibCall(FnName, Node, isSigned, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00005180 break;
5181 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005182 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005183
Chris Lattner83397362005-12-21 18:02:52 +00005184 // Make sure the resultant values have been legalized themselves, unless this
5185 // is a type that requires multi-step expansion.
5186 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5187 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005188 if (Hi.Val)
5189 // Don't legalize the high part if it is expanded to a single node.
5190 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005191 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005192
5193 // Remember in a map if the values will be reused later.
5194 bool isNew =
5195 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5196 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005197}
5198
Chris Lattnerc7029802006-03-18 01:44:44 +00005199/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5200/// two smaller values of MVT::Vector type.
5201void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5202 SDOperand &Hi) {
5203 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5204 SDNode *Node = Op.Val;
5205 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5206 assert(NumElements > 1 && "Cannot split a single element vector!");
5207 unsigned NewNumElts = NumElements/2;
5208 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5209 SDOperand TypeNode = *(Node->op_end()-1);
5210
5211 // See if we already split it.
5212 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5213 = SplitNodes.find(Op);
5214 if (I != SplitNodes.end()) {
5215 Lo = I->second.first;
5216 Hi = I->second.second;
5217 return;
5218 }
5219
5220 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005221 default:
5222#ifndef NDEBUG
5223 Node->dump();
5224#endif
5225 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00005226 case ISD::VBUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005227 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5228 Node->op_begin()+NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005229 LoOps.push_back(NewNumEltsNode);
5230 LoOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005231 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005232
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005233 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5234 Node->op_end()-2);
Chris Lattnerc7029802006-03-18 01:44:44 +00005235 HiOps.push_back(NewNumEltsNode);
5236 HiOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005237 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005238 break;
5239 }
5240 case ISD::VADD:
5241 case ISD::VSUB:
5242 case ISD::VMUL:
5243 case ISD::VSDIV:
5244 case ISD::VUDIV:
5245 case ISD::VAND:
5246 case ISD::VOR:
5247 case ISD::VXOR: {
5248 SDOperand LL, LH, RL, RH;
5249 SplitVectorOp(Node->getOperand(0), LL, LH);
5250 SplitVectorOp(Node->getOperand(1), RL, RH);
5251
5252 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5253 NewNumEltsNode, TypeNode);
5254 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5255 NewNumEltsNode, TypeNode);
5256 break;
5257 }
5258 case ISD::VLOAD: {
5259 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5260 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5261 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5262
5263 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5264 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5265 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5266 getIntPtrConstant(IncrementSize));
5267 // FIXME: This creates a bogus srcvalue!
5268 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5269
5270 // Build a factor node to remember that this load is independent of the
5271 // other one.
5272 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5273 Hi.getValue(1));
5274
5275 // Remember that we legalized the chain.
5276 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005277 break;
5278 }
Chris Lattner7692eb42006-03-23 21:16:34 +00005279 case ISD::VBIT_CONVERT: {
5280 // We know the result is a vector. The input may be either a vector or a
5281 // scalar value.
5282 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5283 // Lower to a store/load. FIXME: this could be improved probably.
5284 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5285
Evan Cheng786225a2006-10-05 23:01:46 +00005286 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005287 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005288 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5289 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5290 SplitVectorOp(St, Lo, Hi);
5291 } else {
5292 // If the input is a vector type, we have to either scalarize it, pack it
5293 // or convert it based on whether the input vector type is legal.
5294 SDNode *InVal = Node->getOperand(0).Val;
5295 unsigned NumElems =
5296 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5297 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5298
5299 // If the input is from a single element vector, scalarize the vector,
5300 // then treat like a scalar.
5301 if (NumElems == 1) {
5302 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5303 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5304 Op.getOperand(1), Op.getOperand(2));
5305 SplitVectorOp(Scalar, Lo, Hi);
5306 } else {
5307 // Split the input vector.
5308 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5309
5310 // Convert each of the pieces now.
5311 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5312 NewNumEltsNode, TypeNode);
5313 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5314 NewNumEltsNode, TypeNode);
5315 }
5316 break;
5317 }
5318 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005319 }
5320
5321 // Remember in a map if the values will be reused later.
5322 bool isNew =
5323 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5324 assert(isNew && "Value already expanded?!?");
5325}
5326
5327
5328/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5329/// equivalent operation that returns a scalar (e.g. F32) or packed value
5330/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5331/// type for the result.
5332SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5333 MVT::ValueType NewVT) {
5334 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5335 SDNode *Node = Op.Val;
5336
5337 // See if we already packed it.
5338 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5339 if (I != PackedNodes.end()) return I->second;
5340
5341 SDOperand Result;
5342 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005343 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005344#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00005345 Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005346#endif
Chris Lattner2332b9f2006-03-19 01:17:20 +00005347 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005348 case ISD::VADD:
5349 case ISD::VSUB:
5350 case ISD::VMUL:
5351 case ISD::VSDIV:
5352 case ISD::VUDIV:
5353 case ISD::VAND:
5354 case ISD::VOR:
5355 case ISD::VXOR:
5356 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5357 NewVT,
5358 PackVectorOp(Node->getOperand(0), NewVT),
5359 PackVectorOp(Node->getOperand(1), NewVT));
5360 break;
5361 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00005362 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5363 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005364
Evan Cheng466685d2006-10-09 20:57:25 +00005365 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5366 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattnerc7029802006-03-18 01:44:44 +00005367
5368 // Remember that we legalized the chain.
5369 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5370 break;
5371 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00005372 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00005373 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005374 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00005375 Result = Node->getOperand(0);
5376 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005377 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00005378
5379 // If all elements of the build_vector are undefs, return an undef.
5380 bool AllUndef = true;
5381 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5382 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5383 AllUndef = false;
5384 break;
5385 }
5386 if (AllUndef) {
5387 Result = DAG.getNode(ISD::UNDEF, NewVT);
5388 } else {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005389 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5390 Node->getNumOperands()-2);
Chris Lattner17614ea2006-04-08 05:34:25 +00005391 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005392 }
5393 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00005394 case ISD::VINSERT_VECTOR_ELT:
5395 if (!MVT::isVector(NewVT)) {
5396 // Returning a scalar? Must be the inserted element.
5397 Result = Node->getOperand(1);
5398 } else {
5399 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5400 PackVectorOp(Node->getOperand(0), NewVT),
5401 Node->getOperand(1), Node->getOperand(2));
5402 }
5403 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00005404 case ISD::VVECTOR_SHUFFLE:
5405 if (!MVT::isVector(NewVT)) {
5406 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5407 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5408 if (cast<ConstantSDNode>(EltNum)->getValue())
5409 Result = PackVectorOp(Node->getOperand(1), NewVT);
5410 else
5411 Result = PackVectorOp(Node->getOperand(0), NewVT);
5412 } else {
5413 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5414 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5415 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5416 Node->getOperand(2).Val->op_end()-2);
5417 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005418 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5419 Node->getOperand(2).Val->op_begin(),
5420 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattner5b2316e2006-03-28 20:24:43 +00005421
5422 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5423 PackVectorOp(Node->getOperand(0), NewVT),
5424 PackVectorOp(Node->getOperand(1), NewVT), BV);
5425 }
5426 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00005427 case ISD::VBIT_CONVERT:
5428 if (Op.getOperand(0).getValueType() != MVT::Vector)
5429 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5430 else {
5431 // If the input is a vector type, we have to either scalarize it, pack it
5432 // or convert it based on whether the input vector type is legal.
5433 SDNode *InVal = Node->getOperand(0).Val;
5434 unsigned NumElems =
5435 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5436 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5437
5438 // Figure out if there is a Packed type corresponding to this Vector
5439 // type. If so, convert to the packed type.
5440 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5441 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5442 // Turn this into a bit convert of the packed input.
5443 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5444 PackVectorOp(Node->getOperand(0), TVT));
5445 break;
5446 } else if (NumElems == 1) {
5447 // Turn this into a bit convert of the scalar input.
5448 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5449 PackVectorOp(Node->getOperand(0), EVT));
5450 break;
5451 } else {
5452 // FIXME: UNIMP!
5453 assert(0 && "Cast from unsupported vector type not implemented yet!");
5454 }
5455 }
Evan Chengdb3c6262006-04-10 18:54:36 +00005456 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005457 case ISD::VSELECT:
5458 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5459 PackVectorOp(Op.getOperand(1), NewVT),
5460 PackVectorOp(Op.getOperand(2), NewVT));
5461 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005462 }
5463
5464 if (TLI.isTypeLegal(NewVT))
5465 Result = LegalizeOp(Result);
5466 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5467 assert(isNew && "Value already packed?");
5468 return Result;
5469}
5470
Chris Lattner3e928bb2005-01-07 07:47:09 +00005471
5472// SelectionDAG::Legalize - This is the entry point for the file.
5473//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005474void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005475 if (ViewLegalizeDAGs) viewGraph();
5476
Chris Lattner3e928bb2005-01-07 07:47:09 +00005477 /// run - This is the main entry point to this class.
5478 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005479 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005480}
5481