blob: 1d624dbc14742ea0566c99f225f82af46b477a27 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000017#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000018#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000019#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000020#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000022#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/CommandLine.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include <iostream>
Evan Cheng033e6812006-03-24 01:17:21 +000025#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000026using namespace llvm;
27
Chris Lattner5e46a192006-04-02 03:07:27 +000028#ifndef NDEBUG
29static cl::opt<bool>
30ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
31 cl::desc("Pop up a window to show dags before legalize"));
32#else
33static const bool ViewLegalizeDAGs = 0;
34#endif
35
Chris Lattner3e928bb2005-01-07 07:47:09 +000036//===----------------------------------------------------------------------===//
37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
38/// hacks on it until the target machine can handle it. This involves
39/// eliminating value sizes the machine cannot handle (promoting small sizes to
40/// large sizes or splitting up large values into small values) as well as
41/// eliminating operations the machine cannot handle.
42///
43/// This code also does a small amount of optimization and recognition of idioms
44/// as part of its processing. For example, if a target does not support a
45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
46/// will attempt merge setcc and brc instructions into brcc's.
47///
48namespace {
49class SelectionDAGLegalize {
50 TargetLowering &TLI;
51 SelectionDAG &DAG;
52
Chris Lattner6831a812006-02-13 09:18:02 +000053 // Libcall insertion helpers.
54
55 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
56 /// legalized. We use this to ensure that calls are properly serialized
57 /// against each other, including inserted libcalls.
58 SDOperand LastCALLSEQ_END;
59
60 /// IsLegalizingCall - This member is used *only* for purposes of providing
61 /// helpful assertions that a libcall isn't created while another call is
62 /// being legalized (which could lead to non-serialized call sequences).
63 bool IsLegalizingCall;
64
Chris Lattner3e928bb2005-01-07 07:47:09 +000065 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000066 Legal, // The target natively supports this operation.
67 Promote, // This operation should be executed in a larger type.
68 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000069 };
Chris Lattner6831a812006-02-13 09:18:02 +000070
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ValueTypeActions - This is a bitvector that contains two bits for each
72 /// value type, where the two bits correspond to the LegalizeAction enum.
73 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000074 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000075
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 /// LegalizedNodes - For nodes that are of legal width, and that have more
77 /// than one use, this map indicates what regularized operand to use. This
78 /// allows us to avoid legalizing the same thing more than once.
79 std::map<SDOperand, SDOperand> LegalizedNodes;
80
Chris Lattner03c85462005-01-15 05:21:40 +000081 /// PromotedNodes - For nodes that are below legal width, and that have more
82 /// than one use, this map indicates what promoted value to use. This allows
83 /// us to avoid promoting the same thing more than once.
84 std::map<SDOperand, SDOperand> PromotedNodes;
85
Chris Lattnerc7029802006-03-18 01:44:44 +000086 /// ExpandedNodes - For nodes that need to be expanded this map indicates
87 /// which which operands are the expanded version of the input. This allows
88 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000089 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
90
Chris Lattnerc7029802006-03-18 01:44:44 +000091 /// SplitNodes - For vector nodes that need to be split, this map indicates
92 /// which which operands are the split version of the input. This allows us
93 /// to avoid splitting the same node more than once.
94 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
95
96 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
97 /// concrete packed types, this contains the mapping of ones we have already
98 /// processed to the result.
99 std::map<SDOperand, SDOperand> PackedNodes;
100
Chris Lattner8afc48e2005-01-07 22:28:47 +0000101 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000102 LegalizedNodes.insert(std::make_pair(From, To));
103 // If someone requests legalization of the new node, return itself.
104 if (From != To)
105 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000106 }
Chris Lattner03c85462005-01-15 05:21:40 +0000107 void AddPromotedOperand(SDOperand From, SDOperand To) {
108 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
109 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000110 // If someone requests legalization of the new node, return itself.
111 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000112 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113
Chris Lattner3e928bb2005-01-07 07:47:09 +0000114public:
115
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000116 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000117
Chris Lattner3e928bb2005-01-07 07:47:09 +0000118 /// getTypeAction - Return how we should legalize values of this type, either
119 /// it is already legal or we need to expand it into multiple registers of
120 /// smaller integer type, or we need to promote it to a larger type.
121 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000122 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123 }
124
125 /// isTypeLegal - Return true if this type is legal on this target.
126 ///
127 bool isTypeLegal(MVT::ValueType VT) const {
128 return getTypeAction(VT) == Legal;
129 }
130
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131 void LegalizeDAG();
132
Chris Lattner456a93a2006-01-28 07:39:30 +0000133private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000134 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
135 /// appropriate for its type.
136 void HandleOp(SDOperand Op);
137
138 /// LegalizeOp - We know that the specified value has a legal type.
139 /// Recursively ensure that the operands have legal types, then return the
140 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000142
143 /// PromoteOp - Given an operation that produces a value in an invalid type,
144 /// promote it to compute the value into a larger type. The produced value
145 /// will have the correct bits for the low portion of the register, but no
146 /// guarantee is made about the top bits: it may be zero, sign-extended, or
147 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000148 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149
Chris Lattnerc7029802006-03-18 01:44:44 +0000150 /// ExpandOp - Expand the specified SDOperand into its two component pieces
151 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
152 /// the LegalizeNodes map is filled in for any results that are not expanded,
153 /// the ExpandedNodes map is filled in for any results that are expanded, and
154 /// the Lo/Hi values are returned. This applies to integer types and Vector
155 /// types.
156 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
157
158 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
159 /// two smaller values of MVT::Vector type.
160 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
161
162 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
163 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
164 /// this is called, we know that PackedVT is the right type for the result and
165 /// we know that this type is legal for the target.
166 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
167
Chris Lattner4352cc92006-04-04 17:23:26 +0000168 /// isShuffleLegal - Return true if a vector shuffle is legal with the
169 /// specified mask and type. Targets can specify exactly which masks they
170 /// support and the code generator is tasked with not creating illegal masks.
171 ///
172 /// Note that this will also return true for shuffles that are promoted to a
173 /// different type.
174 ///
175 /// If this is a legal shuffle, this method returns the (possibly promoted)
176 /// build_vector Mask. If it's not a legal shuffle, it returns null.
177 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
178
Chris Lattner6831a812006-02-13 09:18:02 +0000179 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
180
Nate Begeman750ac1b2006-02-01 07:19:44 +0000181 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
182
Chris Lattnerce872152006-03-19 06:31:19 +0000183 SDOperand CreateStackTemporary(MVT::ValueType VT);
184
Chris Lattner77e77a62005-01-21 06:05:23 +0000185 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
186 SDOperand &Hi);
187 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
188 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000189
Chris Lattner35481892005-12-23 00:16:34 +0000190 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000191 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000192 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000193 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
194 SDOperand LegalOp,
195 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000196 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
197 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000198 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
199 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000200
Chris Lattner456a93a2006-01-28 07:39:30 +0000201 SDOperand ExpandBSWAP(SDOperand Op);
202 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000203 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
204 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000205 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
206 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000207
Chris Lattner15972212006-03-31 17:55:51 +0000208 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000209 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000210
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211 SDOperand getIntPtrConstant(uint64_t Val) {
212 return DAG.getConstant(Val, TLI.getPointerTy());
213 }
214};
215}
216
Chris Lattner4352cc92006-04-04 17:23:26 +0000217/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
218/// specified mask and type. Targets can specify exactly which masks they
219/// support and the code generator is tasked with not creating illegal masks.
220///
221/// Note that this will also return true for shuffles that are promoted to a
222/// different type.
223SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
224 SDOperand Mask) const {
225 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
226 default: return 0;
227 case TargetLowering::Legal:
228 case TargetLowering::Custom:
229 break;
230 case TargetLowering::Promote: {
231 // If this is promoted to a different type, convert the shuffle mask and
232 // ask if it is legal in the promoted type!
233 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
234
235 // If we changed # elements, change the shuffle mask.
236 unsigned NumEltsGrowth =
237 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
238 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
239 if (NumEltsGrowth > 1) {
240 // Renumber the elements.
241 std::vector<SDOperand> Ops;
242 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
243 SDOperand InOp = Mask.getOperand(i);
244 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
245 if (InOp.getOpcode() == ISD::UNDEF)
246 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
247 else {
248 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
249 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
250 }
251 }
252 }
253 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, Ops);
254 }
255 VT = NVT;
256 break;
257 }
258 }
259 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
260}
261
Chris Lattnerc7029802006-03-18 01:44:44 +0000262/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
263/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000264static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000265 switch (VecOp) {
266 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000267 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
268 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
269 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
270 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
271 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
272 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
273 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
274 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000275 }
276}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000277
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000278SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
279 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
280 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000281 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000282 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000283}
284
Chris Lattner32fca002005-10-06 01:20:27 +0000285/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
286/// not been visited yet and if all of its operands have already been visited.
287static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
288 std::map<SDNode*, unsigned> &Visited) {
289 if (++Visited[N] != N->getNumOperands())
290 return; // Haven't visited all operands yet
291
292 Order.push_back(N);
293
294 if (N->hasOneUse()) { // Tail recurse in common case.
295 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
296 return;
297 }
298
299 // Now that we have N in, add anything that uses it if all of their operands
300 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000301 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
302 ComputeTopDownOrdering(*UI, Order, Visited);
303}
304
Chris Lattner1618beb2005-07-29 00:11:56 +0000305
Chris Lattner3e928bb2005-01-07 07:47:09 +0000306void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000307 LastCALLSEQ_END = DAG.getEntryNode();
308 IsLegalizingCall = false;
309
Chris Lattnerab510a72005-10-02 17:49:46 +0000310 // The legalize process is inherently a bottom-up recursive process (users
311 // legalize their uses before themselves). Given infinite stack space, we
312 // could just start legalizing on the root and traverse the whole graph. In
313 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000314 // blocks. To avoid this problem, compute an ordering of the nodes where each
315 // node is only legalized after all of its operands are legalized.
316 std::map<SDNode*, unsigned> Visited;
317 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000318
Chris Lattner32fca002005-10-06 01:20:27 +0000319 // Compute ordering from all of the leaves in the graphs, those (like the
320 // entry node) that have no operands.
321 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
322 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000323 if (I->getNumOperands() == 0) {
324 Visited[I] = 0 - 1U;
325 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000326 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000327 }
328
Chris Lattnerde202b32005-11-09 23:47:37 +0000329 assert(Order.size() == Visited.size() &&
330 Order.size() ==
331 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000332 "Error: DAG is cyclic!");
333 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000334
Chris Lattnerc7029802006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000346 SplitNodes.clear();
347 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000350 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000351}
352
Chris Lattner6831a812006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
385 SDNode *User = *UI;
386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
409 SDNode *Dest) {
410 if (N == Dest) return true; // N certainly leads to Dest :)
411
412 // If the first result of this node has been already legalized, then it cannot
413 // reach N.
414 switch (getTypeAction(N->getValueType(0))) {
415 case Legal:
416 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
417 break;
418 case Promote:
419 if (PromotedNodes.count(SDOperand(N, 0))) return false;
420 break;
421 case Expand:
422 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 }
425
426 // Okay, this node has not already been legalized. Check and legalize all
427 // operands. If none lead to Dest, then we can legalize this node.
428 bool OperandsLeadToDest = false;
429 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
430 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
431 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
432
433 if (OperandsLeadToDest) return true;
434
435 // Okay, this node looks safe, legalize it and return false.
436 switch (getTypeAction(N->getValueType(0))) {
437 case Legal:
438 LegalizeOp(SDOperand(N, 0));
439 break;
440 case Promote:
441 PromoteOp(SDOperand(N, 0));
442 break;
443 case Expand: {
444 SDOperand X, Y;
445 ExpandOp(SDOperand(N, 0), X, Y);
446 break;
447 }
448 }
449 return false;
450}
451
Chris Lattnerc7029802006-03-18 01:44:44 +0000452/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
453/// appropriate for its type.
454void SelectionDAGLegalize::HandleOp(SDOperand Op) {
455 switch (getTypeAction(Op.getValueType())) {
456 default: assert(0 && "Bad type action!");
457 case Legal: LegalizeOp(Op); break;
458 case Promote: PromoteOp(Op); break;
459 case Expand:
460 if (Op.getValueType() != MVT::Vector) {
461 SDOperand X, Y;
462 ExpandOp(Op, X, Y);
463 } else {
464 SDNode *N = Op.Val;
465 unsigned NumOps = N->getNumOperands();
466 unsigned NumElements =
467 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
468 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
469 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
470 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
471 // In the common case, this is a legal vector type, convert it to the
472 // packed operation and type now.
473 PackVectorOp(Op, PackedVT);
474 } else if (NumElements == 1) {
475 // Otherwise, if this is a single element vector, convert it to a
476 // scalar operation.
477 PackVectorOp(Op, EVT);
478 } else {
479 // Otherwise, this is a multiple element vector that isn't supported.
480 // Split it in half and legalize both parts.
481 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000482 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000483 }
484 }
485 break;
486 }
487}
Chris Lattner6831a812006-02-13 09:18:02 +0000488
489
Chris Lattnerc7029802006-03-18 01:44:44 +0000490/// LegalizeOp - We know that the specified value has a legal type.
491/// Recursively ensure that the operands have legal types, then return the
492/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000494 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000495 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000496 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000497
Chris Lattner3e928bb2005-01-07 07:47:09 +0000498 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000499 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000500 if (Node->getNumValues() > 1) {
501 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000502 if (getTypeAction(Node->getValueType(i)) != Legal) {
503 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000504 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000505 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000506 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000507 }
508 }
509
Chris Lattner45982da2005-05-12 16:53:42 +0000510 // Note that LegalizeOp may be reentered even from single-use nodes, which
511 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000512 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
513 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000514
Nate Begeman9373a812005-08-10 20:51:12 +0000515 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000516 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000517 bool isCustom = false;
518
Chris Lattner3e928bb2005-01-07 07:47:09 +0000519 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000520 case ISD::FrameIndex:
521 case ISD::EntryToken:
522 case ISD::Register:
523 case ISD::BasicBlock:
524 case ISD::TargetFrameIndex:
525 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000526 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000527 case ISD::TargetConstantPool:
528 case ISD::TargetGlobalAddress:
529 case ISD::TargetExternalSymbol:
530 case ISD::VALUETYPE:
531 case ISD::SRCVALUE:
532 case ISD::STRING:
533 case ISD::CONDCODE:
534 // Primitives must all be legal.
535 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
536 "This must be legal!");
537 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000538 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000539 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
540 // If this is a target node, legalize it by legalizing the operands then
541 // passing it through.
542 std::vector<SDOperand> Ops;
543 bool Changed = false;
544 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
545 Ops.push_back(LegalizeOp(Node->getOperand(i)));
546 Changed = Changed || Node->getOperand(i) != Ops.back();
547 }
548 if (Changed)
549 if (Node->getNumValues() == 1)
550 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
551 else {
552 std::vector<MVT::ValueType> VTs(Node->value_begin(),
553 Node->value_end());
554 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
555 }
556
557 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
558 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
559 return Result.getValue(Op.ResNo);
560 }
561 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000562 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
563 assert(0 && "Do not know how to legalize this operator!");
564 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000565 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000566 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000567 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000568 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
569 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000570 case TargetLowering::Custom:
571 Tmp1 = TLI.LowerOperation(Op, DAG);
572 if (Tmp1.Val) Result = Tmp1;
573 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000574 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000575 break;
576 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000577 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000578 case ISD::AssertSext:
579 case ISD::AssertZext:
580 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000582 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000583 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000584 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000585 Result = Node->getOperand(Op.ResNo);
586 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000587 case ISD::CopyFromReg:
588 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000589 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000590 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000592 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000593 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000594 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000595 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000596 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
597 } else {
598 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
599 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000600 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
601 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000602 // Since CopyFromReg produces two values, make sure to remember that we
603 // legalized both of them.
604 AddLegalizedOperand(Op.getValue(0), Result);
605 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
606 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000607 case ISD::UNDEF: {
608 MVT::ValueType VT = Op.getValueType();
609 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000610 default: assert(0 && "This action is not supported yet!");
611 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000612 if (MVT::isInteger(VT))
613 Result = DAG.getConstant(0, VT);
614 else if (MVT::isFloatingPoint(VT))
615 Result = DAG.getConstantFP(0, VT);
616 else
617 assert(0 && "Unknown value type!");
618 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000619 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000620 break;
621 }
622 break;
623 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000624
Chris Lattner48b61a72006-03-28 00:40:33 +0000625 case ISD::INTRINSIC_W_CHAIN:
626 case ISD::INTRINSIC_WO_CHAIN:
627 case ISD::INTRINSIC_VOID: {
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000628 std::vector<SDOperand> Ops;
629 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
630 Ops.push_back(LegalizeOp(Node->getOperand(i)));
631 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner10d7fa62006-03-26 09:12:51 +0000632
633 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000634 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000635 TargetLowering::Custom) {
636 Tmp3 = TLI.LowerOperation(Result, DAG);
637 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000638 }
639
640 if (Result.Val->getNumValues() == 1) break;
641
642 // Must have return value and chain result.
643 assert(Result.Val->getNumValues() == 2 &&
644 "Cannot return more than two values!");
645
646 // Since loads produce two values, make sure to remember that we
647 // legalized both of them.
648 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
649 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
650 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000651 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000652
653 case ISD::LOCATION:
654 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
656
657 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
658 case TargetLowering::Promote:
659 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000660 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000661 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000662 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
663 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
664
665 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
666 const std::string &FName =
667 cast<StringSDNode>(Node->getOperand(3))->getValue();
668 const std::string &DirName =
669 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000670 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000671
Jim Laskeye81aecb2005-12-21 20:51:37 +0000672 std::vector<SDOperand> Ops;
673 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000674 SDOperand LineOp = Node->getOperand(1);
675 SDOperand ColOp = Node->getOperand(2);
676
677 if (useDEBUG_LOC) {
678 Ops.push_back(LineOp); // line #
679 Ops.push_back(ColOp); // col #
680 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
681 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
682 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000683 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
684 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000685 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
686 Ops.push_back(DAG.getConstant(ID, MVT::i32));
687 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
688 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000689 } else {
690 Result = Tmp1; // chain
691 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000692 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000693 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000694 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000695 if (Tmp1 != Node->getOperand(0) ||
696 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000697 std::vector<SDOperand> Ops;
698 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000699 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
700 Ops.push_back(Node->getOperand(1)); // line # must be legal.
701 Ops.push_back(Node->getOperand(2)); // col # must be legal.
702 } else {
703 // Otherwise promote them.
704 Ops.push_back(PromoteOp(Node->getOperand(1)));
705 Ops.push_back(PromoteOp(Node->getOperand(2)));
706 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000707 Ops.push_back(Node->getOperand(3)); // filename must be legal.
708 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000709 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000710 }
711 break;
712 }
713 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000714
715 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000716 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000717 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000718 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000719 case TargetLowering::Legal:
720 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
721 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
722 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
723 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000724 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000725 break;
726 }
727 break;
728
729 case ISD::DEBUG_LABEL:
730 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
731 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000732 default: assert(0 && "This action is not supported yet!");
733 case TargetLowering::Legal:
734 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
735 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000736 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000737 break;
738 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000739 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000740
Chris Lattner3e928bb2005-01-07 07:47:09 +0000741 case ISD::Constant:
742 // We know we don't need to expand constants here, constants only have one
743 // value and we check that it is fine above.
744
745 // FIXME: Maybe we should handle things like targets that don't support full
746 // 32-bit immediates?
747 break;
748 case ISD::ConstantFP: {
749 // Spill FP immediates to the constant pool if the target cannot directly
750 // codegen them. Targets often have some immediate values that can be
751 // efficiently generated into an FP register without a load. We explicitly
752 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000753 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
754
755 // Check to see if this FP immediate is already legal.
756 bool isLegal = false;
757 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
758 E = TLI.legal_fpimm_end(); I != E; ++I)
759 if (CFP->isExactlyValue(*I)) {
760 isLegal = true;
761 break;
762 }
763
Chris Lattner3181a772006-01-29 06:26:56 +0000764 // If this is a legal constant, turn it into a TargetConstantFP node.
765 if (isLegal) {
766 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
767 break;
768 }
769
770 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
771 default: assert(0 && "This action is not supported yet!");
772 case TargetLowering::Custom:
773 Tmp3 = TLI.LowerOperation(Result, DAG);
774 if (Tmp3.Val) {
775 Result = Tmp3;
776 break;
777 }
778 // FALLTHROUGH
779 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000780 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000781 bool Extend = false;
782
Chris Lattner456a93a2006-01-28 07:39:30 +0000783 // If a FP immediate is precise when represented as a float and if the
784 // target can do an extending load from float to double, we put it into
785 // the constant pool as a float, even if it's is statically typed as a
786 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000787 MVT::ValueType VT = CFP->getValueType(0);
788 bool isDouble = VT == MVT::f64;
789 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
790 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000791 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
792 // Only do this if the target has a native EXTLOAD instruction from
793 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000794 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000795 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
796 VT = MVT::f32;
797 Extend = true;
798 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000799
Chris Lattner456a93a2006-01-28 07:39:30 +0000800 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000801 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000802 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
803 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000804 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000805 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
806 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000807 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000808 }
809 break;
810 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000811 case ISD::TokenFactor:
812 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000813 Tmp1 = LegalizeOp(Node->getOperand(0));
814 Tmp2 = LegalizeOp(Node->getOperand(1));
815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
816 } else if (Node->getNumOperands() == 3) {
817 Tmp1 = LegalizeOp(Node->getOperand(0));
818 Tmp2 = LegalizeOp(Node->getOperand(1));
819 Tmp3 = LegalizeOp(Node->getOperand(2));
820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000821 } else {
822 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000823 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000824 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
825 Ops.push_back(LegalizeOp(Node->getOperand(i)));
826 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000827 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000828 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000829
Chris Lattnerb2827b02006-03-19 00:52:58 +0000830 case ISD::BUILD_VECTOR:
831 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000832 default: assert(0 && "This action is not supported yet!");
833 case TargetLowering::Custom:
834 Tmp3 = TLI.LowerOperation(Result, DAG);
835 if (Tmp3.Val) {
836 Result = Tmp3;
837 break;
838 }
839 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000840 case TargetLowering::Expand:
841 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000842 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000843 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000844 break;
845 case ISD::INSERT_VECTOR_ELT:
846 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
847 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
848 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
849 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
850
851 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
852 Node->getValueType(0))) {
853 default: assert(0 && "This action is not supported yet!");
854 case TargetLowering::Legal:
855 break;
856 case TargetLowering::Custom:
857 Tmp3 = TLI.LowerOperation(Result, DAG);
858 if (Tmp3.Val) {
859 Result = Tmp3;
860 break;
861 }
862 // FALLTHROUGH
863 case TargetLowering::Expand: {
864 // If the target doesn't support this, we have to spill the input vector
865 // to a temporary stack slot, update the element, then reload it. This is
866 // badness. We could also load the value into a vector register (either
867 // with a "move to register" or "extload into register" instruction, then
868 // permute it into place, if the idx is a constant and if the idx is
869 // supported by the target.
Evan Chengeb0b4612006-03-31 01:27:51 +0000870 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
871 // Store the vector.
872 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
873 Tmp1, StackPtr, DAG.getSrcValue(NULL));
874
875 // Truncate or zero extend offset to target pointer type.
876 MVT::ValueType IntPtr = TLI.getPointerTy();
877 if (Tmp3.getValueType() > IntPtr)
878 Tmp3 = DAG.getNode(ISD::TRUNCATE, IntPtr, Tmp3);
879 else
880 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Tmp3);
881
882 // Add the offset to the index.
883 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
884 Tmp3 = DAG.getNode(ISD::MUL, Tmp3.getValueType(), Tmp3,
885 DAG.getConstant(EltSize, Tmp3.getValueType()));
886 SDOperand StackPtr2 =
887 DAG.getNode(ISD::ADD, Tmp3.getValueType(), Tmp3, StackPtr);
888 // Store the scalar value.
889 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
890 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
891 // Load the updated vector.
892 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
893 DAG.getSrcValue(NULL));
Chris Lattner2332b9f2006-03-19 01:17:20 +0000894 break;
895 }
896 }
897 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000898 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000899 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
900 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
901 break;
902 }
903
Chris Lattnerce872152006-03-19 06:31:19 +0000904 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
905 Result = DAG.UpdateNodeOperands(Result, Tmp1);
906 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
907 Node->getValueType(0))) {
908 default: assert(0 && "This action is not supported yet!");
909 case TargetLowering::Legal:
910 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000911 case TargetLowering::Custom:
912 Tmp3 = TLI.LowerOperation(Result, DAG);
913 if (Tmp3.Val) {
914 Result = Tmp3;
915 break;
916 }
917 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000918 case TargetLowering::Expand:
919 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +0000920 break;
921 }
Chris Lattnerce872152006-03-19 06:31:19 +0000922 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000923 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +0000924 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
925 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
926 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
927
928 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +0000929 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
930 default: assert(0 && "Unknown operation action!");
931 case TargetLowering::Legal:
932 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
933 "vector shuffle should not be created if not legal!");
934 break;
935 case TargetLowering::Custom:
Chris Lattner87100e02006-03-20 01:52:29 +0000936 Tmp1 = TLI.LowerOperation(Result, DAG);
937 if (Tmp1.Val) Result = Tmp1;
Chris Lattner4352cc92006-04-04 17:23:26 +0000938 break;
939 case TargetLowering::Promote: {
940 // Change base type to a different vector type.
941 MVT::ValueType OVT = Node->getValueType(0);
942 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
943
944 // Cast the two input vectors.
945 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
946 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
947
948 // Convert the shuffle mask to the right # elements.
949 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
950 assert(Tmp3.Val && "Shuffle not legal?");
951 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
952 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
953 break;
954 }
Chris Lattner87100e02006-03-20 01:52:29 +0000955 }
956 break;
Chris Lattner384504c2006-03-21 20:44:12 +0000957
958 case ISD::EXTRACT_VECTOR_ELT:
959 Tmp1 = LegalizeOp(Node->getOperand(0));
960 Tmp2 = LegalizeOp(Node->getOperand(1));
961 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +0000962
963 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
964 Tmp1.getValueType())) {
965 default: assert(0 && "This action is not supported yet!");
966 case TargetLowering::Legal:
967 break;
968 case TargetLowering::Custom:
969 Tmp3 = TLI.LowerOperation(Result, DAG);
970 if (Tmp3.Val) {
971 Result = Tmp3;
972 break;
973 }
974 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +0000975 case TargetLowering::Expand:
976 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +0000977 break;
978 }
Chris Lattner384504c2006-03-21 20:44:12 +0000979 break;
980
Chris Lattner15972212006-03-31 17:55:51 +0000981 case ISD::VEXTRACT_VECTOR_ELT:
982 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +0000983 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000984
Chris Lattner6831a812006-02-13 09:18:02 +0000985 case ISD::CALLSEQ_START: {
986 SDNode *CallEnd = FindCallEndFromCallStart(Node);
987
988 // Recursively Legalize all of the inputs of the call end that do not lead
989 // to this call start. This ensures that any libcalls that need be inserted
990 // are inserted *before* the CALLSEQ_START.
991 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
992 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
993
994 // Now that we legalized all of the inputs (which may have inserted
995 // libcalls) create the new CALLSEQ_START node.
996 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
997
998 // Merge in the last call, to ensure that this call start after the last
999 // call ended.
1000 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1001 Tmp1 = LegalizeOp(Tmp1);
1002
1003 // Do not try to legalize the target-specific arguments (#1+).
1004 if (Tmp1 != Node->getOperand(0)) {
1005 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1006 Ops[0] = Tmp1;
1007 Result = DAG.UpdateNodeOperands(Result, Ops);
1008 }
1009
1010 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001011 AddLegalizedOperand(Op.getValue(0), Result);
1012 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1013 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1014
Chris Lattner6831a812006-02-13 09:18:02 +00001015 // Now that the callseq_start and all of the non-call nodes above this call
1016 // sequence have been legalized, legalize the call itself. During this
1017 // process, no libcalls can/will be inserted, guaranteeing that no calls
1018 // can overlap.
1019 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1020 SDOperand InCallSEQ = LastCALLSEQ_END;
1021 // Note that we are selecting this call!
1022 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1023 IsLegalizingCall = true;
1024
1025 // Legalize the call, starting from the CALLSEQ_END.
1026 LegalizeOp(LastCALLSEQ_END);
1027 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1028 return Result;
1029 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001030 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001031 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1032 // will cause this node to be legalized as well as handling libcalls right.
1033 if (LastCALLSEQ_END.Val != Node) {
1034 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1035 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1036 assert(I != LegalizedNodes.end() &&
1037 "Legalizing the call start should have legalized this node!");
1038 return I->second;
1039 }
1040
1041 // Otherwise, the call start has been legalized and everything is going
1042 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001044 // Do not try to legalize the target-specific arguments (#1+), except for
1045 // an optional flag input.
1046 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1047 if (Tmp1 != Node->getOperand(0)) {
1048 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1049 Ops[0] = Tmp1;
1050 Result = DAG.UpdateNodeOperands(Result, Ops);
1051 }
1052 } else {
1053 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1054 if (Tmp1 != Node->getOperand(0) ||
1055 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1056 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1057 Ops[0] = Tmp1;
1058 Ops.back() = Tmp2;
1059 Result = DAG.UpdateNodeOperands(Result, Ops);
1060 }
Chris Lattner6a542892006-01-24 05:48:21 +00001061 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001062 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001063 // This finishes up call legalization.
1064 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001065
1066 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1067 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1068 if (Node->getNumValues() == 2)
1069 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1070 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001071 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001072 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1073 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1074 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001076
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001077 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001078 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001079 switch (TLI.getOperationAction(Node->getOpcode(),
1080 Node->getValueType(0))) {
1081 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001082 case TargetLowering::Expand: {
1083 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1084 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1085 " not tell us which reg is the stack pointer!");
1086 SDOperand Chain = Tmp1.getOperand(0);
1087 SDOperand Size = Tmp2.getOperand(1);
1088 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1089 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1090 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001091 Tmp1 = LegalizeOp(Tmp1);
1092 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001093 break;
1094 }
1095 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001096 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1097 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001098 Tmp1 = LegalizeOp(Tmp3);
1099 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001100 }
Chris Lattner903d2782006-01-15 08:54:32 +00001101 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001102 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001103 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001104 }
Chris Lattner903d2782006-01-15 08:54:32 +00001105 // Since this op produce two values, make sure to remember that we
1106 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001107 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1108 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001109 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001110 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001111 case ISD::INLINEASM:
1112 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1113 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001114 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +00001115 Tmp2 = Tmp3 = SDOperand(0, 0);
1116 else
1117 Tmp3 = LegalizeOp(Tmp2);
1118
1119 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1120 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1121 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001122 if (Tmp3.Val) Ops.back() = Tmp3;
1123 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001124 }
1125
1126 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001127 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001128 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1129 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001130 case ISD::BR:
1131 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001132 // Ensure that libcalls are emitted before a branch.
1133 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1134 Tmp1 = LegalizeOp(Tmp1);
1135 LastCALLSEQ_END = DAG.getEntryNode();
1136
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001138 break;
1139
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001140 case ISD::BRCOND:
1141 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001142 // Ensure that libcalls are emitted before a return.
1143 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1144 Tmp1 = LegalizeOp(Tmp1);
1145 LastCALLSEQ_END = DAG.getEntryNode();
1146
Chris Lattner47e92232005-01-18 19:27:06 +00001147 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1148 case Expand: assert(0 && "It's impossible to expand bools");
1149 case Legal:
1150 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1151 break;
1152 case Promote:
1153 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1154 break;
1155 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001156
1157 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001159
1160 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1161 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001162 case TargetLowering::Legal: break;
1163 case TargetLowering::Custom:
1164 Tmp1 = TLI.LowerOperation(Result, DAG);
1165 if (Tmp1.Val) Result = Tmp1;
1166 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001167 case TargetLowering::Expand:
1168 // Expand brcond's setcc into its constituent parts and create a BR_CC
1169 // Node.
1170 if (Tmp2.getOpcode() == ISD::SETCC) {
1171 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1172 Tmp2.getOperand(0), Tmp2.getOperand(1),
1173 Node->getOperand(2));
1174 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001175 // Make sure the condition is either zero or one. It may have been
1176 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001177 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1178 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1179 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001180
Nate Begeman7cbd5252005-08-16 19:49:35 +00001181 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1182 DAG.getCondCode(ISD::SETNE), Tmp2,
1183 DAG.getConstant(0, Tmp2.getValueType()),
1184 Node->getOperand(2));
1185 }
1186 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001187 }
1188 break;
1189 case ISD::BR_CC:
1190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001191 // Ensure that libcalls are emitted before a branch.
1192 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1193 Tmp1 = LegalizeOp(Tmp1);
1194 LastCALLSEQ_END = DAG.getEntryNode();
1195
Nate Begeman750ac1b2006-02-01 07:19:44 +00001196 Tmp2 = Node->getOperand(2); // LHS
1197 Tmp3 = Node->getOperand(3); // RHS
1198 Tmp4 = Node->getOperand(1); // CC
1199
1200 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1201
1202 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1203 // the LHS is a legal SETCC itself. In this case, we need to compare
1204 // the result against zero to select between true and false values.
1205 if (Tmp3.Val == 0) {
1206 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1207 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001208 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001209
1210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1211 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001212
Chris Lattner181b7a32005-12-17 23:46:46 +00001213 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1214 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001215 case TargetLowering::Legal: break;
1216 case TargetLowering::Custom:
1217 Tmp4 = TLI.LowerOperation(Result, DAG);
1218 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001219 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001220 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001221 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001222 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001223 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1224 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001225
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001226 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001227 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1228 Tmp2 = Result.getValue(0);
1229 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001230
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001231 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1232 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001233 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001234 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001235 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001236 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001237 Tmp2 = LegalizeOp(Tmp1);
1238 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001239 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001240 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001241 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001242 // Since loads produce two values, make sure to remember that we
1243 // legalized both of them.
1244 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1245 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1246 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001247 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001248 case ISD::EXTLOAD:
1249 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001250 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001251 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1252 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001253
Chris Lattner5f056bf2005-07-10 01:55:33 +00001254 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001255 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001256 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001257 case TargetLowering::Promote:
1258 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001259 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1260 DAG.getValueType(MVT::i8));
1261 Tmp1 = Result.getValue(0);
1262 Tmp2 = Result.getValue(1);
1263 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001264 case TargetLowering::Custom:
1265 isCustom = true;
1266 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001267 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1269 Node->getOperand(3));
1270 Tmp1 = Result.getValue(0);
1271 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001272
1273 if (isCustom) {
1274 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1275 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001276 Tmp1 = LegalizeOp(Tmp3);
1277 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001278 }
1279 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001280 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001281 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001282 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001283 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1284 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001285 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001286 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1287 Tmp2 = LegalizeOp(Load.getValue(1));
1288 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001289 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001290 assert(Node->getOpcode() != ISD::EXTLOAD &&
1291 "EXTLOAD should always be supported!");
1292 // Turn the unsupported load into an EXTLOAD followed by an explicit
1293 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001294 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1295 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001296 SDOperand ValRes;
1297 if (Node->getOpcode() == ISD::SEXTLOAD)
1298 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001299 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001300 else
1301 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001302 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1303 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1304 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001305 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001306 // Since loads produce two values, make sure to remember that we legalized
1307 // both of them.
1308 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1309 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1310 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001311 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001312 case ISD::EXTRACT_ELEMENT: {
1313 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1314 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001315 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001316 case Legal:
1317 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1318 // 1 -> Hi
1319 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1320 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1321 TLI.getShiftAmountTy()));
1322 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1323 } else {
1324 // 0 -> Lo
1325 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1326 Node->getOperand(0));
1327 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001328 break;
1329 case Expand:
1330 // Get both the low and high parts.
1331 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1332 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1333 Result = Tmp2; // 1 -> Hi
1334 else
1335 Result = Tmp1; // 0 -> Lo
1336 break;
1337 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001338 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001339 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001340
1341 case ISD::CopyToReg:
1342 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001343
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001344 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001345 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001346 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001347 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001348 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001349 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001350 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001351 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001352 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001353 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1355 Tmp3);
1356 } else {
1357 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001358 }
1359
1360 // Since this produces two values, make sure to remember that we legalized
1361 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001362 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001363 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001364 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001365 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001366 break;
1367
1368 case ISD::RET:
1369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001370
1371 // Ensure that libcalls are emitted before a return.
1372 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1373 Tmp1 = LegalizeOp(Tmp1);
1374 LastCALLSEQ_END = DAG.getEntryNode();
1375
Chris Lattner3e928bb2005-01-07 07:47:09 +00001376 switch (Node->getNumOperands()) {
1377 case 2: // ret val
1378 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1379 case Legal:
1380 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001382 break;
1383 case Expand: {
1384 SDOperand Lo, Hi;
1385 ExpandOp(Node->getOperand(1), Lo, Hi);
1386 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001387 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001388 }
1389 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001390 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001391 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1392 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001393 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001394 }
1395 break;
1396 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001397 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001398 break;
1399 default: { // ret <values>
1400 std::vector<SDOperand> NewValues;
1401 NewValues.push_back(Tmp1);
1402 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1403 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1404 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001405 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001406 break;
1407 case Expand: {
1408 SDOperand Lo, Hi;
1409 ExpandOp(Node->getOperand(i), Lo, Hi);
1410 NewValues.push_back(Lo);
1411 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001412 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001413 }
1414 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001415 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001416 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001417
1418 if (NewValues.size() == Node->getNumOperands())
1419 Result = DAG.UpdateNodeOperands(Result, NewValues);
1420 else
1421 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001422 break;
1423 }
1424 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001425
Chris Lattner6862dbc2006-01-29 21:02:23 +00001426 if (Result.getOpcode() == ISD::RET) {
1427 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1428 default: assert(0 && "This action is not supported yet!");
1429 case TargetLowering::Legal: break;
1430 case TargetLowering::Custom:
1431 Tmp1 = TLI.LowerOperation(Result, DAG);
1432 if (Tmp1.Val) Result = Tmp1;
1433 break;
1434 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001435 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001436 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001437 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001438 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1439 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1440
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001441 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001442 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001443 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001444 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001445 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001446 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001447 } else {
1448 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001449 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001450 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001451 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1452 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001453 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001454 }
1455
Chris Lattner3e928bb2005-01-07 07:47:09 +00001456 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1457 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001458 Tmp3 = LegalizeOp(Node->getOperand(1));
1459 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1460 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001461
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001462 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001463 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1464 default: assert(0 && "This action is not supported yet!");
1465 case TargetLowering::Legal: break;
1466 case TargetLowering::Custom:
1467 Tmp1 = TLI.LowerOperation(Result, DAG);
1468 if (Tmp1.Val) Result = Tmp1;
1469 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001470 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001471 break;
1472 }
1473 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001474 // Truncate the value and store the result.
1475 Tmp3 = PromoteOp(Node->getOperand(1));
1476 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001477 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001478 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001479 break;
1480
Chris Lattner3e928bb2005-01-07 07:47:09 +00001481 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001482 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001483 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001484
1485 // If this is a vector type, then we have to calculate the increment as
1486 // the product of the element size in bytes, and the number of elements
1487 // in the high half of the vector.
1488 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1489 SDNode *InVal = Node->getOperand(1).Val;
1490 unsigned NumElems =
1491 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1492 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1493
1494 // Figure out if there is a Packed type corresponding to this Vector
1495 // type. If so, convert to the packed type.
1496 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1497 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1498 // Turn this into a normal store of the packed type.
1499 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1501 Node->getOperand(3));
1502 break;
1503 } else if (NumElems == 1) {
1504 // Turn this into a normal store of the scalar type.
1505 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1506 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1507 Node->getOperand(3));
Chris Lattner2ae2e982006-03-31 17:37:22 +00001508 // The scalarized value type may not be legal, e.g. it might require
1509 // promotion or expansion. Relegalize the scalar store.
1510 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001511 break;
1512 } else {
1513 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1514 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1515 }
1516 } else {
1517 ExpandOp(Node->getOperand(1), Lo, Hi);
1518 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001519
Chris Lattnerd9731af2006-03-31 18:20:46 +00001520 if (!TLI.isLittleEndian())
1521 std::swap(Lo, Hi);
1522 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001523
Chris Lattneredb1add2005-05-11 04:51:16 +00001524 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1525 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001526 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1527 getIntPtrConstant(IncrementSize));
1528 assert(isTypeLegal(Tmp2.getValueType()) &&
1529 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001530 // FIXME: This sets the srcvalue of both halves to be the same, which is
1531 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001532 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1533 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001534 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1535 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001536 }
1537 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001538 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001539 case ISD::PCMARKER:
1540 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001541 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001542 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001543 case ISD::STACKSAVE:
1544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001545 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1546 Tmp1 = Result.getValue(0);
1547 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001548
Chris Lattner140d53c2006-01-13 02:50:02 +00001549 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1550 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001551 case TargetLowering::Legal: break;
1552 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001553 Tmp3 = TLI.LowerOperation(Result, DAG);
1554 if (Tmp3.Val) {
1555 Tmp1 = LegalizeOp(Tmp3);
1556 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001557 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001558 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001559 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001560 // Expand to CopyFromReg if the target set
1561 // StackPointerRegisterToSaveRestore.
1562 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001563 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001564 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001565 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001566 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001567 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1568 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001569 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001570 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001571 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001572
1573 // Since stacksave produce two values, make sure to remember that we
1574 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001575 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1576 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1577 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001578
Chris Lattner140d53c2006-01-13 02:50:02 +00001579 case ISD::STACKRESTORE:
1580 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1581 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001582 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001583
1584 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1585 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001586 case TargetLowering::Legal: break;
1587 case TargetLowering::Custom:
1588 Tmp1 = TLI.LowerOperation(Result, DAG);
1589 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001590 break;
1591 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001592 // Expand to CopyToReg if the target set
1593 // StackPointerRegisterToSaveRestore.
1594 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1595 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1596 } else {
1597 Result = Tmp1;
1598 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001599 break;
1600 }
1601 break;
1602
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001603 case ISD::READCYCLECOUNTER:
1604 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001605 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001606
1607 // Since rdcc produce two values, make sure to remember that we legalized
1608 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001609 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001610 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001611 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001612
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001613 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001614 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1615 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1616
Chris Lattner456a93a2006-01-28 07:39:30 +00001617 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1618 "Cannot handle illegal TRUNCSTORE yet!");
1619 Tmp2 = LegalizeOp(Node->getOperand(1));
1620
1621 // The only promote case we handle is TRUNCSTORE:i1 X into
1622 // -> TRUNCSTORE:i8 (and X, 1)
1623 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1624 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1625 TargetLowering::Promote) {
1626 // Promote the bool to a mask then store.
1627 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1628 DAG.getConstant(1, Tmp2.getValueType()));
1629 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1630 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001631
Chris Lattner456a93a2006-01-28 07:39:30 +00001632 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1633 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001634 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1635 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001636 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001637
Chris Lattner456a93a2006-01-28 07:39:30 +00001638 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1639 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1640 default: assert(0 && "This action is not supported yet!");
1641 case TargetLowering::Legal: break;
1642 case TargetLowering::Custom:
1643 Tmp1 = TLI.LowerOperation(Result, DAG);
1644 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001645 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001646 }
1647 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001648 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001649 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001650 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1651 case Expand: assert(0 && "It's impossible to expand bools");
1652 case Legal:
1653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1654 break;
1655 case Promote:
1656 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1657 break;
1658 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001659 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001660 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001661
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001662 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001663
Nate Begemanb942a3d2005-08-23 04:29:48 +00001664 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001665 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001666 case TargetLowering::Legal: break;
1667 case TargetLowering::Custom: {
1668 Tmp1 = TLI.LowerOperation(Result, DAG);
1669 if (Tmp1.Val) Result = Tmp1;
1670 break;
1671 }
Nate Begeman9373a812005-08-10 20:51:12 +00001672 case TargetLowering::Expand:
1673 if (Tmp1.getOpcode() == ISD::SETCC) {
1674 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1675 Tmp2, Tmp3,
1676 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1677 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001678 // Make sure the condition is either zero or one. It may have been
1679 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001680 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1681 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1682 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001683 Result = DAG.getSelectCC(Tmp1,
1684 DAG.getConstant(0, Tmp1.getValueType()),
1685 Tmp2, Tmp3, ISD::SETNE);
1686 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001687 break;
1688 case TargetLowering::Promote: {
1689 MVT::ValueType NVT =
1690 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1691 unsigned ExtOp, TruncOp;
1692 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001693 ExtOp = ISD::ANY_EXTEND;
1694 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001695 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001696 ExtOp = ISD::FP_EXTEND;
1697 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001698 }
1699 // Promote each of the values to the new type.
1700 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1701 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1702 // Perform the larger operation, then round down.
1703 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1704 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1705 break;
1706 }
1707 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001708 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001709 case ISD::SELECT_CC: {
1710 Tmp1 = Node->getOperand(0); // LHS
1711 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001712 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1713 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001714 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001715
Nate Begeman750ac1b2006-02-01 07:19:44 +00001716 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1717
1718 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1719 // the LHS is a legal SETCC itself. In this case, we need to compare
1720 // the result against zero to select between true and false values.
1721 if (Tmp2.Val == 0) {
1722 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1723 CC = DAG.getCondCode(ISD::SETNE);
1724 }
1725 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1726
1727 // Everything is legal, see if we should expand this op or something.
1728 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1729 default: assert(0 && "This action is not supported yet!");
1730 case TargetLowering::Legal: break;
1731 case TargetLowering::Custom:
1732 Tmp1 = TLI.LowerOperation(Result, DAG);
1733 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001734 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001735 }
1736 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001737 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001738 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001739 Tmp1 = Node->getOperand(0);
1740 Tmp2 = Node->getOperand(1);
1741 Tmp3 = Node->getOperand(2);
1742 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1743
1744 // If we had to Expand the SetCC operands into a SELECT node, then it may
1745 // not always be possible to return a true LHS & RHS. In this case, just
1746 // return the value we legalized, returned in the LHS
1747 if (Tmp2.Val == 0) {
1748 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001749 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001750 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001751
Chris Lattner73e142f2006-01-30 22:43:50 +00001752 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001753 default: assert(0 && "Cannot handle this action for SETCC yet!");
1754 case TargetLowering::Custom:
1755 isCustom = true;
1756 // FALLTHROUGH.
1757 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001758 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001759 if (isCustom) {
1760 Tmp3 = TLI.LowerOperation(Result, DAG);
1761 if (Tmp3.Val) Result = Tmp3;
1762 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001763 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001764 case TargetLowering::Promote: {
1765 // First step, figure out the appropriate operation to use.
1766 // Allow SETCC to not be supported for all legal data types
1767 // Mostly this targets FP
1768 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1769 MVT::ValueType OldVT = NewInTy;
1770
1771 // Scan for the appropriate larger type to use.
1772 while (1) {
1773 NewInTy = (MVT::ValueType)(NewInTy+1);
1774
1775 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1776 "Fell off of the edge of the integer world");
1777 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1778 "Fell off of the edge of the floating point world");
1779
1780 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001781 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001782 break;
1783 }
1784 if (MVT::isInteger(NewInTy))
1785 assert(0 && "Cannot promote Legal Integer SETCC yet");
1786 else {
1787 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1788 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1789 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001790 Tmp1 = LegalizeOp(Tmp1);
1791 Tmp2 = LegalizeOp(Tmp2);
1792 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001793 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001794 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001795 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001796 case TargetLowering::Expand:
1797 // Expand a setcc node into a select_cc of the same condition, lhs, and
1798 // rhs that selects between const 1 (true) and const 0 (false).
1799 MVT::ValueType VT = Node->getValueType(0);
1800 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1801 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1802 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001803 break;
1804 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001805 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001806 case ISD::MEMSET:
1807 case ISD::MEMCPY:
1808 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001809 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001810 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1811
1812 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1813 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1814 case Expand: assert(0 && "Cannot expand a byte!");
1815 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001816 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001817 break;
1818 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001819 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001820 break;
1821 }
1822 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001823 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001824 }
Chris Lattner272455b2005-02-02 03:44:41 +00001825
1826 SDOperand Tmp4;
1827 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001828 case Expand: {
1829 // Length is too big, just take the lo-part of the length.
1830 SDOperand HiPart;
1831 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1832 break;
1833 }
Chris Lattnere5605212005-01-28 22:29:18 +00001834 case Legal:
1835 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001836 break;
1837 case Promote:
1838 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001839 break;
1840 }
1841
1842 SDOperand Tmp5;
1843 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1844 case Expand: assert(0 && "Cannot expand this yet!");
1845 case Legal:
1846 Tmp5 = LegalizeOp(Node->getOperand(4));
1847 break;
1848 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001849 Tmp5 = PromoteOp(Node->getOperand(4));
1850 break;
1851 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001852
1853 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1854 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001855 case TargetLowering::Custom:
1856 isCustom = true;
1857 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001858 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001860 if (isCustom) {
1861 Tmp1 = TLI.LowerOperation(Result, DAG);
1862 if (Tmp1.Val) Result = Tmp1;
1863 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001864 break;
1865 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001866 // Otherwise, the target does not support this operation. Lower the
1867 // operation to an explicit libcall as appropriate.
1868 MVT::ValueType IntPtr = TLI.getPointerTy();
1869 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1870 std::vector<std::pair<SDOperand, const Type*> > Args;
1871
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001872 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001873 if (Node->getOpcode() == ISD::MEMSET) {
1874 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001875 // Extend the (previously legalized) ubyte argument to be an int value
1876 // for the call.
1877 if (Tmp3.getValueType() > MVT::i32)
1878 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1879 else
1880 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001881 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1882 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1883
1884 FnName = "memset";
1885 } else if (Node->getOpcode() == ISD::MEMCPY ||
1886 Node->getOpcode() == ISD::MEMMOVE) {
1887 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1888 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1889 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1890 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1891 } else {
1892 assert(0 && "Unknown op!");
1893 }
Chris Lattner45982da2005-05-12 16:53:42 +00001894
Chris Lattnere1bd8222005-01-11 05:57:22 +00001895 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001896 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001897 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001898 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001899 break;
1900 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001901 }
1902 break;
1903 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001904
Chris Lattner5b359c62005-04-02 04:00:59 +00001905 case ISD::SHL_PARTS:
1906 case ISD::SRA_PARTS:
1907 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001908 std::vector<SDOperand> Ops;
1909 bool Changed = false;
1910 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1911 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1912 Changed |= Ops.back() != Node->getOperand(i);
1913 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001914 if (Changed)
1915 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001916
Evan Cheng05a2d562006-01-09 18:31:59 +00001917 switch (TLI.getOperationAction(Node->getOpcode(),
1918 Node->getValueType(0))) {
1919 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001920 case TargetLowering::Legal: break;
1921 case TargetLowering::Custom:
1922 Tmp1 = TLI.LowerOperation(Result, DAG);
1923 if (Tmp1.Val) {
1924 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001925 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001926 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001927 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1928 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001929 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001930 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001931 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001932 return RetVal;
1933 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001934 break;
1935 }
1936
Chris Lattner2c8086f2005-04-02 05:00:07 +00001937 // Since these produce multiple values, make sure to remember that we
1938 // legalized all of them.
1939 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1940 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1941 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001942 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001943
1944 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001945 case ISD::ADD:
1946 case ISD::SUB:
1947 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001948 case ISD::MULHS:
1949 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001950 case ISD::UDIV:
1951 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001952 case ISD::AND:
1953 case ISD::OR:
1954 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001955 case ISD::SHL:
1956 case ISD::SRL:
1957 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001958 case ISD::FADD:
1959 case ISD::FSUB:
1960 case ISD::FMUL:
1961 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001962 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001963 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1964 case Expand: assert(0 && "Not possible");
1965 case Legal:
1966 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1967 break;
1968 case Promote:
1969 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1970 break;
1971 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001972
1973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001974
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001975 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00001976 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00001977 case TargetLowering::Legal: break;
1978 case TargetLowering::Custom:
1979 Tmp1 = TLI.LowerOperation(Result, DAG);
1980 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001981 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00001982 case TargetLowering::Expand: {
1983 assert(MVT::isVector(Node->getValueType(0)) &&
1984 "Cannot expand this binary operator!");
1985 // Expand the operation into a bunch of nasty scalar code.
1986 std::vector<SDOperand> Ops;
1987 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
1988 MVT::ValueType PtrVT = TLI.getPointerTy();
1989 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
1990 i != e; ++i) {
1991 SDOperand Idx = DAG.getConstant(i, PtrVT);
1992 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
1993 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
1994 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
1995 }
1996 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
1997 break;
1998 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001999 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002000 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002001
2002 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2003 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2004 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2005 case Expand: assert(0 && "Not possible");
2006 case Legal:
2007 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2008 break;
2009 case Promote:
2010 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2011 break;
2012 }
2013
2014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2015
2016 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2017 default: assert(0 && "Operation not supported");
2018 case TargetLowering::Custom:
2019 Tmp1 = TLI.LowerOperation(Result, DAG);
2020 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002021 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002022 case TargetLowering::Legal: break;
2023 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00002024 // If this target supports fabs/fneg natively, do this efficiently.
2025 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2026 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2027 // Get the sign bit of the RHS.
2028 MVT::ValueType IVT =
2029 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2030 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2031 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2032 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2033 // Get the absolute value of the result.
2034 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2035 // Select between the nabs and abs value based on the sign bit of
2036 // the input.
2037 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2038 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2039 AbsVal),
2040 AbsVal);
2041 Result = LegalizeOp(Result);
2042 break;
2043 }
2044
2045 // Otherwise, do bitwise ops!
2046
2047 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00002048 const char *FnName;
2049 if (Node->getValueType(0) == MVT::f32) {
2050 FnName = "copysignf";
2051 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2052 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2053 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2054 } else {
2055 FnName = "copysign";
2056 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2057 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2058 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2059 }
2060 SDOperand Dummy;
2061 Result = ExpandLibCall(FnName, Node, Dummy);
2062 break;
2063 }
2064 break;
2065
Nate Begeman551bf3f2006-02-17 05:43:56 +00002066 case ISD::ADDC:
2067 case ISD::SUBC:
2068 Tmp1 = LegalizeOp(Node->getOperand(0));
2069 Tmp2 = LegalizeOp(Node->getOperand(1));
2070 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2071 // Since this produces two values, make sure to remember that we legalized
2072 // both of them.
2073 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2074 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2075 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002076
Nate Begeman551bf3f2006-02-17 05:43:56 +00002077 case ISD::ADDE:
2078 case ISD::SUBE:
2079 Tmp1 = LegalizeOp(Node->getOperand(0));
2080 Tmp2 = LegalizeOp(Node->getOperand(1));
2081 Tmp3 = LegalizeOp(Node->getOperand(2));
2082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2083 // Since this produces two values, make sure to remember that we legalized
2084 // both of them.
2085 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2086 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2087 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002088
Nate Begeman419f8b62005-10-18 00:27:41 +00002089 case ISD::BUILD_PAIR: {
2090 MVT::ValueType PairTy = Node->getValueType(0);
2091 // TODO: handle the case where the Lo and Hi operands are not of legal type
2092 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2093 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2094 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002095 case TargetLowering::Promote:
2096 case TargetLowering::Custom:
2097 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002098 case TargetLowering::Legal:
2099 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2100 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2101 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002102 case TargetLowering::Expand:
2103 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2104 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2105 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2106 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2107 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002108 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002109 break;
2110 }
2111 break;
2112 }
2113
Nate Begemanc105e192005-04-06 00:23:54 +00002114 case ISD::UREM:
2115 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002116 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002117 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2118 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002119
Nate Begemanc105e192005-04-06 00:23:54 +00002120 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002121 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2122 case TargetLowering::Custom:
2123 isCustom = true;
2124 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002125 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002127 if (isCustom) {
2128 Tmp1 = TLI.LowerOperation(Result, DAG);
2129 if (Tmp1.Val) Result = Tmp1;
2130 }
Nate Begemanc105e192005-04-06 00:23:54 +00002131 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002132 case TargetLowering::Expand:
2133 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002134 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002135 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002136 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002137 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2138 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2139 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2140 } else {
2141 // Floating point mod -> fmod libcall.
2142 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2143 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002144 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002145 }
2146 break;
2147 }
2148 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002149 case ISD::VAARG: {
2150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2151 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2152
Chris Lattner5c62f332006-01-28 07:42:08 +00002153 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002154 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2155 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002156 case TargetLowering::Custom:
2157 isCustom = true;
2158 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002159 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002160 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2161 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002162 Tmp1 = Result.getValue(1);
2163
2164 if (isCustom) {
2165 Tmp2 = TLI.LowerOperation(Result, DAG);
2166 if (Tmp2.Val) {
2167 Result = LegalizeOp(Tmp2);
2168 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2169 }
2170 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002171 break;
2172 case TargetLowering::Expand: {
2173 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2174 Node->getOperand(2));
2175 // Increment the pointer, VAList, to the next vaarg
2176 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2177 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2178 TLI.getPointerTy()));
2179 // Store the incremented VAList to the legalized pointer
2180 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2181 Node->getOperand(2));
2182 // Load the actual argument out of the pointer VAList
2183 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002184 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002185 Result = LegalizeOp(Result);
2186 break;
2187 }
2188 }
2189 // Since VAARG produces two values, make sure to remember that we
2190 // legalized both of them.
2191 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002192 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2193 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002194 }
2195
2196 case ISD::VACOPY:
2197 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2198 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2199 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2200
2201 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2202 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002203 case TargetLowering::Custom:
2204 isCustom = true;
2205 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002206 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2208 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002209 if (isCustom) {
2210 Tmp1 = TLI.LowerOperation(Result, DAG);
2211 if (Tmp1.Val) Result = Tmp1;
2212 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002213 break;
2214 case TargetLowering::Expand:
2215 // This defaults to loading a pointer from the input and storing it to the
2216 // output, returning the chain.
2217 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2218 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2219 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002220 break;
2221 }
2222 break;
2223
2224 case ISD::VAEND:
2225 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2226 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2227
2228 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2229 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002230 case TargetLowering::Custom:
2231 isCustom = true;
2232 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002233 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002234 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002235 if (isCustom) {
2236 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2237 if (Tmp1.Val) Result = Tmp1;
2238 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002239 break;
2240 case TargetLowering::Expand:
2241 Result = Tmp1; // Default to a no-op, return the chain
2242 break;
2243 }
2244 break;
2245
2246 case ISD::VASTART:
2247 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2248 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2249
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002250 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2251
Nate Begemanacc398c2006-01-25 18:21:52 +00002252 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2253 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002254 case TargetLowering::Legal: break;
2255 case TargetLowering::Custom:
2256 Tmp1 = TLI.LowerOperation(Result, DAG);
2257 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002258 break;
2259 }
2260 break;
2261
Nate Begeman35ef9132006-01-11 21:21:00 +00002262 case ISD::ROTL:
2263 case ISD::ROTR:
2264 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2265 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002266
2267 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2268 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002269 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002270 break;
2271
Nate Begemand88fc032006-01-14 03:14:10 +00002272 case ISD::BSWAP:
2273 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2274 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002275 case TargetLowering::Custom:
2276 assert(0 && "Cannot custom legalize this yet!");
2277 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002278 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002279 break;
2280 case TargetLowering::Promote: {
2281 MVT::ValueType OVT = Tmp1.getValueType();
2282 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2283 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002284
Chris Lattner456a93a2006-01-28 07:39:30 +00002285 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2286 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2287 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2288 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2289 break;
2290 }
2291 case TargetLowering::Expand:
2292 Result = ExpandBSWAP(Tmp1);
2293 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002294 }
2295 break;
2296
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002297 case ISD::CTPOP:
2298 case ISD::CTTZ:
2299 case ISD::CTLZ:
2300 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2301 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002302 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002303 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002304 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002305 break;
2306 case TargetLowering::Promote: {
2307 MVT::ValueType OVT = Tmp1.getValueType();
2308 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002309
2310 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002311 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2312 // Perform the larger operation, then subtract if needed.
2313 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002314 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002315 case ISD::CTPOP:
2316 Result = Tmp1;
2317 break;
2318 case ISD::CTTZ:
2319 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002320 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2321 DAG.getConstant(getSizeInBits(NVT), NVT),
2322 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002323 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002324 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2325 break;
2326 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002327 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002328 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2329 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002330 getSizeInBits(OVT), NVT));
2331 break;
2332 }
2333 break;
2334 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002335 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002336 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002337 break;
2338 }
2339 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002340
Chris Lattner2c8086f2005-04-02 05:00:07 +00002341 // Unary operators
2342 case ISD::FABS:
2343 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002344 case ISD::FSQRT:
2345 case ISD::FSIN:
2346 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002347 Tmp1 = LegalizeOp(Node->getOperand(0));
2348 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002349 case TargetLowering::Promote:
2350 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002351 isCustom = true;
2352 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002353 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002354 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002355 if (isCustom) {
2356 Tmp1 = TLI.LowerOperation(Result, DAG);
2357 if (Tmp1.Val) Result = Tmp1;
2358 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002359 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002360 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002361 switch (Node->getOpcode()) {
2362 default: assert(0 && "Unreachable!");
2363 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002364 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2365 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002366 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002367 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002368 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002369 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2370 MVT::ValueType VT = Node->getValueType(0);
2371 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002372 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002373 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2374 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002375 break;
2376 }
2377 case ISD::FSQRT:
2378 case ISD::FSIN:
2379 case ISD::FCOS: {
2380 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002381 const char *FnName = 0;
2382 switch(Node->getOpcode()) {
2383 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2384 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2385 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2386 default: assert(0 && "Unreachable!");
2387 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002388 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002389 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002390 break;
2391 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002392 }
2393 break;
2394 }
2395 break;
Chris Lattner35481892005-12-23 00:16:34 +00002396
2397 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002398 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002399 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002400 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002401 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2402 Node->getOperand(0).getValueType())) {
2403 default: assert(0 && "Unknown operation action!");
2404 case TargetLowering::Expand:
2405 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2406 break;
2407 case TargetLowering::Legal:
2408 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002409 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002410 break;
2411 }
2412 }
2413 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002414 case ISD::VBIT_CONVERT: {
2415 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2416 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2417
2418 // The input has to be a vector type, we have to either scalarize it, pack
2419 // it, or convert it based on whether the input vector type is legal.
2420 SDNode *InVal = Node->getOperand(0).Val;
2421 unsigned NumElems =
2422 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2423 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2424
2425 // Figure out if there is a Packed type corresponding to this Vector
2426 // type. If so, convert to the packed type.
2427 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2428 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2429 // Turn this into a bit convert of the packed input.
2430 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2431 PackVectorOp(Node->getOperand(0), TVT));
2432 break;
2433 } else if (NumElems == 1) {
2434 // Turn this into a bit convert of the scalar input.
2435 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2436 PackVectorOp(Node->getOperand(0), EVT));
2437 break;
2438 } else {
2439 // FIXME: UNIMP! Store then reload
2440 assert(0 && "Cast from unsupported vector type not implemented yet!");
2441 }
2442 }
2443
Chris Lattner2c8086f2005-04-02 05:00:07 +00002444 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002445 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002446 case ISD::UINT_TO_FP: {
2447 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002448 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2449 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002450 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002451 Node->getOperand(0).getValueType())) {
2452 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002453 case TargetLowering::Custom:
2454 isCustom = true;
2455 // FALLTHROUGH
2456 case TargetLowering::Legal:
2457 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002458 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002459 if (isCustom) {
2460 Tmp1 = TLI.LowerOperation(Result, DAG);
2461 if (Tmp1.Val) Result = Tmp1;
2462 }
2463 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002464 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002465 Result = ExpandLegalINT_TO_FP(isSigned,
2466 LegalizeOp(Node->getOperand(0)),
2467 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002468 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002469 case TargetLowering::Promote:
2470 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2471 Node->getValueType(0),
2472 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002473 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002474 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002475 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002476 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002477 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2478 Node->getValueType(0), Node->getOperand(0));
2479 break;
2480 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002481 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002482 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002483 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2484 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002485 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002486 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2487 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002488 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002489 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2490 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002491 break;
2492 }
2493 break;
2494 }
2495 case ISD::TRUNCATE:
2496 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2497 case Legal:
2498 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002499 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002500 break;
2501 case Expand:
2502 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2503
2504 // Since the result is legal, we should just be able to truncate the low
2505 // part of the source.
2506 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2507 break;
2508 case Promote:
2509 Result = PromoteOp(Node->getOperand(0));
2510 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2511 break;
2512 }
2513 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002514
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002515 case ISD::FP_TO_SINT:
2516 case ISD::FP_TO_UINT:
2517 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2518 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002519 Tmp1 = LegalizeOp(Node->getOperand(0));
2520
Chris Lattner1618beb2005-07-29 00:11:56 +00002521 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2522 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002523 case TargetLowering::Custom:
2524 isCustom = true;
2525 // FALLTHROUGH
2526 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002527 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002528 if (isCustom) {
2529 Tmp1 = TLI.LowerOperation(Result, DAG);
2530 if (Tmp1.Val) Result = Tmp1;
2531 }
2532 break;
2533 case TargetLowering::Promote:
2534 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2535 Node->getOpcode() == ISD::FP_TO_SINT);
2536 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002537 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002538 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2539 SDOperand True, False;
2540 MVT::ValueType VT = Node->getOperand(0).getValueType();
2541 MVT::ValueType NVT = Node->getValueType(0);
2542 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2543 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2544 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2545 Node->getOperand(0), Tmp2, ISD::SETLT);
2546 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2547 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002548 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002549 Tmp2));
2550 False = DAG.getNode(ISD::XOR, NVT, False,
2551 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002552 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2553 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002554 } else {
2555 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2556 }
2557 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002558 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002559 break;
2560 case Expand:
2561 assert(0 && "Shouldn't need to expand other operators here!");
2562 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002563 Tmp1 = PromoteOp(Node->getOperand(0));
2564 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2565 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002566 break;
2567 }
2568 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002569
Chris Lattner13c78e22005-09-02 00:18:10 +00002570 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002571 case ISD::ZERO_EXTEND:
2572 case ISD::SIGN_EXTEND:
2573 case ISD::FP_EXTEND:
2574 case ISD::FP_ROUND:
2575 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002576 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002577 case Legal:
2578 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002579 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002580 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002581 case Promote:
2582 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002583 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002584 Tmp1 = PromoteOp(Node->getOperand(0));
2585 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002586 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002587 case ISD::ZERO_EXTEND:
2588 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002589 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002590 Result = DAG.getZeroExtendInReg(Result,
2591 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002592 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002593 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002594 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002595 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002596 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002597 Result,
2598 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002599 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002600 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002601 Result = PromoteOp(Node->getOperand(0));
2602 if (Result.getValueType() != Op.getValueType())
2603 // Dynamically dead while we have only 2 FP types.
2604 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2605 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002606 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002607 Result = PromoteOp(Node->getOperand(0));
2608 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2609 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002610 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002611 }
2612 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002613 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002614 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002615 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002616 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002617
2618 // If this operation is not supported, convert it to a shl/shr or load/store
2619 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002620 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2621 default: assert(0 && "This action not supported for this op yet!");
2622 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002623 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002624 break;
2625 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002626 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002627 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002628 // NOTE: we could fall back on load/store here too for targets without
2629 // SAR. However, it is doubtful that any exist.
2630 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2631 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002632 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002633 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2634 Node->getOperand(0), ShiftCst);
2635 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2636 Result, ShiftCst);
2637 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2638 // The only way we can lower this is to turn it into a STORETRUNC,
2639 // EXTLOAD pair, targetting a temporary location (a stack slot).
2640
2641 // NOTE: there is a choice here between constantly creating new stack
2642 // slots and always reusing the same one. We currently always create
2643 // new ones, as reuse may inhibit scheduling.
2644 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2645 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2646 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2647 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002648 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002649 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2650 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2651 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002652 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002653 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002654 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2655 Result, StackSlot, DAG.getSrcValue(NULL),
2656 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002657 } else {
2658 assert(0 && "Unknown op");
2659 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002660 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002661 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002662 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002663 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002664 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002665
2666 // Make sure that the generated code is itself legal.
2667 if (Result != Op)
2668 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002669
Chris Lattner45982da2005-05-12 16:53:42 +00002670 // Note that LegalizeOp may be reentered even from single-use nodes, which
2671 // means that we always must cache transformed nodes.
2672 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002673 return Result;
2674}
2675
Chris Lattner8b6fa222005-01-15 22:16:26 +00002676/// PromoteOp - Given an operation that produces a value in an invalid type,
2677/// promote it to compute the value into a larger type. The produced value will
2678/// have the correct bits for the low portion of the register, but no guarantee
2679/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002680SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2681 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002682 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002683 assert(getTypeAction(VT) == Promote &&
2684 "Caller should expand or legalize operands that are not promotable!");
2685 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2686 "Cannot promote to smaller type!");
2687
Chris Lattner03c85462005-01-15 05:21:40 +00002688 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002689 SDOperand Result;
2690 SDNode *Node = Op.Val;
2691
Chris Lattner6fdcb252005-09-02 20:32:45 +00002692 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2693 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002694
Chris Lattner03c85462005-01-15 05:21:40 +00002695 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002696 case ISD::CopyFromReg:
2697 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002698 default:
2699 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2700 assert(0 && "Do not know how to promote this operator!");
2701 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002702 case ISD::UNDEF:
2703 Result = DAG.getNode(ISD::UNDEF, NVT);
2704 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002705 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002706 if (VT != MVT::i1)
2707 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2708 else
2709 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002710 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2711 break;
2712 case ISD::ConstantFP:
2713 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2714 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2715 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002716
Chris Lattner82fbfb62005-01-18 02:59:52 +00002717 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002718 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002719 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2720 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002721 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002722
2723 case ISD::TRUNCATE:
2724 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2725 case Legal:
2726 Result = LegalizeOp(Node->getOperand(0));
2727 assert(Result.getValueType() >= NVT &&
2728 "This truncation doesn't make sense!");
2729 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2730 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2731 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002732 case Promote:
2733 // The truncation is not required, because we don't guarantee anything
2734 // about high bits anyway.
2735 Result = PromoteOp(Node->getOperand(0));
2736 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002737 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002738 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2739 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002740 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002741 }
2742 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002743 case ISD::SIGN_EXTEND:
2744 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002745 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002746 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2747 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2748 case Legal:
2749 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002750 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002751 break;
2752 case Promote:
2753 // Promote the reg if it's smaller.
2754 Result = PromoteOp(Node->getOperand(0));
2755 // The high bits are not guaranteed to be anything. Insert an extend.
2756 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002757 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002758 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002759 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002760 Result = DAG.getZeroExtendInReg(Result,
2761 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002762 break;
2763 }
2764 break;
Chris Lattner35481892005-12-23 00:16:34 +00002765 case ISD::BIT_CONVERT:
2766 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2767 Result = PromoteOp(Result);
2768 break;
2769
Chris Lattner8b6fa222005-01-15 22:16:26 +00002770 case ISD::FP_EXTEND:
2771 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2772 case ISD::FP_ROUND:
2773 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2774 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2775 case Promote: assert(0 && "Unreachable with 2 FP types!");
2776 case Legal:
2777 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002778 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002779 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002780 break;
2781 }
2782 break;
2783
2784 case ISD::SINT_TO_FP:
2785 case ISD::UINT_TO_FP:
2786 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2787 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002788 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002789 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002790 break;
2791
2792 case Promote:
2793 Result = PromoteOp(Node->getOperand(0));
2794 if (Node->getOpcode() == ISD::SINT_TO_FP)
2795 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002796 Result,
2797 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002798 else
Chris Lattner23993562005-04-13 02:38:47 +00002799 Result = DAG.getZeroExtendInReg(Result,
2800 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002801 // No extra round required here.
2802 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002803 break;
2804 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002805 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2806 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002807 // Round if we cannot tolerate excess precision.
2808 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002809 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2810 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002811 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002812 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002813 break;
2814
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002815 case ISD::SIGN_EXTEND_INREG:
2816 Result = PromoteOp(Node->getOperand(0));
2817 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2818 Node->getOperand(1));
2819 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002820 case ISD::FP_TO_SINT:
2821 case ISD::FP_TO_UINT:
2822 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2823 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002824 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002825 break;
2826 case Promote:
2827 // The input result is prerounded, so we don't have to do anything
2828 // special.
2829 Tmp1 = PromoteOp(Node->getOperand(0));
2830 break;
2831 case Expand:
2832 assert(0 && "not implemented");
2833 }
Nate Begemand2558e32005-08-14 01:20:53 +00002834 // If we're promoting a UINT to a larger size, check to see if the new node
2835 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2836 // we can use that instead. This allows us to generate better code for
2837 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2838 // legal, such as PowerPC.
2839 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002840 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002841 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2842 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002843 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2844 } else {
2845 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2846 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002847 break;
2848
Chris Lattner2c8086f2005-04-02 05:00:07 +00002849 case ISD::FABS:
2850 case ISD::FNEG:
2851 Tmp1 = PromoteOp(Node->getOperand(0));
2852 assert(Tmp1.getValueType() == NVT);
2853 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2854 // NOTE: we do not have to do any extra rounding here for
2855 // NoExcessFPPrecision, because we know the input will have the appropriate
2856 // precision, and these operations don't modify precision at all.
2857 break;
2858
Chris Lattnerda6ba872005-04-28 21:44:33 +00002859 case ISD::FSQRT:
2860 case ISD::FSIN:
2861 case ISD::FCOS:
2862 Tmp1 = PromoteOp(Node->getOperand(0));
2863 assert(Tmp1.getValueType() == NVT);
2864 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002865 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002866 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2867 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002868 break;
2869
Chris Lattner03c85462005-01-15 05:21:40 +00002870 case ISD::AND:
2871 case ISD::OR:
2872 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002873 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002874 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002875 case ISD::MUL:
2876 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002877 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002878 // that too is okay if they are integer operations.
2879 Tmp1 = PromoteOp(Node->getOperand(0));
2880 Tmp2 = PromoteOp(Node->getOperand(1));
2881 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2882 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002883 break;
2884 case ISD::FADD:
2885 case ISD::FSUB:
2886 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002887 Tmp1 = PromoteOp(Node->getOperand(0));
2888 Tmp2 = PromoteOp(Node->getOperand(1));
2889 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2890 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2891
2892 // Floating point operations will give excess precision that we may not be
2893 // able to tolerate. If we DO allow excess precision, just leave it,
2894 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002895 // FIXME: Why would we need to round FP ops more than integer ones?
2896 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002897 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002898 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2899 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002900 break;
2901
Chris Lattner8b6fa222005-01-15 22:16:26 +00002902 case ISD::SDIV:
2903 case ISD::SREM:
2904 // These operators require that their input be sign extended.
2905 Tmp1 = PromoteOp(Node->getOperand(0));
2906 Tmp2 = PromoteOp(Node->getOperand(1));
2907 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002908 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2909 DAG.getValueType(VT));
2910 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2911 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002912 }
2913 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2914
2915 // Perform FP_ROUND: this is probably overly pessimistic.
2916 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002917 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2918 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002919 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002920 case ISD::FDIV:
2921 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002922 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002923 // These operators require that their input be fp extended.
2924 Tmp1 = PromoteOp(Node->getOperand(0));
2925 Tmp2 = PromoteOp(Node->getOperand(1));
2926 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2927
2928 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002929 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002930 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2931 DAG.getValueType(VT));
2932 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002933
2934 case ISD::UDIV:
2935 case ISD::UREM:
2936 // These operators require that their input be zero extended.
2937 Tmp1 = PromoteOp(Node->getOperand(0));
2938 Tmp2 = PromoteOp(Node->getOperand(1));
2939 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002940 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2941 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002942 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2943 break;
2944
2945 case ISD::SHL:
2946 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002947 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002948 break;
2949 case ISD::SRA:
2950 // The input value must be properly sign extended.
2951 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002952 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2953 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002954 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002955 break;
2956 case ISD::SRL:
2957 // The input value must be properly zero extended.
2958 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002959 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002960 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002961 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002962
2963 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002964 Tmp1 = Node->getOperand(0); // Get the chain.
2965 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002966 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2967 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2968 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2969 } else {
2970 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2971 Node->getOperand(2));
2972 // Increment the pointer, VAList, to the next vaarg
2973 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2974 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2975 TLI.getPointerTy()));
2976 // Store the incremented VAList to the legalized pointer
2977 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2978 Node->getOperand(2));
2979 // Load the actual argument out of the pointer VAList
2980 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2981 DAG.getSrcValue(0), VT);
2982 }
2983 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002984 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002985 break;
2986
Chris Lattner03c85462005-01-15 05:21:40 +00002987 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002988 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2989 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002990 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002991 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002992 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002993 case ISD::SEXTLOAD:
2994 case ISD::ZEXTLOAD:
2995 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002996 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2997 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002998 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002999 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003000 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003001 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003002 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003003 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3004 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003005 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003006 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003007 case ISD::SELECT_CC:
3008 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3009 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3010 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003011 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003012 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003013 case ISD::BSWAP:
3014 Tmp1 = Node->getOperand(0);
3015 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3016 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3017 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3018 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3019 TLI.getShiftAmountTy()));
3020 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003021 case ISD::CTPOP:
3022 case ISD::CTTZ:
3023 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003024 // Zero extend the argument
3025 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003026 // Perform the larger operation, then subtract if needed.
3027 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003028 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003029 case ISD::CTPOP:
3030 Result = Tmp1;
3031 break;
3032 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003033 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003034 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003035 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003036 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003037 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003038 break;
3039 case ISD::CTLZ:
3040 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003041 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3042 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003043 getSizeInBits(VT), NVT));
3044 break;
3045 }
3046 break;
Chris Lattner15972212006-03-31 17:55:51 +00003047 case ISD::VEXTRACT_VECTOR_ELT:
3048 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3049 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003050 case ISD::EXTRACT_VECTOR_ELT:
3051 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3052 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003053 }
3054
3055 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003056
3057 // Make sure the result is itself legal.
3058 Result = LegalizeOp(Result);
3059
3060 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003061 AddPromotedOperand(Op, Result);
3062 return Result;
3063}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003064
Chris Lattner15972212006-03-31 17:55:51 +00003065/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3066/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3067/// on the vector type. The return type of this matches the element type of the
3068/// vector, which may not be legal for the target.
3069SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3070 // We know that operand #0 is the Vec vector. If the index is a constant
3071 // or if the invec is a supported hardware type, we can use it. Otherwise,
3072 // lower to a store then an indexed load.
3073 SDOperand Vec = Op.getOperand(0);
3074 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3075
3076 SDNode *InVal = Vec.Val;
3077 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3078 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3079
3080 // Figure out if there is a Packed type corresponding to this Vector
3081 // type. If so, convert to the packed type.
3082 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3083 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3084 // Turn this into a packed extract_vector_elt operation.
3085 Vec = PackVectorOp(Vec, TVT);
3086 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3087 } else if (NumElems == 1) {
3088 // This must be an access of the only element. Return it.
3089 return PackVectorOp(Vec, EVT);
3090 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3091 SDOperand Lo, Hi;
3092 SplitVectorOp(Vec, Lo, Hi);
3093 if (CIdx->getValue() < NumElems/2) {
3094 Vec = Lo;
3095 } else {
3096 Vec = Hi;
3097 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3098 }
3099
3100 // It's now an extract from the appropriate high or low part. Recurse.
3101 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3102 return LowerVEXTRACT_VECTOR_ELT(Op);
3103 } else {
3104 // Variable index case for extract element.
3105 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3106 assert(0 && "unimp!");
3107 return SDOperand();
3108 }
3109}
3110
Chris Lattner4aab2f42006-04-02 05:06:04 +00003111/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3112/// memory traffic.
3113SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3114 SDOperand Vector = Op.getOperand(0);
3115 SDOperand Idx = Op.getOperand(1);
3116
3117 // If the target doesn't support this, store the value to a temporary
3118 // stack slot, then LOAD the scalar element back out.
3119 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3120 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3121 Vector, StackPtr, DAG.getSrcValue(NULL));
3122
3123 // Add the offset to the index.
3124 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3125 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3126 DAG.getConstant(EltSize, Idx.getValueType()));
3127 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3128
3129 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3130}
3131
Chris Lattner15972212006-03-31 17:55:51 +00003132
Nate Begeman750ac1b2006-02-01 07:19:44 +00003133/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3134/// with condition CC on the current target. This usually involves legalizing
3135/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3136/// there may be no choice but to create a new SetCC node to represent the
3137/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3138/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3139void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3140 SDOperand &RHS,
3141 SDOperand &CC) {
3142 SDOperand Tmp1, Tmp2, Result;
3143
3144 switch (getTypeAction(LHS.getValueType())) {
3145 case Legal:
3146 Tmp1 = LegalizeOp(LHS); // LHS
3147 Tmp2 = LegalizeOp(RHS); // RHS
3148 break;
3149 case Promote:
3150 Tmp1 = PromoteOp(LHS); // LHS
3151 Tmp2 = PromoteOp(RHS); // RHS
3152
3153 // If this is an FP compare, the operands have already been extended.
3154 if (MVT::isInteger(LHS.getValueType())) {
3155 MVT::ValueType VT = LHS.getValueType();
3156 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3157
3158 // Otherwise, we have to insert explicit sign or zero extends. Note
3159 // that we could insert sign extends for ALL conditions, but zero extend
3160 // is cheaper on many machines (an AND instead of two shifts), so prefer
3161 // it.
3162 switch (cast<CondCodeSDNode>(CC)->get()) {
3163 default: assert(0 && "Unknown integer comparison!");
3164 case ISD::SETEQ:
3165 case ISD::SETNE:
3166 case ISD::SETUGE:
3167 case ISD::SETUGT:
3168 case ISD::SETULE:
3169 case ISD::SETULT:
3170 // ALL of these operations will work if we either sign or zero extend
3171 // the operands (including the unsigned comparisons!). Zero extend is
3172 // usually a simpler/cheaper operation, so prefer it.
3173 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3174 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3175 break;
3176 case ISD::SETGE:
3177 case ISD::SETGT:
3178 case ISD::SETLT:
3179 case ISD::SETLE:
3180 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3181 DAG.getValueType(VT));
3182 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3183 DAG.getValueType(VT));
3184 break;
3185 }
3186 }
3187 break;
3188 case Expand:
3189 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3190 ExpandOp(LHS, LHSLo, LHSHi);
3191 ExpandOp(RHS, RHSLo, RHSHi);
3192 switch (cast<CondCodeSDNode>(CC)->get()) {
3193 case ISD::SETEQ:
3194 case ISD::SETNE:
3195 if (RHSLo == RHSHi)
3196 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3197 if (RHSCST->isAllOnesValue()) {
3198 // Comparison to -1.
3199 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3200 Tmp2 = RHSLo;
3201 break;
3202 }
3203
3204 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3205 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3206 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3207 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3208 break;
3209 default:
3210 // If this is a comparison of the sign bit, just look at the top part.
3211 // X > -1, x < 0
3212 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3213 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3214 CST->getValue() == 0) || // X < 0
3215 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3216 CST->isAllOnesValue())) { // X > -1
3217 Tmp1 = LHSHi;
3218 Tmp2 = RHSHi;
3219 break;
3220 }
3221
3222 // FIXME: This generated code sucks.
3223 ISD::CondCode LowCC;
3224 switch (cast<CondCodeSDNode>(CC)->get()) {
3225 default: assert(0 && "Unknown integer setcc!");
3226 case ISD::SETLT:
3227 case ISD::SETULT: LowCC = ISD::SETULT; break;
3228 case ISD::SETGT:
3229 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3230 case ISD::SETLE:
3231 case ISD::SETULE: LowCC = ISD::SETULE; break;
3232 case ISD::SETGE:
3233 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3234 }
3235
3236 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3237 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3238 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3239
3240 // NOTE: on targets without efficient SELECT of bools, we can always use
3241 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3242 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3243 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3244 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3245 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3246 Result, Tmp1, Tmp2));
3247 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003248 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003249 }
3250 }
3251 LHS = Tmp1;
3252 RHS = Tmp2;
3253}
3254
Chris Lattner35481892005-12-23 00:16:34 +00003255/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003256/// The resultant code need not be legal. Note that SrcOp is the input operand
3257/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003258SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3259 SDOperand SrcOp) {
3260 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003261 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003262
3263 // Emit a store to the stack slot.
3264 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003265 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003266 // Result is a load from the stack slot.
3267 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3268}
3269
Chris Lattner4352cc92006-04-04 17:23:26 +00003270SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3271 // Create a vector sized/aligned stack slot, store the value to element #0,
3272 // then load the whole vector back out.
3273 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3274 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3275 Node->getOperand(0), StackPtr,
3276 DAG.getSrcValue(NULL));
3277 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3278}
3279
3280
Chris Lattnerce872152006-03-19 06:31:19 +00003281/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3282/// support the operation, but do support the resultant packed vector type.
3283SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3284
3285 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003286 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003287 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003288 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003289 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003290 std::map<SDOperand, std::vector<unsigned> > Values;
3291 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003292 bool isConstant = true;
3293 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3294 SplatValue.getOpcode() != ISD::UNDEF)
3295 isConstant = false;
3296
Evan Cheng033e6812006-03-24 01:17:21 +00003297 for (unsigned i = 1; i < NumElems; ++i) {
3298 SDOperand V = Node->getOperand(i);
3299 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3300 if (I != Values.end())
3301 I->second.push_back(i);
3302 else
3303 Values[V].push_back(i);
3304 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003305 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003306 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003307 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003308
3309 // If this isn't a constant element or an undef, we can't use a constant
3310 // pool load.
3311 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3312 V.getOpcode() != ISD::UNDEF)
3313 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003314 }
3315
3316 if (isOnlyLowElement) {
3317 // If the low element is an undef too, then this whole things is an undef.
3318 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3319 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3320 // Otherwise, turn this into a scalar_to_vector node.
3321 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3322 Node->getOperand(0));
3323 }
3324
Chris Lattner2eb86532006-03-24 07:29:17 +00003325 // If all elements are constants, create a load from the constant pool.
3326 if (isConstant) {
3327 MVT::ValueType VT = Node->getValueType(0);
3328 const Type *OpNTy =
3329 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3330 std::vector<Constant*> CV;
3331 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3332 if (ConstantFPSDNode *V =
3333 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3334 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3335 } else if (ConstantSDNode *V =
3336 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3337 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3338 } else {
3339 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3340 CV.push_back(UndefValue::get(OpNTy));
3341 }
3342 }
3343 Constant *CP = ConstantPacked::get(CV);
3344 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3345 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3346 DAG.getSrcValue(NULL));
3347 }
3348
Chris Lattner87100e02006-03-20 01:52:29 +00003349 if (SplatValue.Val) { // Splat of one value?
3350 // Build the shuffle constant vector: <0, 0, 0, 0>
3351 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003352 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003353 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003354 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003355 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3356
3357 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003358 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003359 // Get the splatted value into the low element of a vector register.
3360 SDOperand LowValVec =
3361 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3362
3363 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3364 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3365 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3366 SplatMask);
3367 }
3368 }
3369
Evan Cheng033e6812006-03-24 01:17:21 +00003370 // If there are only two unique elements, we may be able to turn this into a
3371 // vector shuffle.
3372 if (Values.size() == 2) {
3373 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3374 MVT::ValueType MaskVT =
3375 MVT::getIntVectorWithNumElements(NumElems);
3376 std::vector<SDOperand> MaskVec(NumElems);
3377 unsigned i = 0;
3378 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3379 E = Values.end(); I != E; ++I) {
3380 for (std::vector<unsigned>::iterator II = I->second.begin(),
3381 EE = I->second.end(); II != EE; ++II)
3382 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3383 i += NumElems;
3384 }
3385 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3386
3387 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003388 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3389 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng033e6812006-03-24 01:17:21 +00003390 std::vector<SDOperand> Ops;
3391 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3392 E = Values.end(); I != E; ++I) {
3393 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3394 I->first);
3395 Ops.push_back(Op);
3396 }
3397 Ops.push_back(ShuffleMask);
3398
3399 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3400 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3401 }
3402 }
Chris Lattnerce872152006-03-19 06:31:19 +00003403
3404 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3405 // aligned object on the stack, store each element into it, then load
3406 // the result as a vector.
3407 MVT::ValueType VT = Node->getValueType(0);
3408 // Create the stack frame object.
3409 SDOperand FIPtr = CreateStackTemporary(VT);
3410
3411 // Emit a store of each element to the stack slot.
3412 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003413 unsigned TypeByteSize =
3414 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3415 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3416 // Store (in the right endianness) the elements to memory.
3417 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3418 // Ignore undef elements.
3419 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3420
Chris Lattner841c8822006-03-22 01:46:54 +00003421 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003422
3423 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3424 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3425
3426 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3427 Node->getOperand(i), Idx,
3428 DAG.getSrcValue(NULL)));
3429 }
3430
3431 SDOperand StoreChain;
3432 if (!Stores.empty()) // Not all undef elements?
3433 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3434 else
3435 StoreChain = DAG.getEntryNode();
3436
3437 // Result is a load from the stack slot.
3438 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3439}
3440
3441/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3442/// specified value type.
3443SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3444 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3445 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3446 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3447 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3448}
3449
Chris Lattner5b359c62005-04-02 04:00:59 +00003450void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3451 SDOperand Op, SDOperand Amt,
3452 SDOperand &Lo, SDOperand &Hi) {
3453 // Expand the subcomponents.
3454 SDOperand LHSL, LHSH;
3455 ExpandOp(Op, LHSL, LHSH);
3456
3457 std::vector<SDOperand> Ops;
3458 Ops.push_back(LHSL);
3459 Ops.push_back(LHSH);
3460 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003461 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003462 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003463 Hi = Lo.getValue(1);
3464}
3465
3466
Chris Lattnere34b3962005-01-19 04:19:40 +00003467/// ExpandShift - Try to find a clever way to expand this shift operation out to
3468/// smaller elements. If we can't find a way that is more efficient than a
3469/// libcall on this target, return false. Otherwise, return true with the
3470/// low-parts expanded into Lo and Hi.
3471bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3472 SDOperand &Lo, SDOperand &Hi) {
3473 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3474 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003475
Chris Lattnere34b3962005-01-19 04:19:40 +00003476 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003477 SDOperand ShAmt = LegalizeOp(Amt);
3478 MVT::ValueType ShTy = ShAmt.getValueType();
3479 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3480 unsigned NVTBits = MVT::getSizeInBits(NVT);
3481
3482 // Handle the case when Amt is an immediate. Other cases are currently broken
3483 // and are disabled.
3484 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3485 unsigned Cst = CN->getValue();
3486 // Expand the incoming operand to be shifted, so that we have its parts
3487 SDOperand InL, InH;
3488 ExpandOp(Op, InL, InH);
3489 switch(Opc) {
3490 case ISD::SHL:
3491 if (Cst > VTBits) {
3492 Lo = DAG.getConstant(0, NVT);
3493 Hi = DAG.getConstant(0, NVT);
3494 } else if (Cst > NVTBits) {
3495 Lo = DAG.getConstant(0, NVT);
3496 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003497 } else if (Cst == NVTBits) {
3498 Lo = DAG.getConstant(0, NVT);
3499 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003500 } else {
3501 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3502 Hi = DAG.getNode(ISD::OR, NVT,
3503 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3504 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3505 }
3506 return true;
3507 case ISD::SRL:
3508 if (Cst > VTBits) {
3509 Lo = DAG.getConstant(0, NVT);
3510 Hi = DAG.getConstant(0, NVT);
3511 } else if (Cst > NVTBits) {
3512 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3513 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003514 } else if (Cst == NVTBits) {
3515 Lo = InH;
3516 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003517 } else {
3518 Lo = DAG.getNode(ISD::OR, NVT,
3519 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3520 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3521 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3522 }
3523 return true;
3524 case ISD::SRA:
3525 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003526 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003527 DAG.getConstant(NVTBits-1, ShTy));
3528 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003529 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003530 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003531 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003532 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003533 } else if (Cst == NVTBits) {
3534 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003535 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003536 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003537 } else {
3538 Lo = DAG.getNode(ISD::OR, NVT,
3539 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3540 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3541 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3542 }
3543 return true;
3544 }
3545 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003546 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003547}
Chris Lattner77e77a62005-01-21 06:05:23 +00003548
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003549
Chris Lattner77e77a62005-01-21 06:05:23 +00003550// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3551// does not fit into a register, return the lo part and set the hi part to the
3552// by-reg argument. If it does fit into a single register, return the result
3553// and leave the Hi part unset.
3554SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3555 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003556 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3557 // The input chain to this libcall is the entry node of the function.
3558 // Legalizing the call will automatically add the previous call to the
3559 // dependence.
3560 SDOperand InChain = DAG.getEntryNode();
3561
Chris Lattner77e77a62005-01-21 06:05:23 +00003562 TargetLowering::ArgListTy Args;
3563 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3564 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3565 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3566 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3567 }
3568 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003569
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003570 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003571 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003572 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003573 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3574 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003575
Chris Lattner6831a812006-02-13 09:18:02 +00003576 // Legalize the call sequence, starting with the chain. This will advance
3577 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3578 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3579 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003580 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003581 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003582 default: assert(0 && "Unknown thing");
3583 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003584 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003585 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003586 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003587 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003588 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003589 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003590 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003591}
3592
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003593
Chris Lattner77e77a62005-01-21 06:05:23 +00003594/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3595/// destination type is legal.
3596SDOperand SelectionDAGLegalize::
3597ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003598 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003599 assert(getTypeAction(Source.getValueType()) == Expand &&
3600 "This is not an expansion!");
3601 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3602
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003603 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003604 assert(Source.getValueType() == MVT::i64 &&
3605 "This only works for 64-bit -> FP");
3606 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3607 // incoming integer is set. To handle this, we dynamically test to see if
3608 // it is set, and, if so, add a fudge factor.
3609 SDOperand Lo, Hi;
3610 ExpandOp(Source, Lo, Hi);
3611
Chris Lattner66de05b2005-05-13 04:45:13 +00003612 // If this is unsigned, and not supported, first perform the conversion to
3613 // signed, then adjust the result if the sign bit is set.
3614 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3615 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3616
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003617 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3618 DAG.getConstant(0, Hi.getValueType()),
3619 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003620 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3621 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3622 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003623 uint64_t FF = 0x5f800000ULL;
3624 if (TLI.isLittleEndian()) FF <<= 32;
3625 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003626
Chris Lattner5839bf22005-08-26 17:15:30 +00003627 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003628 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3629 SDOperand FudgeInReg;
3630 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003631 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3632 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003633 else {
3634 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003635 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3636 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003637 }
Chris Lattner473a9902005-09-29 06:44:39 +00003638 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003639 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003640
Chris Lattnera88a2602005-05-14 05:33:54 +00003641 // Check to see if the target has a custom way to lower this. If so, use it.
3642 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3643 default: assert(0 && "This action not implemented for this operation!");
3644 case TargetLowering::Legal:
3645 case TargetLowering::Expand:
3646 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003647 case TargetLowering::Custom: {
3648 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3649 Source), DAG);
3650 if (NV.Val)
3651 return LegalizeOp(NV);
3652 break; // The target decided this was legal after all
3653 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003654 }
3655
Chris Lattner13689e22005-05-12 07:00:44 +00003656 // Expand the source, then glue it back together for the call. We must expand
3657 // the source in case it is shared (this pass of legalize must traverse it).
3658 SDOperand SrcLo, SrcHi;
3659 ExpandOp(Source, SrcLo, SrcHi);
3660 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3661
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003662 const char *FnName = 0;
3663 if (DestTy == MVT::f32)
3664 FnName = "__floatdisf";
3665 else {
3666 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3667 FnName = "__floatdidf";
3668 }
Chris Lattner6831a812006-02-13 09:18:02 +00003669
3670 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3671 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003672 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003673}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003674
Chris Lattner22cde6a2006-01-28 08:25:58 +00003675/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3676/// INT_TO_FP operation of the specified operand when the target requests that
3677/// we expand it. At this point, we know that the result and operand types are
3678/// legal for the target.
3679SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3680 SDOperand Op0,
3681 MVT::ValueType DestVT) {
3682 if (Op0.getValueType() == MVT::i32) {
3683 // simple 32-bit [signed|unsigned] integer to float/double expansion
3684
3685 // get the stack frame index of a 8 byte buffer
3686 MachineFunction &MF = DAG.getMachineFunction();
3687 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3688 // get address of 8 byte buffer
3689 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3690 // word offset constant for Hi/Lo address computation
3691 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3692 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003693 SDOperand Hi = StackSlot;
3694 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3695 if (TLI.isLittleEndian())
3696 std::swap(Hi, Lo);
3697
Chris Lattner22cde6a2006-01-28 08:25:58 +00003698 // if signed map to unsigned space
3699 SDOperand Op0Mapped;
3700 if (isSigned) {
3701 // constant used to invert sign bit (signed to unsigned mapping)
3702 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3703 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3704 } else {
3705 Op0Mapped = Op0;
3706 }
3707 // store the lo of the constructed double - based on integer input
3708 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3709 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3710 // initial hi portion of constructed double
3711 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3712 // store the hi of the constructed double - biased exponent
3713 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3714 InitialHi, Hi, DAG.getSrcValue(NULL));
3715 // load the constructed double
3716 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3717 DAG.getSrcValue(NULL));
3718 // FP constant to bias correct the final result
3719 SDOperand Bias = DAG.getConstantFP(isSigned ?
3720 BitsToDouble(0x4330000080000000ULL)
3721 : BitsToDouble(0x4330000000000000ULL),
3722 MVT::f64);
3723 // subtract the bias
3724 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3725 // final result
3726 SDOperand Result;
3727 // handle final rounding
3728 if (DestVT == MVT::f64) {
3729 // do nothing
3730 Result = Sub;
3731 } else {
3732 // if f32 then cast to f32
3733 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3734 }
3735 return Result;
3736 }
3737 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3738 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3739
3740 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3741 DAG.getConstant(0, Op0.getValueType()),
3742 ISD::SETLT);
3743 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3744 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3745 SignSet, Four, Zero);
3746
3747 // If the sign bit of the integer is set, the large number will be treated
3748 // as a negative number. To counteract this, the dynamic code adds an
3749 // offset depending on the data type.
3750 uint64_t FF;
3751 switch (Op0.getValueType()) {
3752 default: assert(0 && "Unsupported integer type!");
3753 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3754 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3755 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3756 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3757 }
3758 if (TLI.isLittleEndian()) FF <<= 32;
3759 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3760
3761 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3762 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3763 SDOperand FudgeInReg;
3764 if (DestVT == MVT::f32)
3765 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3766 DAG.getSrcValue(NULL));
3767 else {
3768 assert(DestVT == MVT::f64 && "Unexpected conversion");
3769 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3770 DAG.getEntryNode(), CPIdx,
3771 DAG.getSrcValue(NULL), MVT::f32));
3772 }
3773
3774 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3775}
3776
3777/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3778/// *INT_TO_FP operation of the specified operand when the target requests that
3779/// we promote it. At this point, we know that the result and operand types are
3780/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3781/// operation that takes a larger input.
3782SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3783 MVT::ValueType DestVT,
3784 bool isSigned) {
3785 // First step, figure out the appropriate *INT_TO_FP operation to use.
3786 MVT::ValueType NewInTy = LegalOp.getValueType();
3787
3788 unsigned OpToUse = 0;
3789
3790 // Scan for the appropriate larger type to use.
3791 while (1) {
3792 NewInTy = (MVT::ValueType)(NewInTy+1);
3793 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3794
3795 // If the target supports SINT_TO_FP of this type, use it.
3796 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3797 default: break;
3798 case TargetLowering::Legal:
3799 if (!TLI.isTypeLegal(NewInTy))
3800 break; // Can't use this datatype.
3801 // FALL THROUGH.
3802 case TargetLowering::Custom:
3803 OpToUse = ISD::SINT_TO_FP;
3804 break;
3805 }
3806 if (OpToUse) break;
3807 if (isSigned) continue;
3808
3809 // If the target supports UINT_TO_FP of this type, use it.
3810 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3811 default: break;
3812 case TargetLowering::Legal:
3813 if (!TLI.isTypeLegal(NewInTy))
3814 break; // Can't use this datatype.
3815 // FALL THROUGH.
3816 case TargetLowering::Custom:
3817 OpToUse = ISD::UINT_TO_FP;
3818 break;
3819 }
3820 if (OpToUse) break;
3821
3822 // Otherwise, try a larger type.
3823 }
3824
3825 // Okay, we found the operation and type to use. Zero extend our input to the
3826 // desired type then run the operation on it.
3827 return DAG.getNode(OpToUse, DestVT,
3828 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3829 NewInTy, LegalOp));
3830}
3831
3832/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3833/// FP_TO_*INT operation of the specified operand when the target requests that
3834/// we promote it. At this point, we know that the result and operand types are
3835/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3836/// operation that returns a larger result.
3837SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3838 MVT::ValueType DestVT,
3839 bool isSigned) {
3840 // First step, figure out the appropriate FP_TO*INT operation to use.
3841 MVT::ValueType NewOutTy = DestVT;
3842
3843 unsigned OpToUse = 0;
3844
3845 // Scan for the appropriate larger type to use.
3846 while (1) {
3847 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3848 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3849
3850 // If the target supports FP_TO_SINT returning this type, use it.
3851 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3852 default: break;
3853 case TargetLowering::Legal:
3854 if (!TLI.isTypeLegal(NewOutTy))
3855 break; // Can't use this datatype.
3856 // FALL THROUGH.
3857 case TargetLowering::Custom:
3858 OpToUse = ISD::FP_TO_SINT;
3859 break;
3860 }
3861 if (OpToUse) break;
3862
3863 // If the target supports FP_TO_UINT of this type, use it.
3864 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3865 default: break;
3866 case TargetLowering::Legal:
3867 if (!TLI.isTypeLegal(NewOutTy))
3868 break; // Can't use this datatype.
3869 // FALL THROUGH.
3870 case TargetLowering::Custom:
3871 OpToUse = ISD::FP_TO_UINT;
3872 break;
3873 }
3874 if (OpToUse) break;
3875
3876 // Otherwise, try a larger type.
3877 }
3878
3879 // Okay, we found the operation and type to use. Truncate the result of the
3880 // extended FP_TO_*INT operation to the desired size.
3881 return DAG.getNode(ISD::TRUNCATE, DestVT,
3882 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3883}
3884
3885/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3886///
3887SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3888 MVT::ValueType VT = Op.getValueType();
3889 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3890 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3891 switch (VT) {
3892 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3893 case MVT::i16:
3894 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3895 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3896 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3897 case MVT::i32:
3898 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3899 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3900 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3901 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3902 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3903 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3904 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3905 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3906 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3907 case MVT::i64:
3908 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3909 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3910 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3911 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3912 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3913 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3914 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3915 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3916 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3917 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3918 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3919 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3920 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3921 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3922 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3923 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3924 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3925 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3926 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3927 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3928 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3929 }
3930}
3931
3932/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3933///
3934SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3935 switch (Opc) {
3936 default: assert(0 && "Cannot expand this yet!");
3937 case ISD::CTPOP: {
3938 static const uint64_t mask[6] = {
3939 0x5555555555555555ULL, 0x3333333333333333ULL,
3940 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3941 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3942 };
3943 MVT::ValueType VT = Op.getValueType();
3944 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3945 unsigned len = getSizeInBits(VT);
3946 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3947 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3948 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3949 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3950 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3951 DAG.getNode(ISD::AND, VT,
3952 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3953 }
3954 return Op;
3955 }
3956 case ISD::CTLZ: {
3957 // for now, we do this:
3958 // x = x | (x >> 1);
3959 // x = x | (x >> 2);
3960 // ...
3961 // x = x | (x >>16);
3962 // x = x | (x >>32); // for 64-bit input
3963 // return popcount(~x);
3964 //
3965 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3966 MVT::ValueType VT = Op.getValueType();
3967 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3968 unsigned len = getSizeInBits(VT);
3969 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3970 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3971 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3972 }
3973 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3974 return DAG.getNode(ISD::CTPOP, VT, Op);
3975 }
3976 case ISD::CTTZ: {
3977 // for now, we use: { return popcount(~x & (x - 1)); }
3978 // unless the target has ctlz but not ctpop, in which case we use:
3979 // { return 32 - nlz(~x & (x-1)); }
3980 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3981 MVT::ValueType VT = Op.getValueType();
3982 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3983 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3984 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3985 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3986 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3987 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3988 TLI.isOperationLegal(ISD::CTLZ, VT))
3989 return DAG.getNode(ISD::SUB, VT,
3990 DAG.getConstant(getSizeInBits(VT), VT),
3991 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3992 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3993 }
3994 }
3995}
Chris Lattnere34b3962005-01-19 04:19:40 +00003996
3997
Chris Lattner3e928bb2005-01-07 07:47:09 +00003998/// ExpandOp - Expand the specified SDOperand into its two component pieces
3999/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4000/// LegalizeNodes map is filled in for any results that are not expanded, the
4001/// ExpandedNodes map is filled in for any results that are expanded, and the
4002/// Lo/Hi values are returned.
4003void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4004 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004005 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004006 SDNode *Node = Op.Val;
4007 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00004008 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4009 "Cannot expand FP values!");
4010 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004011 "Cannot expand to FP value or to larger int value!");
4012
Chris Lattner6fdcb252005-09-02 20:32:45 +00004013 // See if we already expanded it.
4014 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4015 = ExpandedNodes.find(Op);
4016 if (I != ExpandedNodes.end()) {
4017 Lo = I->second.first;
4018 Hi = I->second.second;
4019 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004020 }
4021
Chris Lattner3e928bb2005-01-07 07:47:09 +00004022 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004023 case ISD::CopyFromReg:
4024 assert(0 && "CopyFromReg must be legal!");
4025 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004026 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
4027 assert(0 && "Do not know how to expand this operator!");
4028 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004029 case ISD::UNDEF:
4030 Lo = DAG.getNode(ISD::UNDEF, NVT);
4031 Hi = DAG.getNode(ISD::UNDEF, NVT);
4032 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004033 case ISD::Constant: {
4034 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4035 Lo = DAG.getConstant(Cst, NVT);
4036 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4037 break;
4038 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004039 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004040 // Return the operands.
4041 Lo = Node->getOperand(0);
4042 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004043 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004044
4045 case ISD::SIGN_EXTEND_INREG:
4046 ExpandOp(Node->getOperand(0), Lo, Hi);
4047 // Sign extend the lo-part.
4048 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4049 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4050 TLI.getShiftAmountTy()));
4051 // sext_inreg the low part if needed.
4052 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4053 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004054
Nate Begemand88fc032006-01-14 03:14:10 +00004055 case ISD::BSWAP: {
4056 ExpandOp(Node->getOperand(0), Lo, Hi);
4057 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4058 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4059 Lo = TempLo;
4060 break;
4061 }
4062
Chris Lattneredb1add2005-05-11 04:51:16 +00004063 case ISD::CTPOP:
4064 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004065 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4066 DAG.getNode(ISD::CTPOP, NVT, Lo),
4067 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004068 Hi = DAG.getConstant(0, NVT);
4069 break;
4070
Chris Lattner39a8f332005-05-12 19:05:01 +00004071 case ISD::CTLZ: {
4072 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004073 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004074 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4075 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004076 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4077 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004078 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4079 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4080
4081 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4082 Hi = DAG.getConstant(0, NVT);
4083 break;
4084 }
4085
4086 case ISD::CTTZ: {
4087 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004088 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004089 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4090 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004091 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4092 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004093 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4094 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4095
4096 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4097 Hi = DAG.getConstant(0, NVT);
4098 break;
4099 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004100
Nate Begemanacc398c2006-01-25 18:21:52 +00004101 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004102 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4103 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004104 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4105 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4106
4107 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004108 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004109 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4110 if (!TLI.isLittleEndian())
4111 std::swap(Lo, Hi);
4112 break;
4113 }
4114
Chris Lattner3e928bb2005-01-07 07:47:09 +00004115 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004116 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4117 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004118 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004119
4120 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00004121 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004122 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4123 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00004124 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004125 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00004126
4127 // Build a factor node to remember that this load is independent of the
4128 // other one.
4129 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4130 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004131
Chris Lattner3e928bb2005-01-07 07:47:09 +00004132 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004133 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004134 if (!TLI.isLittleEndian())
4135 std::swap(Lo, Hi);
4136 break;
4137 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004138 case ISD::AND:
4139 case ISD::OR:
4140 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4141 SDOperand LL, LH, RL, RH;
4142 ExpandOp(Node->getOperand(0), LL, LH);
4143 ExpandOp(Node->getOperand(1), RL, RH);
4144 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4145 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4146 break;
4147 }
4148 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004149 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004150 ExpandOp(Node->getOperand(1), LL, LH);
4151 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00004152 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4153 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004154 break;
4155 }
Nate Begeman9373a812005-08-10 20:51:12 +00004156 case ISD::SELECT_CC: {
4157 SDOperand TL, TH, FL, FH;
4158 ExpandOp(Node->getOperand(2), TL, TH);
4159 ExpandOp(Node->getOperand(3), FL, FH);
4160 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4161 Node->getOperand(1), TL, FL, Node->getOperand(4));
4162 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4163 Node->getOperand(1), TH, FH, Node->getOperand(4));
4164 break;
4165 }
Nate Begeman144ff662005-10-13 17:15:37 +00004166 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004167 SDOperand Chain = Node->getOperand(0);
4168 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004169 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4170
4171 if (EVT == NVT)
4172 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4173 else
4174 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4175 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004176
4177 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004178 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004179
Nate Begeman144ff662005-10-13 17:15:37 +00004180 // The high part is obtained by SRA'ing all but one of the bits of the lo
4181 // part.
4182 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4183 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4184 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00004185 break;
4186 }
4187 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004188 SDOperand Chain = Node->getOperand(0);
4189 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004190 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4191
4192 if (EVT == NVT)
4193 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4194 else
4195 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4196 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004197
4198 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004199 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004200
Nate Begeman144ff662005-10-13 17:15:37 +00004201 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004202 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004203 break;
4204 }
4205 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004206 SDOperand Chain = Node->getOperand(0);
4207 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00004208 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4209
4210 if (EVT == NVT)
4211 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4212 else
4213 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4214 EVT);
4215
4216 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004217 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004218
4219 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004220 Hi = DAG.getNode(ISD::UNDEF, NVT);
4221 break;
4222 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004223 case ISD::ANY_EXTEND:
4224 // The low part is any extension of the input (which degenerates to a copy).
4225 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4226 // The high part is undefined.
4227 Hi = DAG.getNode(ISD::UNDEF, NVT);
4228 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004229 case ISD::SIGN_EXTEND: {
4230 // The low part is just a sign extension of the input (which degenerates to
4231 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004232 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004233
Chris Lattner3e928bb2005-01-07 07:47:09 +00004234 // The high part is obtained by SRA'ing all but one of the bits of the lo
4235 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004236 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004237 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4238 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004239 break;
4240 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004241 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004242 // The low part is just a zero extension of the input (which degenerates to
4243 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004244 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004245
Chris Lattner3e928bb2005-01-07 07:47:09 +00004246 // The high part is just a zero.
4247 Hi = DAG.getConstant(0, NVT);
4248 break;
Chris Lattner35481892005-12-23 00:16:34 +00004249
4250 case ISD::BIT_CONVERT: {
4251 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4252 Node->getOperand(0));
4253 ExpandOp(Tmp, Lo, Hi);
4254 break;
4255 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004256
Chris Lattner8137c9e2006-01-28 05:07:51 +00004257 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004258 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4259 TargetLowering::Custom &&
4260 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004261 Lo = TLI.LowerOperation(Op, DAG);
4262 assert(Lo.Val && "Node must be custom expanded!");
4263 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004264 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004265 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004266 break;
4267
Chris Lattner4e6c7462005-01-08 19:27:05 +00004268 // These operators cannot be expanded directly, emit them as calls to
4269 // library functions.
4270 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004271 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004272 SDOperand Op;
4273 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4274 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004275 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4276 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004277 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004278
Chris Lattnerf20d1832005-07-30 01:40:57 +00004279 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4280
Chris Lattner80a3e942005-07-29 00:33:32 +00004281 // Now that the custom expander is done, expand the result, which is still
4282 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004283 if (Op.Val) {
4284 ExpandOp(Op, Lo, Hi);
4285 break;
4286 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004287 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004288
Chris Lattner4e6c7462005-01-08 19:27:05 +00004289 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004290 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004291 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004292 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004293 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004294
Chris Lattner4e6c7462005-01-08 19:27:05 +00004295 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004296 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004297 SDOperand Op;
4298 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4299 case Expand: assert(0 && "cannot expand FP!");
4300 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4301 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4302 }
4303
4304 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4305
4306 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004307 if (Op.Val) {
4308 ExpandOp(Op, Lo, Hi);
4309 break;
4310 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004311 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004312
Chris Lattner4e6c7462005-01-08 19:27:05 +00004313 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004314 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004315 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004316 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004317 break;
4318
Evan Cheng05a2d562006-01-09 18:31:59 +00004319 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004320 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004321 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004322 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004323 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004324 Op = TLI.LowerOperation(Op, DAG);
4325 if (Op.Val) {
4326 // Now that the custom expander is done, expand the result, which is
4327 // still VT.
4328 ExpandOp(Op, Lo, Hi);
4329 break;
4330 }
4331 }
4332
Chris Lattnere34b3962005-01-19 04:19:40 +00004333 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004334 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004335 break;
Chris Lattner47599822005-04-02 03:38:53 +00004336
4337 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004338 TargetLowering::LegalizeAction Action =
4339 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4340 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4341 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004342 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004343 break;
4344 }
4345
Chris Lattnere34b3962005-01-19 04:19:40 +00004346 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004347 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004348 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004349 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004350
Evan Cheng05a2d562006-01-09 18:31:59 +00004351 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004352 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004353 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004354 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004355 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004356 Op = TLI.LowerOperation(Op, DAG);
4357 if (Op.Val) {
4358 // Now that the custom expander is done, expand the result, which is
4359 // still VT.
4360 ExpandOp(Op, Lo, Hi);
4361 break;
4362 }
4363 }
4364
Chris Lattnere34b3962005-01-19 04:19:40 +00004365 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004366 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004367 break;
Chris Lattner47599822005-04-02 03:38:53 +00004368
4369 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004370 TargetLowering::LegalizeAction Action =
4371 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4372 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4373 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004374 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004375 break;
4376 }
4377
Chris Lattnere34b3962005-01-19 04:19:40 +00004378 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004379 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004380 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004381 }
4382
4383 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004384 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004385 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004386 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004387 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004388 Op = TLI.LowerOperation(Op, DAG);
4389 if (Op.Val) {
4390 // Now that the custom expander is done, expand the result, which is
4391 // still VT.
4392 ExpandOp(Op, Lo, Hi);
4393 break;
4394 }
4395 }
4396
Chris Lattnere34b3962005-01-19 04:19:40 +00004397 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004398 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004399 break;
Chris Lattner47599822005-04-02 03:38:53 +00004400
4401 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004402 TargetLowering::LegalizeAction Action =
4403 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4404 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4405 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004406 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004407 break;
4408 }
4409
Chris Lattnere34b3962005-01-19 04:19:40 +00004410 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004411 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004412 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004413 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004414
Misha Brukmanedf128a2005-04-21 22:36:52 +00004415 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004416 case ISD::SUB: {
4417 // If the target wants to custom expand this, let them.
4418 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4419 TargetLowering::Custom) {
4420 Op = TLI.LowerOperation(Op, DAG);
4421 if (Op.Val) {
4422 ExpandOp(Op, Lo, Hi);
4423 break;
4424 }
4425 }
4426
4427 // Expand the subcomponents.
4428 SDOperand LHSL, LHSH, RHSL, RHSH;
4429 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4430 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004431 std::vector<MVT::ValueType> VTs;
4432 std::vector<SDOperand> LoOps, HiOps;
4433 VTs.push_back(LHSL.getValueType());
4434 VTs.push_back(MVT::Flag);
4435 LoOps.push_back(LHSL);
4436 LoOps.push_back(RHSL);
4437 HiOps.push_back(LHSH);
4438 HiOps.push_back(RHSH);
4439 if (Node->getOpcode() == ISD::ADD) {
4440 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4441 HiOps.push_back(Lo.getValue(1));
4442 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4443 } else {
4444 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4445 HiOps.push_back(Lo.getValue(1));
4446 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4447 }
Chris Lattner84f67882005-01-20 18:52:28 +00004448 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004449 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004450 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004451 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004452 SDOperand LL, LH, RL, RH;
4453 ExpandOp(Node->getOperand(0), LL, LH);
4454 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004455 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4456 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4457 // extended the sign bit of the low half through the upper half, and if so
4458 // emit a MULHS instead of the alternate sequence that is valid for any
4459 // i64 x i64 multiply.
4460 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4461 // is RH an extension of the sign bit of RL?
4462 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4463 RH.getOperand(1).getOpcode() == ISD::Constant &&
4464 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4465 // is LH an extension of the sign bit of LL?
4466 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4467 LH.getOperand(1).getOpcode() == ISD::Constant &&
4468 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4469 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4470 } else {
4471 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4472 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4473 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4474 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4475 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4476 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004477 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4478 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004479 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004480 }
4481 break;
4482 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004483 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4484 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4485 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4486 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004487 }
4488
Chris Lattner83397362005-12-21 18:02:52 +00004489 // Make sure the resultant values have been legalized themselves, unless this
4490 // is a type that requires multi-step expansion.
4491 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4492 Lo = LegalizeOp(Lo);
4493 Hi = LegalizeOp(Hi);
4494 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004495
4496 // Remember in a map if the values will be reused later.
4497 bool isNew =
4498 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4499 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004500}
4501
Chris Lattnerc7029802006-03-18 01:44:44 +00004502/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4503/// two smaller values of MVT::Vector type.
4504void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4505 SDOperand &Hi) {
4506 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4507 SDNode *Node = Op.Val;
4508 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4509 assert(NumElements > 1 && "Cannot split a single element vector!");
4510 unsigned NewNumElts = NumElements/2;
4511 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4512 SDOperand TypeNode = *(Node->op_end()-1);
4513
4514 // See if we already split it.
4515 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4516 = SplitNodes.find(Op);
4517 if (I != SplitNodes.end()) {
4518 Lo = I->second.first;
4519 Hi = I->second.second;
4520 return;
4521 }
4522
4523 switch (Node->getOpcode()) {
Chris Lattner7692eb42006-03-23 21:16:34 +00004524 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004525 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004526 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4527 LoOps.push_back(NewNumEltsNode);
4528 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004529 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004530
4531 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4532 HiOps.push_back(NewNumEltsNode);
4533 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004534 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004535 break;
4536 }
4537 case ISD::VADD:
4538 case ISD::VSUB:
4539 case ISD::VMUL:
4540 case ISD::VSDIV:
4541 case ISD::VUDIV:
4542 case ISD::VAND:
4543 case ISD::VOR:
4544 case ISD::VXOR: {
4545 SDOperand LL, LH, RL, RH;
4546 SplitVectorOp(Node->getOperand(0), LL, LH);
4547 SplitVectorOp(Node->getOperand(1), RL, RH);
4548
4549 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4550 NewNumEltsNode, TypeNode);
4551 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4552 NewNumEltsNode, TypeNode);
4553 break;
4554 }
4555 case ISD::VLOAD: {
4556 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4557 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4558 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4559
4560 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4561 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4562 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4563 getIntPtrConstant(IncrementSize));
4564 // FIXME: This creates a bogus srcvalue!
4565 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4566
4567 // Build a factor node to remember that this load is independent of the
4568 // other one.
4569 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4570 Hi.getValue(1));
4571
4572 // Remember that we legalized the chain.
4573 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00004574 break;
4575 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004576 case ISD::VBIT_CONVERT: {
4577 // We know the result is a vector. The input may be either a vector or a
4578 // scalar value.
4579 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4580 // Lower to a store/load. FIXME: this could be improved probably.
4581 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4582
4583 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4584 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4585 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4586 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4587 SplitVectorOp(St, Lo, Hi);
4588 } else {
4589 // If the input is a vector type, we have to either scalarize it, pack it
4590 // or convert it based on whether the input vector type is legal.
4591 SDNode *InVal = Node->getOperand(0).Val;
4592 unsigned NumElems =
4593 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4594 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4595
4596 // If the input is from a single element vector, scalarize the vector,
4597 // then treat like a scalar.
4598 if (NumElems == 1) {
4599 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4600 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4601 Op.getOperand(1), Op.getOperand(2));
4602 SplitVectorOp(Scalar, Lo, Hi);
4603 } else {
4604 // Split the input vector.
4605 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4606
4607 // Convert each of the pieces now.
4608 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4609 NewNumEltsNode, TypeNode);
4610 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4611 NewNumEltsNode, TypeNode);
4612 }
4613 break;
4614 }
4615 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004616 }
4617
4618 // Remember in a map if the values will be reused later.
4619 bool isNew =
4620 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4621 assert(isNew && "Value already expanded?!?");
4622}
4623
4624
4625/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4626/// equivalent operation that returns a scalar (e.g. F32) or packed value
4627/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4628/// type for the result.
4629SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4630 MVT::ValueType NewVT) {
4631 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4632 SDNode *Node = Op.Val;
4633
4634 // See if we already packed it.
4635 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4636 if (I != PackedNodes.end()) return I->second;
4637
4638 SDOperand Result;
4639 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004640 default:
4641 Node->dump(); std::cerr << "\n";
4642 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004643 case ISD::VADD:
4644 case ISD::VSUB:
4645 case ISD::VMUL:
4646 case ISD::VSDIV:
4647 case ISD::VUDIV:
4648 case ISD::VAND:
4649 case ISD::VOR:
4650 case ISD::VXOR:
4651 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4652 NewVT,
4653 PackVectorOp(Node->getOperand(0), NewVT),
4654 PackVectorOp(Node->getOperand(1), NewVT));
4655 break;
4656 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004657 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4658 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004659
Chris Lattner4794a6b2006-03-19 00:07:49 +00004660 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004661
4662 // Remember that we legalized the chain.
4663 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4664 break;
4665 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004666 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00004667 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004668 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004669 Result = Node->getOperand(0);
4670 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004671 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004672 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004673 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004674 }
4675 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004676 case ISD::VINSERT_VECTOR_ELT:
4677 if (!MVT::isVector(NewVT)) {
4678 // Returning a scalar? Must be the inserted element.
4679 Result = Node->getOperand(1);
4680 } else {
4681 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4682 PackVectorOp(Node->getOperand(0), NewVT),
4683 Node->getOperand(1), Node->getOperand(2));
4684 }
4685 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00004686 case ISD::VVECTOR_SHUFFLE:
4687 if (!MVT::isVector(NewVT)) {
4688 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4689 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4690 if (cast<ConstantSDNode>(EltNum)->getValue())
4691 Result = PackVectorOp(Node->getOperand(1), NewVT);
4692 else
4693 Result = PackVectorOp(Node->getOperand(0), NewVT);
4694 } else {
4695 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4696 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4697 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4698 Node->getOperand(2).Val->op_end()-2);
4699 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4700 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4701
4702 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4703 PackVectorOp(Node->getOperand(0), NewVT),
4704 PackVectorOp(Node->getOperand(1), NewVT), BV);
4705 }
4706 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004707 case ISD::VBIT_CONVERT:
4708 if (Op.getOperand(0).getValueType() != MVT::Vector)
4709 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4710 else {
4711 // If the input is a vector type, we have to either scalarize it, pack it
4712 // or convert it based on whether the input vector type is legal.
4713 SDNode *InVal = Node->getOperand(0).Val;
4714 unsigned NumElems =
4715 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4716 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4717
4718 // Figure out if there is a Packed type corresponding to this Vector
4719 // type. If so, convert to the packed type.
4720 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4721 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4722 // Turn this into a bit convert of the packed input.
4723 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4724 PackVectorOp(Node->getOperand(0), TVT));
4725 break;
4726 } else if (NumElems == 1) {
4727 // Turn this into a bit convert of the scalar input.
4728 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4729 PackVectorOp(Node->getOperand(0), EVT));
4730 break;
4731 } else {
4732 // FIXME: UNIMP!
4733 assert(0 && "Cast from unsupported vector type not implemented yet!");
4734 }
4735 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004736 }
4737
4738 if (TLI.isTypeLegal(NewVT))
4739 Result = LegalizeOp(Result);
4740 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4741 assert(isNew && "Value already packed?");
4742 return Result;
4743}
4744
Chris Lattner3e928bb2005-01-07 07:47:09 +00004745
4746// SelectionDAG::Legalize - This is the entry point for the file.
4747//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004748void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00004749 if (ViewLegalizeDAGs) viewGraph();
4750
Chris Lattner3e928bb2005-01-07 07:47:09 +00004751 /// run - This is the main entry point to this class.
4752 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004753 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004754}
4755