blob: 3799f7cc056e165091e64f7aa32387a7fc0938a6 [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.
Chris Lattner80edfb32006-04-17 22:10:08 +0000436 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000437 return false;
438}
439
Chris Lattnerc7029802006-03-18 01:44:44 +0000440/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
441/// appropriate for its type.
442void SelectionDAGLegalize::HandleOp(SDOperand Op) {
443 switch (getTypeAction(Op.getValueType())) {
444 default: assert(0 && "Bad type action!");
445 case Legal: LegalizeOp(Op); break;
446 case Promote: PromoteOp(Op); break;
447 case Expand:
448 if (Op.getValueType() != MVT::Vector) {
449 SDOperand X, Y;
450 ExpandOp(Op, X, Y);
451 } else {
452 SDNode *N = Op.Val;
453 unsigned NumOps = N->getNumOperands();
454 unsigned NumElements =
455 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
456 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
457 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
458 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
459 // In the common case, this is a legal vector type, convert it to the
460 // packed operation and type now.
461 PackVectorOp(Op, PackedVT);
462 } else if (NumElements == 1) {
463 // Otherwise, if this is a single element vector, convert it to a
464 // scalar operation.
465 PackVectorOp(Op, EVT);
466 } else {
467 // Otherwise, this is a multiple element vector that isn't supported.
468 // Split it in half and legalize both parts.
469 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000470 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000471 }
472 }
473 break;
474 }
475}
Chris Lattner6831a812006-02-13 09:18:02 +0000476
477
Chris Lattnerc7029802006-03-18 01:44:44 +0000478/// LegalizeOp - We know that the specified value has a legal type.
479/// Recursively ensure that the operands have legal types, then return the
480/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000481SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000482 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000483 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000484 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000485
Chris Lattner3e928bb2005-01-07 07:47:09 +0000486 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000487 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000488 if (Node->getNumValues() > 1) {
489 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000490 if (getTypeAction(Node->getValueType(i)) != Legal) {
491 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000492 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000493 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000494 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000495 }
496 }
497
Chris Lattner45982da2005-05-12 16:53:42 +0000498 // Note that LegalizeOp may be reentered even from single-use nodes, which
499 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000500 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
501 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000502
Nate Begeman9373a812005-08-10 20:51:12 +0000503 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000504 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000505 bool isCustom = false;
506
Chris Lattner3e928bb2005-01-07 07:47:09 +0000507 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000508 case ISD::FrameIndex:
509 case ISD::EntryToken:
510 case ISD::Register:
511 case ISD::BasicBlock:
512 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000513 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000514 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000515 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000516 case ISD::TargetConstantPool:
517 case ISD::TargetGlobalAddress:
518 case ISD::TargetExternalSymbol:
519 case ISD::VALUETYPE:
520 case ISD::SRCVALUE:
521 case ISD::STRING:
522 case ISD::CONDCODE:
523 // Primitives must all be legal.
524 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
525 "This must be legal!");
526 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000527 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000528 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
529 // If this is a target node, legalize it by legalizing the operands then
530 // passing it through.
531 std::vector<SDOperand> Ops;
532 bool Changed = false;
533 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
534 Ops.push_back(LegalizeOp(Node->getOperand(i)));
535 Changed = Changed || Node->getOperand(i) != Ops.back();
536 }
537 if (Changed)
538 if (Node->getNumValues() == 1)
539 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
540 else {
541 std::vector<MVT::ValueType> VTs(Node->value_begin(),
542 Node->value_end());
543 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
544 }
545
546 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
547 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
548 return Result.getValue(Op.ResNo);
549 }
550 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000551 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
552 assert(0 && "Do not know how to legalize this operator!");
553 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000554 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000555 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000556 case ISD::ConstantPool:
557 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000558 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
559 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000560 case TargetLowering::Custom:
561 Tmp1 = TLI.LowerOperation(Op, DAG);
562 if (Tmp1.Val) Result = Tmp1;
563 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000564 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000565 break;
566 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000567 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000568 case ISD::AssertSext:
569 case ISD::AssertZext:
570 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000571 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000572 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000573 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000574 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000575 Result = Node->getOperand(Op.ResNo);
576 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000577 case ISD::CopyFromReg:
578 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000579 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000580 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000582 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000583 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000584 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000585 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000586 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
587 } else {
588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
589 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000590 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
591 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000592 // Since CopyFromReg produces two values, make sure to remember that we
593 // legalized both of them.
594 AddLegalizedOperand(Op.getValue(0), Result);
595 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
596 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000597 case ISD::UNDEF: {
598 MVT::ValueType VT = Op.getValueType();
599 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000600 default: assert(0 && "This action is not supported yet!");
601 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000602 if (MVT::isInteger(VT))
603 Result = DAG.getConstant(0, VT);
604 else if (MVT::isFloatingPoint(VT))
605 Result = DAG.getConstantFP(0, VT);
606 else
607 assert(0 && "Unknown value type!");
608 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000609 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000610 break;
611 }
612 break;
613 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000614
Chris Lattner48b61a72006-03-28 00:40:33 +0000615 case ISD::INTRINSIC_W_CHAIN:
616 case ISD::INTRINSIC_WO_CHAIN:
617 case ISD::INTRINSIC_VOID: {
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000618 std::vector<SDOperand> Ops;
619 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
620 Ops.push_back(LegalizeOp(Node->getOperand(i)));
621 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner10d7fa62006-03-26 09:12:51 +0000622
623 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000624 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000625 TargetLowering::Custom) {
626 Tmp3 = TLI.LowerOperation(Result, DAG);
627 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000628 }
629
630 if (Result.Val->getNumValues() == 1) break;
631
632 // Must have return value and chain result.
633 assert(Result.Val->getNumValues() == 2 &&
634 "Cannot return more than two values!");
635
636 // Since loads produce two values, make sure to remember that we
637 // legalized both of them.
638 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
639 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
640 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000641 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000642
643 case ISD::LOCATION:
644 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
646
647 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
648 case TargetLowering::Promote:
649 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000650 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000651 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000652 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
653 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
654
655 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
656 const std::string &FName =
657 cast<StringSDNode>(Node->getOperand(3))->getValue();
658 const std::string &DirName =
659 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000660 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000661
Jim Laskeye81aecb2005-12-21 20:51:37 +0000662 std::vector<SDOperand> Ops;
663 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000664 SDOperand LineOp = Node->getOperand(1);
665 SDOperand ColOp = Node->getOperand(2);
666
667 if (useDEBUG_LOC) {
668 Ops.push_back(LineOp); // line #
669 Ops.push_back(ColOp); // col #
670 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
671 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
672 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000673 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
674 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000675 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
676 Ops.push_back(DAG.getConstant(ID, MVT::i32));
677 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
678 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000679 } else {
680 Result = Tmp1; // chain
681 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000682 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000683 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000684 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000685 if (Tmp1 != Node->getOperand(0) ||
686 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000687 std::vector<SDOperand> Ops;
688 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000689 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
690 Ops.push_back(Node->getOperand(1)); // line # must be legal.
691 Ops.push_back(Node->getOperand(2)); // col # must be legal.
692 } else {
693 // Otherwise promote them.
694 Ops.push_back(PromoteOp(Node->getOperand(1)));
695 Ops.push_back(PromoteOp(Node->getOperand(2)));
696 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000697 Ops.push_back(Node->getOperand(3)); // filename must be legal.
698 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000699 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000700 }
701 break;
702 }
703 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000704
705 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000706 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000707 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000708 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000709 case TargetLowering::Legal:
710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
711 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
712 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
713 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000714 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000715 break;
716 }
717 break;
718
719 case ISD::DEBUG_LABEL:
720 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
721 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000722 default: assert(0 && "This action is not supported yet!");
723 case TargetLowering::Legal:
724 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
725 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000727 break;
728 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000729 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000730
Chris Lattner3e928bb2005-01-07 07:47:09 +0000731 case ISD::Constant:
732 // We know we don't need to expand constants here, constants only have one
733 // value and we check that it is fine above.
734
735 // FIXME: Maybe we should handle things like targets that don't support full
736 // 32-bit immediates?
737 break;
738 case ISD::ConstantFP: {
739 // Spill FP immediates to the constant pool if the target cannot directly
740 // codegen them. Targets often have some immediate values that can be
741 // efficiently generated into an FP register without a load. We explicitly
742 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000743 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
744
745 // Check to see if this FP immediate is already legal.
746 bool isLegal = false;
747 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
748 E = TLI.legal_fpimm_end(); I != E; ++I)
749 if (CFP->isExactlyValue(*I)) {
750 isLegal = true;
751 break;
752 }
753
Chris Lattner3181a772006-01-29 06:26:56 +0000754 // If this is a legal constant, turn it into a TargetConstantFP node.
755 if (isLegal) {
756 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
757 break;
758 }
759
760 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
761 default: assert(0 && "This action is not supported yet!");
762 case TargetLowering::Custom:
763 Tmp3 = TLI.LowerOperation(Result, DAG);
764 if (Tmp3.Val) {
765 Result = Tmp3;
766 break;
767 }
768 // FALLTHROUGH
769 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000770 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000771 bool Extend = false;
772
Chris Lattner456a93a2006-01-28 07:39:30 +0000773 // If a FP immediate is precise when represented as a float and if the
774 // target can do an extending load from float to double, we put it into
775 // the constant pool as a float, even if it's is statically typed as a
776 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000777 MVT::ValueType VT = CFP->getValueType(0);
778 bool isDouble = VT == MVT::f64;
779 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
780 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000781 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
782 // Only do this if the target has a native EXTLOAD instruction from
783 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000784 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000785 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
786 VT = MVT::f32;
787 Extend = true;
788 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000789
Chris Lattner456a93a2006-01-28 07:39:30 +0000790 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000791 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000792 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
793 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000794 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000795 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
796 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000797 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000798 }
799 break;
800 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000801 case ISD::TokenFactor:
802 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000803 Tmp1 = LegalizeOp(Node->getOperand(0));
804 Tmp2 = LegalizeOp(Node->getOperand(1));
805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
806 } else if (Node->getNumOperands() == 3) {
807 Tmp1 = LegalizeOp(Node->getOperand(0));
808 Tmp2 = LegalizeOp(Node->getOperand(1));
809 Tmp3 = LegalizeOp(Node->getOperand(2));
810 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000811 } else {
812 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000813 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
816 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000817 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000818 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000819
820 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000821 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000822 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000823 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
824 assert(Tmp3.Val && "Target didn't custom lower this node!");
825 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
826 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000827
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000828 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000829 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000830 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
831 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000832 if (Op.ResNo == i)
833 Tmp2 = Tmp1;
834 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
835 }
836 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000837
Chris Lattnerb2827b02006-03-19 00:52:58 +0000838 case ISD::BUILD_VECTOR:
839 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000840 default: assert(0 && "This action is not supported yet!");
841 case TargetLowering::Custom:
842 Tmp3 = TLI.LowerOperation(Result, DAG);
843 if (Tmp3.Val) {
844 Result = Tmp3;
845 break;
846 }
847 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000848 case TargetLowering::Expand:
849 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000850 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000851 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000852 break;
853 case ISD::INSERT_VECTOR_ELT:
854 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
855 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
856 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
858
859 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
860 Node->getValueType(0))) {
861 default: assert(0 && "This action is not supported yet!");
862 case TargetLowering::Legal:
863 break;
864 case TargetLowering::Custom:
865 Tmp3 = TLI.LowerOperation(Result, DAG);
866 if (Tmp3.Val) {
867 Result = Tmp3;
868 break;
869 }
870 // FALLTHROUGH
871 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000872 // If the insert index is a constant, codegen this as a scalar_to_vector,
873 // then a shuffle that inserts it into the right position in the vector.
874 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
875 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
876 Tmp1.getValueType(), Tmp2);
877
878 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
879 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
880 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
881
882 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
883 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
884 // the RHS.
885 std::vector<SDOperand> ShufOps;
886 for (unsigned i = 0; i != NumElts; ++i) {
887 if (i != InsertPos->getValue())
888 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
889 else
890 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
891 }
892 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,ShufOps);
893
894 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
895 Tmp1, ScVec, ShufMask);
896 Result = LegalizeOp(Result);
897 break;
898 }
899
Chris Lattner2332b9f2006-03-19 01:17:20 +0000900 // If the target doesn't support this, we have to spill the input vector
901 // to a temporary stack slot, update the element, then reload it. This is
902 // badness. We could also load the value into a vector register (either
903 // with a "move to register" or "extload into register" instruction, then
904 // permute it into place, if the idx is a constant and if the idx is
905 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000906 MVT::ValueType VT = Tmp1.getValueType();
907 MVT::ValueType EltVT = Tmp2.getValueType();
908 MVT::ValueType IdxVT = Tmp3.getValueType();
909 MVT::ValueType PtrVT = TLI.getPointerTy();
910 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000911 // Store the vector.
912 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
913 Tmp1, StackPtr, DAG.getSrcValue(NULL));
914
915 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000916 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
917 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000918 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000919 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
920 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
921 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000922 // Store the scalar value.
923 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
924 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
925 // Load the updated vector.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000926 Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL));
Chris Lattner2332b9f2006-03-19 01:17:20 +0000927 break;
928 }
929 }
930 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000931 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000932 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
933 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
934 break;
935 }
936
Chris Lattnerce872152006-03-19 06:31:19 +0000937 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
938 Result = DAG.UpdateNodeOperands(Result, Tmp1);
939 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
940 Node->getValueType(0))) {
941 default: assert(0 && "This action is not supported yet!");
942 case TargetLowering::Legal:
943 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000944 case TargetLowering::Custom:
945 Tmp3 = TLI.LowerOperation(Result, DAG);
946 if (Tmp3.Val) {
947 Result = Tmp3;
948 break;
949 }
950 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000951 case TargetLowering::Expand:
952 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +0000953 break;
954 }
Chris Lattnerce872152006-03-19 06:31:19 +0000955 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000956 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +0000957 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
958 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
959 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
960
961 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +0000962 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
963 default: assert(0 && "Unknown operation action!");
964 case TargetLowering::Legal:
965 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
966 "vector shuffle should not be created if not legal!");
967 break;
968 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +0000969 Tmp3 = TLI.LowerOperation(Result, DAG);
970 if (Tmp3.Val) {
971 Result = Tmp3;
972 break;
973 }
974 // FALLTHROUGH
975 case TargetLowering::Expand: {
976 MVT::ValueType VT = Node->getValueType(0);
977 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
978 MVT::ValueType PtrVT = TLI.getPointerTy();
979 SDOperand Mask = Node->getOperand(2);
980 unsigned NumElems = Mask.getNumOperands();
981 std::vector<SDOperand> Ops;
982 for (unsigned i = 0; i != NumElems; ++i) {
983 SDOperand Arg = Mask.getOperand(i);
984 if (Arg.getOpcode() == ISD::UNDEF) {
985 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
986 } else {
987 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
988 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
989 if (Idx < NumElems)
990 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
991 DAG.getConstant(Idx, PtrVT)));
992 else
993 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
994 DAG.getConstant(Idx - NumElems, PtrVT)));
995 }
996 }
997 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
Chris Lattner4352cc92006-04-04 17:23:26 +0000998 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +0000999 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001000 case TargetLowering::Promote: {
1001 // Change base type to a different vector type.
1002 MVT::ValueType OVT = Node->getValueType(0);
1003 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1004
1005 // Cast the two input vectors.
1006 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1007 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1008
1009 // Convert the shuffle mask to the right # elements.
1010 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1011 assert(Tmp3.Val && "Shuffle not legal?");
1012 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1013 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1014 break;
1015 }
Chris Lattner87100e02006-03-20 01:52:29 +00001016 }
1017 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001018
1019 case ISD::EXTRACT_VECTOR_ELT:
1020 Tmp1 = LegalizeOp(Node->getOperand(0));
1021 Tmp2 = LegalizeOp(Node->getOperand(1));
1022 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001023
1024 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1025 Tmp1.getValueType())) {
1026 default: assert(0 && "This action is not supported yet!");
1027 case TargetLowering::Legal:
1028 break;
1029 case TargetLowering::Custom:
1030 Tmp3 = TLI.LowerOperation(Result, DAG);
1031 if (Tmp3.Val) {
1032 Result = Tmp3;
1033 break;
1034 }
1035 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001036 case TargetLowering::Expand:
1037 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001038 break;
1039 }
Chris Lattner384504c2006-03-21 20:44:12 +00001040 break;
1041
Chris Lattner15972212006-03-31 17:55:51 +00001042 case ISD::VEXTRACT_VECTOR_ELT:
1043 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001044 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001045
Chris Lattner6831a812006-02-13 09:18:02 +00001046 case ISD::CALLSEQ_START: {
1047 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1048
1049 // Recursively Legalize all of the inputs of the call end that do not lead
1050 // to this call start. This ensures that any libcalls that need be inserted
1051 // are inserted *before* the CALLSEQ_START.
1052 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1053 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
1054
1055 // Now that we legalized all of the inputs (which may have inserted
1056 // libcalls) create the new CALLSEQ_START node.
1057 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1058
1059 // Merge in the last call, to ensure that this call start after the last
1060 // call ended.
Chris Lattnerb248e162006-05-17 17:55:45 +00001061 if (LastCALLSEQ_END.getOpcode() != ISD::EntryNode) {
1062 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1063 Tmp1 = LegalizeOp(Tmp1);
1064 }
Chris Lattner6831a812006-02-13 09:18:02 +00001065
1066 // Do not try to legalize the target-specific arguments (#1+).
1067 if (Tmp1 != Node->getOperand(0)) {
1068 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1069 Ops[0] = Tmp1;
1070 Result = DAG.UpdateNodeOperands(Result, Ops);
1071 }
1072
1073 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001074 AddLegalizedOperand(Op.getValue(0), Result);
1075 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1076 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1077
Chris Lattner6831a812006-02-13 09:18:02 +00001078 // Now that the callseq_start and all of the non-call nodes above this call
1079 // sequence have been legalized, legalize the call itself. During this
1080 // process, no libcalls can/will be inserted, guaranteeing that no calls
1081 // can overlap.
1082 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1083 SDOperand InCallSEQ = LastCALLSEQ_END;
1084 // Note that we are selecting this call!
1085 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1086 IsLegalizingCall = true;
1087
1088 // Legalize the call, starting from the CALLSEQ_END.
1089 LegalizeOp(LastCALLSEQ_END);
1090 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1091 return Result;
1092 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001093 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001094 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1095 // will cause this node to be legalized as well as handling libcalls right.
1096 if (LastCALLSEQ_END.Val != Node) {
1097 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1098 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1099 assert(I != LegalizedNodes.end() &&
1100 "Legalizing the call start should have legalized this node!");
1101 return I->second;
1102 }
1103
1104 // Otherwise, the call start has been legalized and everything is going
1105 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001106 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001107 // Do not try to legalize the target-specific arguments (#1+), except for
1108 // an optional flag input.
1109 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1110 if (Tmp1 != Node->getOperand(0)) {
1111 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1112 Ops[0] = Tmp1;
1113 Result = DAG.UpdateNodeOperands(Result, Ops);
1114 }
1115 } else {
1116 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1117 if (Tmp1 != Node->getOperand(0) ||
1118 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1119 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1120 Ops[0] = Tmp1;
1121 Ops.back() = Tmp2;
1122 Result = DAG.UpdateNodeOperands(Result, Ops);
1123 }
Chris Lattner6a542892006-01-24 05:48:21 +00001124 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001125 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001126 // This finishes up call legalization.
1127 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001128
1129 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1130 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1131 if (Node->getNumValues() == 2)
1132 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1133 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001134 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001135 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1136 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1137 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001138 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001139
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001140 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001141 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001142 switch (TLI.getOperationAction(Node->getOpcode(),
1143 Node->getValueType(0))) {
1144 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001145 case TargetLowering::Expand: {
1146 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1147 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1148 " not tell us which reg is the stack pointer!");
1149 SDOperand Chain = Tmp1.getOperand(0);
1150 SDOperand Size = Tmp2.getOperand(1);
1151 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1152 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1153 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001154 Tmp1 = LegalizeOp(Tmp1);
1155 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001156 break;
1157 }
1158 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001159 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1160 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001161 Tmp1 = LegalizeOp(Tmp3);
1162 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001163 }
Chris Lattner903d2782006-01-15 08:54:32 +00001164 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001165 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001166 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001167 }
Chris Lattner903d2782006-01-15 08:54:32 +00001168 // Since this op produce two values, make sure to remember that we
1169 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001170 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1171 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001172 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001173 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001174 case ISD::INLINEASM:
1175 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1176 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001177 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +00001178 Tmp2 = Tmp3 = SDOperand(0, 0);
1179 else
1180 Tmp3 = LegalizeOp(Tmp2);
1181
1182 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1183 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1184 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001185 if (Tmp3.Val) Ops.back() = Tmp3;
1186 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001187 }
1188
1189 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001190 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001191 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1192 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001193 case ISD::BR:
1194 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001195 // Ensure that libcalls are emitted before a branch.
1196 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1197 Tmp1 = LegalizeOp(Tmp1);
1198 LastCALLSEQ_END = DAG.getEntryNode();
1199
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001200 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001201 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001202 case ISD::BRIND:
1203 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1204 // Ensure that libcalls are emitted before a branch.
1205 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1206 Tmp1 = LegalizeOp(Tmp1);
1207 LastCALLSEQ_END = DAG.getEntryNode();
1208
1209 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1210 default: assert(0 && "Indirect target must be legal type (pointer)!");
1211 case Legal:
1212 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1213 break;
1214 }
1215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1216 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001217 case ISD::BRCOND:
1218 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001219 // Ensure that libcalls are emitted before a return.
1220 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1221 Tmp1 = LegalizeOp(Tmp1);
1222 LastCALLSEQ_END = DAG.getEntryNode();
1223
Chris Lattner47e92232005-01-18 19:27:06 +00001224 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1225 case Expand: assert(0 && "It's impossible to expand bools");
1226 case Legal:
1227 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1228 break;
1229 case Promote:
1230 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1231 break;
1232 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001233
1234 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001235 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001236
1237 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1238 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001239 case TargetLowering::Legal: break;
1240 case TargetLowering::Custom:
1241 Tmp1 = TLI.LowerOperation(Result, DAG);
1242 if (Tmp1.Val) Result = Tmp1;
1243 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001244 case TargetLowering::Expand:
1245 // Expand brcond's setcc into its constituent parts and create a BR_CC
1246 // Node.
1247 if (Tmp2.getOpcode() == ISD::SETCC) {
1248 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1249 Tmp2.getOperand(0), Tmp2.getOperand(1),
1250 Node->getOperand(2));
1251 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001252 // Make sure the condition is either zero or one. It may have been
1253 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001254 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1255 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1256 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001257
Nate Begeman7cbd5252005-08-16 19:49:35 +00001258 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1259 DAG.getCondCode(ISD::SETNE), Tmp2,
1260 DAG.getConstant(0, Tmp2.getValueType()),
1261 Node->getOperand(2));
1262 }
1263 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001264 }
1265 break;
1266 case ISD::BR_CC:
1267 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001268 // Ensure that libcalls are emitted before a branch.
1269 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1270 Tmp1 = LegalizeOp(Tmp1);
1271 LastCALLSEQ_END = DAG.getEntryNode();
1272
Nate Begeman750ac1b2006-02-01 07:19:44 +00001273 Tmp2 = Node->getOperand(2); // LHS
1274 Tmp3 = Node->getOperand(3); // RHS
1275 Tmp4 = Node->getOperand(1); // CC
1276
1277 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1278
1279 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1280 // the LHS is a legal SETCC itself. In this case, we need to compare
1281 // the result against zero to select between true and false values.
1282 if (Tmp3.Val == 0) {
1283 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1284 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001285 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001286
1287 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1288 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001289
Chris Lattner181b7a32005-12-17 23:46:46 +00001290 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1291 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001292 case TargetLowering::Legal: break;
1293 case TargetLowering::Custom:
1294 Tmp4 = TLI.LowerOperation(Result, DAG);
1295 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001296 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001297 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001298 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001299 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001300 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1301 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001302
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001303 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001305 Tmp3 = Result.getValue(0);
1306 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001307
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001308 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1309 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001310 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001311 case TargetLowering::Custom:
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001312 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001313 if (Tmp1.Val) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001314 Tmp3 = LegalizeOp(Tmp1);
1315 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001316 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001317 break;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001318 case TargetLowering::Promote: {
1319 // Only promote a load of vector type to another.
1320 assert(MVT::isVector(VT) && "Cannot promote this load!");
1321 // Change base type to a different vector type.
1322 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1323
1324 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, Node->getOperand(2));
1325 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1326 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1327 break;
1328 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001329 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001330 // Since loads produce two values, make sure to remember that we
1331 // legalized both of them.
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001332 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1333 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1334 return Op.ResNo ? Tmp4 : Tmp3;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001335 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001336 case ISD::EXTLOAD:
1337 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001338 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001341
Chris Lattner5f056bf2005-07-10 01:55:33 +00001342 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001343 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001344 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001345 case TargetLowering::Promote:
1346 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001347 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1348 DAG.getValueType(MVT::i8));
1349 Tmp1 = Result.getValue(0);
1350 Tmp2 = Result.getValue(1);
1351 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001352 case TargetLowering::Custom:
1353 isCustom = true;
1354 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001355 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001356 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1357 Node->getOperand(3));
1358 Tmp1 = Result.getValue(0);
1359 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001360
1361 if (isCustom) {
1362 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1363 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001364 Tmp1 = LegalizeOp(Tmp3);
1365 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001366 }
1367 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001368 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001369 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001370 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001371 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1372 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001373 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001374 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1375 Tmp2 = LegalizeOp(Load.getValue(1));
1376 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001377 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001378 assert(Node->getOpcode() != ISD::EXTLOAD &&
1379 "EXTLOAD should always be supported!");
1380 // Turn the unsupported load into an EXTLOAD followed by an explicit
1381 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001382 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1383 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001384 SDOperand ValRes;
1385 if (Node->getOpcode() == ISD::SEXTLOAD)
1386 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001387 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001388 else
1389 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001390 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1391 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1392 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001393 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001394 // Since loads produce two values, make sure to remember that we legalized
1395 // both of them.
1396 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1397 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1398 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001399 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001400 case ISD::EXTRACT_ELEMENT: {
1401 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1402 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001403 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001404 case Legal:
1405 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1406 // 1 -> Hi
1407 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1408 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1409 TLI.getShiftAmountTy()));
1410 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1411 } else {
1412 // 0 -> Lo
1413 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1414 Node->getOperand(0));
1415 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001416 break;
1417 case Expand:
1418 // Get both the low and high parts.
1419 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1420 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1421 Result = Tmp2; // 1 -> Hi
1422 else
1423 Result = Tmp1; // 0 -> Lo
1424 break;
1425 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001426 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001427 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001428
1429 case ISD::CopyToReg:
1430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001431
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001432 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001433 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001434 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001435 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001436 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001438 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001439 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001440 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001441 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1443 Tmp3);
1444 } else {
1445 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001446 }
1447
1448 // Since this produces two values, make sure to remember that we legalized
1449 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001450 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001451 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001452 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001453 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001454 break;
1455
1456 case ISD::RET:
1457 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001458
1459 // Ensure that libcalls are emitted before a return.
1460 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1461 Tmp1 = LegalizeOp(Tmp1);
1462 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001463
Chris Lattner3e928bb2005-01-07 07:47:09 +00001464 switch (Node->getNumOperands()) {
1465 case 2: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001466 Tmp2 = Node->getOperand(1);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001467 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001468 case Legal:
Chris Lattnerf87324e2006-04-11 01:31:51 +00001469 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001470 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001471 case Expand:
1472 if (Tmp2.getValueType() != MVT::Vector) {
1473 SDOperand Lo, Hi;
1474 ExpandOp(Tmp2, Lo, Hi);
1475 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1476 } else {
1477 SDNode *InVal = Tmp2.Val;
1478 unsigned NumElems =
1479 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1480 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1481
1482 // Figure out if there is a Packed type corresponding to this Vector
1483 // type. If so, convert to the packed type.
1484 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1485 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1486 // Turn this into a return of the packed type.
1487 Tmp2 = PackVectorOp(Tmp2, TVT);
1488 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1489 } else if (NumElems == 1) {
1490 // Turn this into a return of the scalar type.
1491 Tmp2 = PackVectorOp(Tmp2, EVT);
1492 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001493
1494 // FIXME: Returns of gcc generic vectors smaller than a legal type
1495 // should be returned in integer registers!
1496
Chris Lattnerf87324e2006-04-11 01:31:51 +00001497 // The scalarized value type may not be legal, e.g. it might require
1498 // promotion or expansion. Relegalize the return.
1499 Result = LegalizeOp(Result);
1500 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001501 // FIXME: Returns of gcc generic vectors larger than a legal vector
1502 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001503 SDOperand Lo, Hi;
1504 SplitVectorOp(Tmp2, Lo, Hi);
1505 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1506 Result = LegalizeOp(Result);
1507 }
1508 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001509 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001510 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001511 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1513 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001514 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001515 }
1516 break;
1517 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001518 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001519 break;
1520 default: { // ret <values>
1521 std::vector<SDOperand> NewValues;
1522 NewValues.push_back(Tmp1);
1523 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1524 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1525 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001526 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 break;
1528 case Expand: {
1529 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001530 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1531 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001532 ExpandOp(Node->getOperand(i), Lo, Hi);
1533 NewValues.push_back(Lo);
1534 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001535 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001536 }
1537 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001538 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001539 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001540
1541 if (NewValues.size() == Node->getNumOperands())
1542 Result = DAG.UpdateNodeOperands(Result, NewValues);
1543 else
1544 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001545 break;
1546 }
1547 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001548
Chris Lattner6862dbc2006-01-29 21:02:23 +00001549 if (Result.getOpcode() == ISD::RET) {
1550 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1551 default: assert(0 && "This action is not supported yet!");
1552 case TargetLowering::Legal: break;
1553 case TargetLowering::Custom:
1554 Tmp1 = TLI.LowerOperation(Result, DAG);
1555 if (Tmp1.Val) Result = Tmp1;
1556 break;
1557 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001558 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001559 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001560 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1562 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1563
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001564 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001565 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001566 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001567 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001568 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001569 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001570 } else {
1571 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001572 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001573 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001574 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1575 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001576 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001577 }
1578
Chris Lattner3e928bb2005-01-07 07:47:09 +00001579 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1580 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001581 Tmp3 = LegalizeOp(Node->getOperand(1));
1582 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1583 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001584
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001585 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001586 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1587 default: assert(0 && "This action is not supported yet!");
1588 case TargetLowering::Legal: break;
1589 case TargetLowering::Custom:
1590 Tmp1 = TLI.LowerOperation(Result, DAG);
1591 if (Tmp1.Val) Result = Tmp1;
1592 break;
Chris Lattner2efce0a2006-04-16 01:36:45 +00001593 case TargetLowering::Promote:
1594 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1595 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1596 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1597 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1598 Node->getOperand(3));
1599 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001600 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001601 break;
1602 }
1603 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001604 // Truncate the value and store the result.
1605 Tmp3 = PromoteOp(Node->getOperand(1));
1606 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001607 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001608 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001609 break;
1610
Chris Lattner3e928bb2005-01-07 07:47:09 +00001611 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001612 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001613 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001614
1615 // If this is a vector type, then we have to calculate the increment as
1616 // the product of the element size in bytes, and the number of elements
1617 // in the high half of the vector.
1618 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1619 SDNode *InVal = Node->getOperand(1).Val;
1620 unsigned NumElems =
1621 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1622 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1623
1624 // Figure out if there is a Packed type corresponding to this Vector
1625 // type. If so, convert to the packed type.
1626 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1627 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1628 // Turn this into a normal store of the packed type.
1629 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1630 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1631 Node->getOperand(3));
Chris Lattner2efce0a2006-04-16 01:36:45 +00001632 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001633 break;
1634 } else if (NumElems == 1) {
1635 // Turn this into a normal store of the scalar type.
1636 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1638 Node->getOperand(3));
Chris Lattner2ae2e982006-03-31 17:37:22 +00001639 // The scalarized value type may not be legal, e.g. it might require
1640 // promotion or expansion. Relegalize the scalar store.
1641 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001642 break;
1643 } else {
1644 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1645 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1646 }
1647 } else {
1648 ExpandOp(Node->getOperand(1), Lo, Hi);
1649 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001650
Chris Lattnerd9731af2006-03-31 18:20:46 +00001651 if (!TLI.isLittleEndian())
1652 std::swap(Lo, Hi);
1653 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001654
Chris Lattneredb1add2005-05-11 04:51:16 +00001655 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1656 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001657 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1658 getIntPtrConstant(IncrementSize));
1659 assert(isTypeLegal(Tmp2.getValueType()) &&
1660 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001661 // FIXME: This sets the srcvalue of both halves to be the same, which is
1662 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001663 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1664 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001665 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1666 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001667 }
1668 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001669 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001670 case ISD::PCMARKER:
1671 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001673 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001674 case ISD::STACKSAVE:
1675 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001676 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1677 Tmp1 = Result.getValue(0);
1678 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001679
Chris Lattner140d53c2006-01-13 02:50:02 +00001680 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1681 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001682 case TargetLowering::Legal: break;
1683 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001684 Tmp3 = TLI.LowerOperation(Result, DAG);
1685 if (Tmp3.Val) {
1686 Tmp1 = LegalizeOp(Tmp3);
1687 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001688 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001689 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001690 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001691 // Expand to CopyFromReg if the target set
1692 // StackPointerRegisterToSaveRestore.
1693 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001694 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001695 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001696 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001697 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001698 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1699 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001700 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001701 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001702 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001703
1704 // Since stacksave produce two values, make sure to remember that we
1705 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001706 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1707 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1708 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001709
Chris Lattner140d53c2006-01-13 02:50:02 +00001710 case ISD::STACKRESTORE:
1711 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1712 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001713 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001714
1715 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1716 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001717 case TargetLowering::Legal: break;
1718 case TargetLowering::Custom:
1719 Tmp1 = TLI.LowerOperation(Result, DAG);
1720 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001721 break;
1722 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001723 // Expand to CopyToReg if the target set
1724 // StackPointerRegisterToSaveRestore.
1725 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1726 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1727 } else {
1728 Result = Tmp1;
1729 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001730 break;
1731 }
1732 break;
1733
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001734 case ISD::READCYCLECOUNTER:
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001736 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001737
1738 // Since rdcc produce two values, make sure to remember that we legalized
1739 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001740 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001741 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001742 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001743
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001744 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001745 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1746 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1747
Chris Lattner456a93a2006-01-28 07:39:30 +00001748 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1749 "Cannot handle illegal TRUNCSTORE yet!");
1750 Tmp2 = LegalizeOp(Node->getOperand(1));
1751
1752 // The only promote case we handle is TRUNCSTORE:i1 X into
1753 // -> TRUNCSTORE:i8 (and X, 1)
1754 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1755 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1756 TargetLowering::Promote) {
1757 // Promote the bool to a mask then store.
1758 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1759 DAG.getConstant(1, Tmp2.getValueType()));
1760 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1761 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001762
Chris Lattner456a93a2006-01-28 07:39:30 +00001763 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1764 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001765 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1766 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001767 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001768
Chris Lattner456a93a2006-01-28 07:39:30 +00001769 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1770 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1771 default: assert(0 && "This action is not supported yet!");
1772 case TargetLowering::Legal: break;
1773 case TargetLowering::Custom:
1774 Tmp1 = TLI.LowerOperation(Result, DAG);
1775 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001776 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001777 }
1778 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001779 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001780 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001781 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1782 case Expand: assert(0 && "It's impossible to expand bools");
1783 case Legal:
1784 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1785 break;
1786 case Promote:
1787 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1788 break;
1789 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001790 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001791 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001792
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001793 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001794
Nate Begemanb942a3d2005-08-23 04:29:48 +00001795 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001796 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001797 case TargetLowering::Legal: break;
1798 case TargetLowering::Custom: {
1799 Tmp1 = TLI.LowerOperation(Result, DAG);
1800 if (Tmp1.Val) Result = Tmp1;
1801 break;
1802 }
Nate Begeman9373a812005-08-10 20:51:12 +00001803 case TargetLowering::Expand:
1804 if (Tmp1.getOpcode() == ISD::SETCC) {
1805 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1806 Tmp2, Tmp3,
1807 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1808 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001809 // Make sure the condition is either zero or one. It may have been
1810 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001811 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1812 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1813 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001814 Result = DAG.getSelectCC(Tmp1,
1815 DAG.getConstant(0, Tmp1.getValueType()),
1816 Tmp2, Tmp3, ISD::SETNE);
1817 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001818 break;
1819 case TargetLowering::Promote: {
1820 MVT::ValueType NVT =
1821 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1822 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001823 if (MVT::isVector(Tmp2.getValueType())) {
1824 ExtOp = ISD::BIT_CONVERT;
1825 TruncOp = ISD::BIT_CONVERT;
1826 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001827 ExtOp = ISD::ANY_EXTEND;
1828 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001829 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001830 ExtOp = ISD::FP_EXTEND;
1831 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001832 }
1833 // Promote each of the values to the new type.
1834 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1835 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1836 // Perform the larger operation, then round down.
1837 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1838 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1839 break;
1840 }
1841 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001842 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001843 case ISD::SELECT_CC: {
1844 Tmp1 = Node->getOperand(0); // LHS
1845 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001846 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1847 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001848 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001849
Nate Begeman750ac1b2006-02-01 07:19:44 +00001850 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1851
1852 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1853 // the LHS is a legal SETCC itself. In this case, we need to compare
1854 // the result against zero to select between true and false values.
1855 if (Tmp2.Val == 0) {
1856 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1857 CC = DAG.getCondCode(ISD::SETNE);
1858 }
1859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1860
1861 // Everything is legal, see if we should expand this op or something.
1862 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1863 default: assert(0 && "This action is not supported yet!");
1864 case TargetLowering::Legal: break;
1865 case TargetLowering::Custom:
1866 Tmp1 = TLI.LowerOperation(Result, DAG);
1867 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001868 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001869 }
1870 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001871 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001872 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001873 Tmp1 = Node->getOperand(0);
1874 Tmp2 = Node->getOperand(1);
1875 Tmp3 = Node->getOperand(2);
1876 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1877
1878 // If we had to Expand the SetCC operands into a SELECT node, then it may
1879 // not always be possible to return a true LHS & RHS. In this case, just
1880 // return the value we legalized, returned in the LHS
1881 if (Tmp2.Val == 0) {
1882 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001883 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001884 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001885
Chris Lattner73e142f2006-01-30 22:43:50 +00001886 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001887 default: assert(0 && "Cannot handle this action for SETCC yet!");
1888 case TargetLowering::Custom:
1889 isCustom = true;
1890 // FALLTHROUGH.
1891 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001892 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001893 if (isCustom) {
1894 Tmp3 = TLI.LowerOperation(Result, DAG);
1895 if (Tmp3.Val) Result = Tmp3;
1896 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001897 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001898 case TargetLowering::Promote: {
1899 // First step, figure out the appropriate operation to use.
1900 // Allow SETCC to not be supported for all legal data types
1901 // Mostly this targets FP
1902 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1903 MVT::ValueType OldVT = NewInTy;
1904
1905 // Scan for the appropriate larger type to use.
1906 while (1) {
1907 NewInTy = (MVT::ValueType)(NewInTy+1);
1908
1909 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1910 "Fell off of the edge of the integer world");
1911 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1912 "Fell off of the edge of the floating point world");
1913
1914 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001915 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001916 break;
1917 }
1918 if (MVT::isInteger(NewInTy))
1919 assert(0 && "Cannot promote Legal Integer SETCC yet");
1920 else {
1921 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1922 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1923 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001924 Tmp1 = LegalizeOp(Tmp1);
1925 Tmp2 = LegalizeOp(Tmp2);
1926 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001927 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001928 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001929 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001930 case TargetLowering::Expand:
1931 // Expand a setcc node into a select_cc of the same condition, lhs, and
1932 // rhs that selects between const 1 (true) and const 0 (false).
1933 MVT::ValueType VT = Node->getValueType(0);
1934 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1935 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1936 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001937 break;
1938 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001939 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001940 case ISD::MEMSET:
1941 case ISD::MEMCPY:
1942 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001944 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1945
1946 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1947 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1948 case Expand: assert(0 && "Cannot expand a byte!");
1949 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001950 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001951 break;
1952 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001953 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001954 break;
1955 }
1956 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001957 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001958 }
Chris Lattner272455b2005-02-02 03:44:41 +00001959
1960 SDOperand Tmp4;
1961 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001962 case Expand: {
1963 // Length is too big, just take the lo-part of the length.
1964 SDOperand HiPart;
1965 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1966 break;
1967 }
Chris Lattnere5605212005-01-28 22:29:18 +00001968 case Legal:
1969 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001970 break;
1971 case Promote:
1972 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001973 break;
1974 }
1975
1976 SDOperand Tmp5;
1977 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1978 case Expand: assert(0 && "Cannot expand this yet!");
1979 case Legal:
1980 Tmp5 = LegalizeOp(Node->getOperand(4));
1981 break;
1982 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001983 Tmp5 = PromoteOp(Node->getOperand(4));
1984 break;
1985 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001986
1987 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1988 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001989 case TargetLowering::Custom:
1990 isCustom = true;
1991 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001992 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001993 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001994 if (isCustom) {
1995 Tmp1 = TLI.LowerOperation(Result, DAG);
1996 if (Tmp1.Val) Result = Tmp1;
1997 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001998 break;
1999 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002000 // Otherwise, the target does not support this operation. Lower the
2001 // operation to an explicit libcall as appropriate.
2002 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002003 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Chris Lattnere1bd8222005-01-11 05:57:22 +00002004 std::vector<std::pair<SDOperand, const Type*> > Args;
2005
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002006 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002007 if (Node->getOpcode() == ISD::MEMSET) {
2008 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002009 // Extend the (previously legalized) ubyte argument to be an int value
2010 // for the call.
2011 if (Tmp3.getValueType() > MVT::i32)
2012 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2013 else
2014 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002015 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
2016 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
2017
2018 FnName = "memset";
2019 } else if (Node->getOpcode() == ISD::MEMCPY ||
2020 Node->getOpcode() == ISD::MEMMOVE) {
2021 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
2022 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
2023 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
2024 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2025 } else {
2026 assert(0 && "Unknown op!");
2027 }
Chris Lattner45982da2005-05-12 16:53:42 +00002028
Chris Lattnere1bd8222005-01-11 05:57:22 +00002029 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002030 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002031 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002032 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002033 break;
2034 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002035 }
2036 break;
2037 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002038
Chris Lattner5b359c62005-04-02 04:00:59 +00002039 case ISD::SHL_PARTS:
2040 case ISD::SRA_PARTS:
2041 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002042 std::vector<SDOperand> Ops;
2043 bool Changed = false;
2044 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2045 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2046 Changed |= Ops.back() != Node->getOperand(i);
2047 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002048 if (Changed)
2049 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00002050
Evan Cheng05a2d562006-01-09 18:31:59 +00002051 switch (TLI.getOperationAction(Node->getOpcode(),
2052 Node->getValueType(0))) {
2053 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002054 case TargetLowering::Legal: break;
2055 case TargetLowering::Custom:
2056 Tmp1 = TLI.LowerOperation(Result, DAG);
2057 if (Tmp1.Val) {
2058 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002059 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002060 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002061 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2062 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002063 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002064 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002065 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002066 return RetVal;
2067 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002068 break;
2069 }
2070
Chris Lattner2c8086f2005-04-02 05:00:07 +00002071 // Since these produce multiple values, make sure to remember that we
2072 // legalized all of them.
2073 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2074 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2075 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002076 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002077
2078 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002079 case ISD::ADD:
2080 case ISD::SUB:
2081 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002082 case ISD::MULHS:
2083 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002084 case ISD::UDIV:
2085 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002086 case ISD::AND:
2087 case ISD::OR:
2088 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002089 case ISD::SHL:
2090 case ISD::SRL:
2091 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002092 case ISD::FADD:
2093 case ISD::FSUB:
2094 case ISD::FMUL:
2095 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002096 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002097 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2098 case Expand: assert(0 && "Not possible");
2099 case Legal:
2100 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2101 break;
2102 case Promote:
2103 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2104 break;
2105 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002106
2107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002108
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002109 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002110 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002111 case TargetLowering::Legal: break;
2112 case TargetLowering::Custom:
2113 Tmp1 = TLI.LowerOperation(Result, DAG);
2114 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002115 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002116 case TargetLowering::Expand: {
2117 assert(MVT::isVector(Node->getValueType(0)) &&
2118 "Cannot expand this binary operator!");
2119 // Expand the operation into a bunch of nasty scalar code.
2120 std::vector<SDOperand> Ops;
2121 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2122 MVT::ValueType PtrVT = TLI.getPointerTy();
2123 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2124 i != e; ++i) {
2125 SDOperand Idx = DAG.getConstant(i, PtrVT);
2126 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2127 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2128 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2129 }
2130 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
2131 break;
2132 }
Evan Chengcc987612006-04-12 21:20:24 +00002133 case TargetLowering::Promote: {
2134 switch (Node->getOpcode()) {
2135 default: assert(0 && "Do not know how to promote this BinOp!");
2136 case ISD::AND:
2137 case ISD::OR:
2138 case ISD::XOR: {
2139 MVT::ValueType OVT = Node->getValueType(0);
2140 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2141 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2142 // Bit convert each of the values to the new type.
2143 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2144 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2145 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2146 // Bit convert the result back the original type.
2147 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2148 break;
2149 }
2150 }
2151 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002152 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002153 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002154
2155 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2156 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2157 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2158 case Expand: assert(0 && "Not possible");
2159 case Legal:
2160 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2161 break;
2162 case Promote:
2163 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2164 break;
2165 }
2166
2167 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2168
2169 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2170 default: assert(0 && "Operation not supported");
2171 case TargetLowering::Custom:
2172 Tmp1 = TLI.LowerOperation(Result, DAG);
2173 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002174 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002175 case TargetLowering::Legal: break;
2176 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00002177 // If this target supports fabs/fneg natively, do this efficiently.
2178 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2179 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2180 // Get the sign bit of the RHS.
2181 MVT::ValueType IVT =
2182 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2183 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2184 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2185 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2186 // Get the absolute value of the result.
2187 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2188 // Select between the nabs and abs value based on the sign bit of
2189 // the input.
2190 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2191 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2192 AbsVal),
2193 AbsVal);
2194 Result = LegalizeOp(Result);
2195 break;
2196 }
2197
2198 // Otherwise, do bitwise ops!
2199
2200 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00002201 const char *FnName;
2202 if (Node->getValueType(0) == MVT::f32) {
2203 FnName = "copysignf";
2204 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2205 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2206 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2207 } else {
2208 FnName = "copysign";
2209 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2210 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2211 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2212 }
2213 SDOperand Dummy;
2214 Result = ExpandLibCall(FnName, Node, Dummy);
2215 break;
2216 }
2217 break;
2218
Nate Begeman551bf3f2006-02-17 05:43:56 +00002219 case ISD::ADDC:
2220 case ISD::SUBC:
2221 Tmp1 = LegalizeOp(Node->getOperand(0));
2222 Tmp2 = LegalizeOp(Node->getOperand(1));
2223 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2224 // Since this produces two values, make sure to remember that we legalized
2225 // both of them.
2226 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2227 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2228 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002229
Nate Begeman551bf3f2006-02-17 05:43:56 +00002230 case ISD::ADDE:
2231 case ISD::SUBE:
2232 Tmp1 = LegalizeOp(Node->getOperand(0));
2233 Tmp2 = LegalizeOp(Node->getOperand(1));
2234 Tmp3 = LegalizeOp(Node->getOperand(2));
2235 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2236 // Since this produces two values, make sure to remember that we legalized
2237 // both of them.
2238 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2239 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2240 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002241
Nate Begeman419f8b62005-10-18 00:27:41 +00002242 case ISD::BUILD_PAIR: {
2243 MVT::ValueType PairTy = Node->getValueType(0);
2244 // TODO: handle the case where the Lo and Hi operands are not of legal type
2245 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2246 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2247 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002248 case TargetLowering::Promote:
2249 case TargetLowering::Custom:
2250 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002251 case TargetLowering::Legal:
2252 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2253 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2254 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002255 case TargetLowering::Expand:
2256 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2257 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2258 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2259 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2260 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002261 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002262 break;
2263 }
2264 break;
2265 }
2266
Nate Begemanc105e192005-04-06 00:23:54 +00002267 case ISD::UREM:
2268 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002269 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002270 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2271 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002272
Nate Begemanc105e192005-04-06 00:23:54 +00002273 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002274 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2275 case TargetLowering::Custom:
2276 isCustom = true;
2277 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002278 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002279 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002280 if (isCustom) {
2281 Tmp1 = TLI.LowerOperation(Result, DAG);
2282 if (Tmp1.Val) Result = Tmp1;
2283 }
Nate Begemanc105e192005-04-06 00:23:54 +00002284 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002285 case TargetLowering::Expand:
2286 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002287 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002288 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002289 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002290 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2291 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2292 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2293 } else {
2294 // Floating point mod -> fmod libcall.
2295 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2296 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002297 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002298 }
2299 break;
2300 }
2301 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002302 case ISD::VAARG: {
2303 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2304 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2305
Chris Lattner5c62f332006-01-28 07:42:08 +00002306 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002307 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2308 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002309 case TargetLowering::Custom:
2310 isCustom = true;
2311 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002312 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002313 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2314 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002315 Tmp1 = Result.getValue(1);
2316
2317 if (isCustom) {
2318 Tmp2 = TLI.LowerOperation(Result, DAG);
2319 if (Tmp2.Val) {
2320 Result = LegalizeOp(Tmp2);
2321 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2322 }
2323 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002324 break;
2325 case TargetLowering::Expand: {
2326 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2327 Node->getOperand(2));
2328 // Increment the pointer, VAList, to the next vaarg
2329 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2330 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2331 TLI.getPointerTy()));
2332 // Store the incremented VAList to the legalized pointer
2333 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2334 Node->getOperand(2));
2335 // Load the actual argument out of the pointer VAList
2336 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002337 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002338 Result = LegalizeOp(Result);
2339 break;
2340 }
2341 }
2342 // Since VAARG produces two values, make sure to remember that we
2343 // legalized both of them.
2344 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002345 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2346 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002347 }
2348
2349 case ISD::VACOPY:
2350 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2351 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2352 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2353
2354 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2355 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002356 case TargetLowering::Custom:
2357 isCustom = true;
2358 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002359 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2361 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002362 if (isCustom) {
2363 Tmp1 = TLI.LowerOperation(Result, DAG);
2364 if (Tmp1.Val) Result = Tmp1;
2365 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002366 break;
2367 case TargetLowering::Expand:
2368 // This defaults to loading a pointer from the input and storing it to the
2369 // output, returning the chain.
2370 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2371 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2372 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002373 break;
2374 }
2375 break;
2376
2377 case ISD::VAEND:
2378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2379 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2380
2381 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2382 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002383 case TargetLowering::Custom:
2384 isCustom = true;
2385 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002386 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002387 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002388 if (isCustom) {
2389 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2390 if (Tmp1.Val) Result = Tmp1;
2391 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002392 break;
2393 case TargetLowering::Expand:
2394 Result = Tmp1; // Default to a no-op, return the chain
2395 break;
2396 }
2397 break;
2398
2399 case ISD::VASTART:
2400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2401 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2402
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2404
Nate Begemanacc398c2006-01-25 18:21:52 +00002405 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2406 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002407 case TargetLowering::Legal: break;
2408 case TargetLowering::Custom:
2409 Tmp1 = TLI.LowerOperation(Result, DAG);
2410 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002411 break;
2412 }
2413 break;
2414
Nate Begeman35ef9132006-01-11 21:21:00 +00002415 case ISD::ROTL:
2416 case ISD::ROTR:
2417 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2418 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002419
2420 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2421 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002422 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002423 break;
2424
Nate Begemand88fc032006-01-14 03:14:10 +00002425 case ISD::BSWAP:
2426 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2427 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002428 case TargetLowering::Custom:
2429 assert(0 && "Cannot custom legalize this yet!");
2430 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002431 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002432 break;
2433 case TargetLowering::Promote: {
2434 MVT::ValueType OVT = Tmp1.getValueType();
2435 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2436 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002437
Chris Lattner456a93a2006-01-28 07:39:30 +00002438 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2439 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2440 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2441 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2442 break;
2443 }
2444 case TargetLowering::Expand:
2445 Result = ExpandBSWAP(Tmp1);
2446 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002447 }
2448 break;
2449
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002450 case ISD::CTPOP:
2451 case ISD::CTTZ:
2452 case ISD::CTLZ:
2453 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2454 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002455 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002456 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002457 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002458 break;
2459 case TargetLowering::Promote: {
2460 MVT::ValueType OVT = Tmp1.getValueType();
2461 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002462
2463 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002464 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2465 // Perform the larger operation, then subtract if needed.
2466 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002467 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002468 case ISD::CTPOP:
2469 Result = Tmp1;
2470 break;
2471 case ISD::CTTZ:
2472 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002473 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2474 DAG.getConstant(getSizeInBits(NVT), NVT),
2475 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002476 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002477 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2478 break;
2479 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002480 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002481 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2482 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002483 getSizeInBits(OVT), NVT));
2484 break;
2485 }
2486 break;
2487 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002488 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002489 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002490 break;
2491 }
2492 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002493
Chris Lattner2c8086f2005-04-02 05:00:07 +00002494 // Unary operators
2495 case ISD::FABS:
2496 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002497 case ISD::FSQRT:
2498 case ISD::FSIN:
2499 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002500 Tmp1 = LegalizeOp(Node->getOperand(0));
2501 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002502 case TargetLowering::Promote:
2503 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002504 isCustom = true;
2505 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002506 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002507 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002508 if (isCustom) {
2509 Tmp1 = TLI.LowerOperation(Result, DAG);
2510 if (Tmp1.Val) Result = Tmp1;
2511 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002512 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002513 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002514 switch (Node->getOpcode()) {
2515 default: assert(0 && "Unreachable!");
2516 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002517 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2518 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002519 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002520 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002521 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002522 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2523 MVT::ValueType VT = Node->getValueType(0);
2524 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002525 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002526 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2527 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002528 break;
2529 }
2530 case ISD::FSQRT:
2531 case ISD::FSIN:
2532 case ISD::FCOS: {
2533 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002534 const char *FnName = 0;
2535 switch(Node->getOpcode()) {
2536 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2537 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2538 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2539 default: assert(0 && "Unreachable!");
2540 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002541 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002542 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002543 break;
2544 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002545 }
2546 break;
2547 }
2548 break;
Chris Lattner35481892005-12-23 00:16:34 +00002549
2550 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002551 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002552 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002553 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002554 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2555 Node->getOperand(0).getValueType())) {
2556 default: assert(0 && "Unknown operation action!");
2557 case TargetLowering::Expand:
2558 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2559 break;
2560 case TargetLowering::Legal:
2561 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002562 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002563 break;
2564 }
2565 }
2566 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002567 case ISD::VBIT_CONVERT: {
2568 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2569 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2570
2571 // The input has to be a vector type, we have to either scalarize it, pack
2572 // it, or convert it based on whether the input vector type is legal.
2573 SDNode *InVal = Node->getOperand(0).Val;
2574 unsigned NumElems =
2575 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2576 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2577
2578 // Figure out if there is a Packed type corresponding to this Vector
2579 // type. If so, convert to the packed type.
2580 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2581 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2582 // Turn this into a bit convert of the packed input.
2583 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2584 PackVectorOp(Node->getOperand(0), TVT));
2585 break;
2586 } else if (NumElems == 1) {
2587 // Turn this into a bit convert of the scalar input.
2588 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2589 PackVectorOp(Node->getOperand(0), EVT));
2590 break;
2591 } else {
2592 // FIXME: UNIMP! Store then reload
2593 assert(0 && "Cast from unsupported vector type not implemented yet!");
2594 }
2595 }
2596
Chris Lattner2c8086f2005-04-02 05:00:07 +00002597 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002598 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002599 case ISD::UINT_TO_FP: {
2600 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002601 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2602 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002603 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002604 Node->getOperand(0).getValueType())) {
2605 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002606 case TargetLowering::Custom:
2607 isCustom = true;
2608 // FALLTHROUGH
2609 case TargetLowering::Legal:
2610 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002611 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002612 if (isCustom) {
2613 Tmp1 = TLI.LowerOperation(Result, DAG);
2614 if (Tmp1.Val) Result = Tmp1;
2615 }
2616 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002617 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002618 Result = ExpandLegalINT_TO_FP(isSigned,
2619 LegalizeOp(Node->getOperand(0)),
2620 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002621 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002622 case TargetLowering::Promote:
2623 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2624 Node->getValueType(0),
2625 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002626 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002627 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002628 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002629 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002630 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2631 Node->getValueType(0), Node->getOperand(0));
2632 break;
2633 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002634 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002635 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002636 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2637 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002638 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002639 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2640 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002641 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002642 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2643 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002644 break;
2645 }
2646 break;
2647 }
2648 case ISD::TRUNCATE:
2649 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2650 case Legal:
2651 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002652 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002653 break;
2654 case Expand:
2655 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2656
2657 // Since the result is legal, we should just be able to truncate the low
2658 // part of the source.
2659 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2660 break;
2661 case Promote:
2662 Result = PromoteOp(Node->getOperand(0));
2663 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2664 break;
2665 }
2666 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002667
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002668 case ISD::FP_TO_SINT:
2669 case ISD::FP_TO_UINT:
2670 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2671 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002672 Tmp1 = LegalizeOp(Node->getOperand(0));
2673
Chris Lattner1618beb2005-07-29 00:11:56 +00002674 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2675 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002676 case TargetLowering::Custom:
2677 isCustom = true;
2678 // FALLTHROUGH
2679 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002680 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002681 if (isCustom) {
2682 Tmp1 = TLI.LowerOperation(Result, DAG);
2683 if (Tmp1.Val) Result = Tmp1;
2684 }
2685 break;
2686 case TargetLowering::Promote:
2687 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2688 Node->getOpcode() == ISD::FP_TO_SINT);
2689 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002690 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002691 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2692 SDOperand True, False;
2693 MVT::ValueType VT = Node->getOperand(0).getValueType();
2694 MVT::ValueType NVT = Node->getValueType(0);
2695 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2696 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2697 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2698 Node->getOperand(0), Tmp2, ISD::SETLT);
2699 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2700 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002701 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002702 Tmp2));
2703 False = DAG.getNode(ISD::XOR, NVT, False,
2704 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002705 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2706 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002707 } else {
2708 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2709 }
2710 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002711 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002712 break;
2713 case Expand:
2714 assert(0 && "Shouldn't need to expand other operators here!");
2715 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002716 Tmp1 = PromoteOp(Node->getOperand(0));
2717 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2718 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002719 break;
2720 }
2721 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002722
Chris Lattner13c78e22005-09-02 00:18:10 +00002723 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002724 case ISD::ZERO_EXTEND:
2725 case ISD::SIGN_EXTEND:
2726 case ISD::FP_EXTEND:
2727 case ISD::FP_ROUND:
2728 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002729 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002730 case Legal:
2731 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002732 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002733 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002734 case Promote:
2735 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002736 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002737 Tmp1 = PromoteOp(Node->getOperand(0));
2738 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002739 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002740 case ISD::ZERO_EXTEND:
2741 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002742 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002743 Result = DAG.getZeroExtendInReg(Result,
2744 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002745 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002746 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002747 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002748 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002749 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002750 Result,
2751 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002752 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002753 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002754 Result = PromoteOp(Node->getOperand(0));
2755 if (Result.getValueType() != Op.getValueType())
2756 // Dynamically dead while we have only 2 FP types.
2757 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2758 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002759 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002760 Result = PromoteOp(Node->getOperand(0));
2761 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2762 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002763 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002764 }
2765 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002766 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002767 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002768 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002769 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002770
2771 // If this operation is not supported, convert it to a shl/shr or load/store
2772 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002773 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2774 default: assert(0 && "This action not supported for this op yet!");
2775 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002776 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002777 break;
2778 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002779 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002780 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002781 // NOTE: we could fall back on load/store here too for targets without
2782 // SAR. However, it is doubtful that any exist.
2783 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2784 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002785 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002786 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2787 Node->getOperand(0), ShiftCst);
2788 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2789 Result, ShiftCst);
2790 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2791 // The only way we can lower this is to turn it into a STORETRUNC,
2792 // EXTLOAD pair, targetting a temporary location (a stack slot).
2793
2794 // NOTE: there is a choice here between constantly creating new stack
2795 // slots and always reusing the same one. We currently always create
2796 // new ones, as reuse may inhibit scheduling.
2797 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00002798 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
2799 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002800 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002801 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002802 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2803 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2804 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002805 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002806 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002807 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2808 Result, StackSlot, DAG.getSrcValue(NULL),
2809 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002810 } else {
2811 assert(0 && "Unknown op");
2812 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002813 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002814 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002815 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002816 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002817 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002818
Chris Lattner4ddd2832006-04-08 04:13:17 +00002819 assert(Result.getValueType() == Op.getValueType() &&
2820 "Bad legalization!");
2821
Chris Lattner456a93a2006-01-28 07:39:30 +00002822 // Make sure that the generated code is itself legal.
2823 if (Result != Op)
2824 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002825
Chris Lattner45982da2005-05-12 16:53:42 +00002826 // Note that LegalizeOp may be reentered even from single-use nodes, which
2827 // means that we always must cache transformed nodes.
2828 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002829 return Result;
2830}
2831
Chris Lattner8b6fa222005-01-15 22:16:26 +00002832/// PromoteOp - Given an operation that produces a value in an invalid type,
2833/// promote it to compute the value into a larger type. The produced value will
2834/// have the correct bits for the low portion of the register, but no guarantee
2835/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002836SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2837 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002838 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002839 assert(getTypeAction(VT) == Promote &&
2840 "Caller should expand or legalize operands that are not promotable!");
2841 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2842 "Cannot promote to smaller type!");
2843
Chris Lattner03c85462005-01-15 05:21:40 +00002844 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002845 SDOperand Result;
2846 SDNode *Node = Op.Val;
2847
Chris Lattner6fdcb252005-09-02 20:32:45 +00002848 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2849 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002850
Chris Lattner03c85462005-01-15 05:21:40 +00002851 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002852 case ISD::CopyFromReg:
2853 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002854 default:
2855 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2856 assert(0 && "Do not know how to promote this operator!");
2857 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002858 case ISD::UNDEF:
2859 Result = DAG.getNode(ISD::UNDEF, NVT);
2860 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002861 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002862 if (VT != MVT::i1)
2863 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2864 else
2865 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002866 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2867 break;
2868 case ISD::ConstantFP:
2869 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2870 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2871 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002872
Chris Lattner82fbfb62005-01-18 02:59:52 +00002873 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002874 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002875 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2876 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002877 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00002878
Chris Lattner03c85462005-01-15 05:21:40 +00002879 case ISD::TRUNCATE:
2880 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2881 case Legal:
2882 Result = LegalizeOp(Node->getOperand(0));
2883 assert(Result.getValueType() >= NVT &&
2884 "This truncation doesn't make sense!");
2885 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2886 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2887 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002888 case Promote:
2889 // The truncation is not required, because we don't guarantee anything
2890 // about high bits anyway.
2891 Result = PromoteOp(Node->getOperand(0));
2892 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002893 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002894 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2895 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002896 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002897 }
2898 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002899 case ISD::SIGN_EXTEND:
2900 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002901 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002902 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2903 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2904 case Legal:
2905 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002906 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002907 break;
2908 case Promote:
2909 // Promote the reg if it's smaller.
2910 Result = PromoteOp(Node->getOperand(0));
2911 // The high bits are not guaranteed to be anything. Insert an extend.
2912 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002913 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002914 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002915 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002916 Result = DAG.getZeroExtendInReg(Result,
2917 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002918 break;
2919 }
2920 break;
Chris Lattner35481892005-12-23 00:16:34 +00002921 case ISD::BIT_CONVERT:
2922 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2923 Result = PromoteOp(Result);
2924 break;
2925
Chris Lattner8b6fa222005-01-15 22:16:26 +00002926 case ISD::FP_EXTEND:
2927 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2928 case ISD::FP_ROUND:
2929 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2930 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2931 case Promote: assert(0 && "Unreachable with 2 FP types!");
2932 case Legal:
2933 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002934 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002935 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002936 break;
2937 }
2938 break;
2939
2940 case ISD::SINT_TO_FP:
2941 case ISD::UINT_TO_FP:
2942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2943 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002944 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002945 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002946 break;
2947
2948 case Promote:
2949 Result = PromoteOp(Node->getOperand(0));
2950 if (Node->getOpcode() == ISD::SINT_TO_FP)
2951 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002952 Result,
2953 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002954 else
Chris Lattner23993562005-04-13 02:38:47 +00002955 Result = DAG.getZeroExtendInReg(Result,
2956 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002957 // No extra round required here.
2958 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002959 break;
2960 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002961 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2962 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002963 // Round if we cannot tolerate excess precision.
2964 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002965 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2966 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002967 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002968 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002969 break;
2970
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002971 case ISD::SIGN_EXTEND_INREG:
2972 Result = PromoteOp(Node->getOperand(0));
2973 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2974 Node->getOperand(1));
2975 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002976 case ISD::FP_TO_SINT:
2977 case ISD::FP_TO_UINT:
2978 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2979 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002980 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002981 break;
2982 case Promote:
2983 // The input result is prerounded, so we don't have to do anything
2984 // special.
2985 Tmp1 = PromoteOp(Node->getOperand(0));
2986 break;
2987 case Expand:
2988 assert(0 && "not implemented");
2989 }
Nate Begemand2558e32005-08-14 01:20:53 +00002990 // If we're promoting a UINT to a larger size, check to see if the new node
2991 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2992 // we can use that instead. This allows us to generate better code for
2993 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2994 // legal, such as PowerPC.
2995 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002996 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002997 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2998 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002999 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3000 } else {
3001 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3002 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003003 break;
3004
Chris Lattner2c8086f2005-04-02 05:00:07 +00003005 case ISD::FABS:
3006 case ISD::FNEG:
3007 Tmp1 = PromoteOp(Node->getOperand(0));
3008 assert(Tmp1.getValueType() == NVT);
3009 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3010 // NOTE: we do not have to do any extra rounding here for
3011 // NoExcessFPPrecision, because we know the input will have the appropriate
3012 // precision, and these operations don't modify precision at all.
3013 break;
3014
Chris Lattnerda6ba872005-04-28 21:44:33 +00003015 case ISD::FSQRT:
3016 case ISD::FSIN:
3017 case ISD::FCOS:
3018 Tmp1 = PromoteOp(Node->getOperand(0));
3019 assert(Tmp1.getValueType() == NVT);
3020 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003021 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003022 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3023 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003024 break;
3025
Chris Lattner03c85462005-01-15 05:21:40 +00003026 case ISD::AND:
3027 case ISD::OR:
3028 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003029 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003030 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003031 case ISD::MUL:
3032 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003033 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003034 // that too is okay if they are integer operations.
3035 Tmp1 = PromoteOp(Node->getOperand(0));
3036 Tmp2 = PromoteOp(Node->getOperand(1));
3037 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3038 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003039 break;
3040 case ISD::FADD:
3041 case ISD::FSUB:
3042 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003043 Tmp1 = PromoteOp(Node->getOperand(0));
3044 Tmp2 = PromoteOp(Node->getOperand(1));
3045 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3046 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3047
3048 // Floating point operations will give excess precision that we may not be
3049 // able to tolerate. If we DO allow excess precision, just leave it,
3050 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003051 // FIXME: Why would we need to round FP ops more than integer ones?
3052 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003053 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003054 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3055 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003056 break;
3057
Chris Lattner8b6fa222005-01-15 22:16:26 +00003058 case ISD::SDIV:
3059 case ISD::SREM:
3060 // These operators require that their input be sign extended.
3061 Tmp1 = PromoteOp(Node->getOperand(0));
3062 Tmp2 = PromoteOp(Node->getOperand(1));
3063 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003064 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3065 DAG.getValueType(VT));
3066 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3067 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003068 }
3069 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3070
3071 // Perform FP_ROUND: this is probably overly pessimistic.
3072 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003073 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3074 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003075 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003076 case ISD::FDIV:
3077 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003078 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003079 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003080 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3081 case Legal:
3082 Tmp1 = LegalizeOp(Node->getOperand(0));
3083 break;
3084 case Promote:
3085 Tmp1 = PromoteOp(Node->getOperand(0));
3086 break;
3087 case Expand:
3088 assert(0 && "not implemented");
3089 }
3090 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3091 case Legal:
3092 Tmp2 = LegalizeOp(Node->getOperand(1));
3093 break;
3094 case Promote:
3095 Tmp2 = PromoteOp(Node->getOperand(1));
3096 break;
3097 case Expand:
3098 assert(0 && "not implemented");
3099 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003100 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3101
3102 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003103 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003104 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3105 DAG.getValueType(VT));
3106 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003107
3108 case ISD::UDIV:
3109 case ISD::UREM:
3110 // These operators require that their input be zero extended.
3111 Tmp1 = PromoteOp(Node->getOperand(0));
3112 Tmp2 = PromoteOp(Node->getOperand(1));
3113 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003114 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3115 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003116 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3117 break;
3118
3119 case ISD::SHL:
3120 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003121 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003122 break;
3123 case ISD::SRA:
3124 // The input value must be properly sign extended.
3125 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003126 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3127 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003128 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003129 break;
3130 case ISD::SRL:
3131 // The input value must be properly zero extended.
3132 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003133 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003134 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003135 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003136
3137 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003138 Tmp1 = Node->getOperand(0); // Get the chain.
3139 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003140 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3141 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3142 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3143 } else {
3144 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3145 Node->getOperand(2));
3146 // Increment the pointer, VAList, to the next vaarg
3147 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3148 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3149 TLI.getPointerTy()));
3150 // Store the incremented VAList to the legalized pointer
3151 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3152 Node->getOperand(2));
3153 // Load the actual argument out of the pointer VAList
3154 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3155 DAG.getSrcValue(0), VT);
3156 }
3157 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003158 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003159 break;
3160
Chris Lattner03c85462005-01-15 05:21:40 +00003161 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003162 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3163 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003164 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003165 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003166 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003167 case ISD::SEXTLOAD:
3168 case ISD::ZEXTLOAD:
3169 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003170 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3171 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00003172 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003173 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003174 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003175 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003176 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003177 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3178 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003179 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003180 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003181 case ISD::SELECT_CC:
3182 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3183 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3184 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003185 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003186 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003187 case ISD::BSWAP:
3188 Tmp1 = Node->getOperand(0);
3189 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3190 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3191 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3192 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3193 TLI.getShiftAmountTy()));
3194 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003195 case ISD::CTPOP:
3196 case ISD::CTTZ:
3197 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003198 // Zero extend the argument
3199 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003200 // Perform the larger operation, then subtract if needed.
3201 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003202 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003203 case ISD::CTPOP:
3204 Result = Tmp1;
3205 break;
3206 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003207 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003208 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003209 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003210 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003211 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003212 break;
3213 case ISD::CTLZ:
3214 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003215 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3216 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003217 getSizeInBits(VT), NVT));
3218 break;
3219 }
3220 break;
Chris Lattner15972212006-03-31 17:55:51 +00003221 case ISD::VEXTRACT_VECTOR_ELT:
3222 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3223 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003224 case ISD::EXTRACT_VECTOR_ELT:
3225 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3226 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003227 }
3228
3229 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003230
3231 // Make sure the result is itself legal.
3232 Result = LegalizeOp(Result);
3233
3234 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003235 AddPromotedOperand(Op, Result);
3236 return Result;
3237}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003238
Chris Lattner15972212006-03-31 17:55:51 +00003239/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3240/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3241/// on the vector type. The return type of this matches the element type of the
3242/// vector, which may not be legal for the target.
3243SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3244 // We know that operand #0 is the Vec vector. If the index is a constant
3245 // or if the invec is a supported hardware type, we can use it. Otherwise,
3246 // lower to a store then an indexed load.
3247 SDOperand Vec = Op.getOperand(0);
3248 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3249
3250 SDNode *InVal = Vec.Val;
3251 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3252 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3253
3254 // Figure out if there is a Packed type corresponding to this Vector
3255 // type. If so, convert to the packed type.
3256 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3257 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3258 // Turn this into a packed extract_vector_elt operation.
3259 Vec = PackVectorOp(Vec, TVT);
3260 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3261 } else if (NumElems == 1) {
3262 // This must be an access of the only element. Return it.
3263 return PackVectorOp(Vec, EVT);
3264 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3265 SDOperand Lo, Hi;
3266 SplitVectorOp(Vec, Lo, Hi);
3267 if (CIdx->getValue() < NumElems/2) {
3268 Vec = Lo;
3269 } else {
3270 Vec = Hi;
3271 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3272 }
3273
3274 // It's now an extract from the appropriate high or low part. Recurse.
3275 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3276 return LowerVEXTRACT_VECTOR_ELT(Op);
3277 } else {
3278 // Variable index case for extract element.
3279 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3280 assert(0 && "unimp!");
3281 return SDOperand();
3282 }
3283}
3284
Chris Lattner4aab2f42006-04-02 05:06:04 +00003285/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3286/// memory traffic.
3287SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3288 SDOperand Vector = Op.getOperand(0);
3289 SDOperand Idx = Op.getOperand(1);
3290
3291 // If the target doesn't support this, store the value to a temporary
3292 // stack slot, then LOAD the scalar element back out.
3293 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3294 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3295 Vector, StackPtr, DAG.getSrcValue(NULL));
3296
3297 // Add the offset to the index.
3298 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3299 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3300 DAG.getConstant(EltSize, Idx.getValueType()));
3301 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3302
3303 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3304}
3305
Chris Lattner15972212006-03-31 17:55:51 +00003306
Nate Begeman750ac1b2006-02-01 07:19:44 +00003307/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3308/// with condition CC on the current target. This usually involves legalizing
3309/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3310/// there may be no choice but to create a new SetCC node to represent the
3311/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3312/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3313void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3314 SDOperand &RHS,
3315 SDOperand &CC) {
3316 SDOperand Tmp1, Tmp2, Result;
3317
3318 switch (getTypeAction(LHS.getValueType())) {
3319 case Legal:
3320 Tmp1 = LegalizeOp(LHS); // LHS
3321 Tmp2 = LegalizeOp(RHS); // RHS
3322 break;
3323 case Promote:
3324 Tmp1 = PromoteOp(LHS); // LHS
3325 Tmp2 = PromoteOp(RHS); // RHS
3326
3327 // If this is an FP compare, the operands have already been extended.
3328 if (MVT::isInteger(LHS.getValueType())) {
3329 MVT::ValueType VT = LHS.getValueType();
3330 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3331
3332 // Otherwise, we have to insert explicit sign or zero extends. Note
3333 // that we could insert sign extends for ALL conditions, but zero extend
3334 // is cheaper on many machines (an AND instead of two shifts), so prefer
3335 // it.
3336 switch (cast<CondCodeSDNode>(CC)->get()) {
3337 default: assert(0 && "Unknown integer comparison!");
3338 case ISD::SETEQ:
3339 case ISD::SETNE:
3340 case ISD::SETUGE:
3341 case ISD::SETUGT:
3342 case ISD::SETULE:
3343 case ISD::SETULT:
3344 // ALL of these operations will work if we either sign or zero extend
3345 // the operands (including the unsigned comparisons!). Zero extend is
3346 // usually a simpler/cheaper operation, so prefer it.
3347 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3348 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3349 break;
3350 case ISD::SETGE:
3351 case ISD::SETGT:
3352 case ISD::SETLT:
3353 case ISD::SETLE:
3354 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3355 DAG.getValueType(VT));
3356 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3357 DAG.getValueType(VT));
3358 break;
3359 }
3360 }
3361 break;
3362 case Expand:
3363 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3364 ExpandOp(LHS, LHSLo, LHSHi);
3365 ExpandOp(RHS, RHSLo, RHSHi);
3366 switch (cast<CondCodeSDNode>(CC)->get()) {
3367 case ISD::SETEQ:
3368 case ISD::SETNE:
3369 if (RHSLo == RHSHi)
3370 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3371 if (RHSCST->isAllOnesValue()) {
3372 // Comparison to -1.
3373 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3374 Tmp2 = RHSLo;
3375 break;
3376 }
3377
3378 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3379 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3380 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3381 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3382 break;
3383 default:
3384 // If this is a comparison of the sign bit, just look at the top part.
3385 // X > -1, x < 0
3386 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3387 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3388 CST->getValue() == 0) || // X < 0
3389 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3390 CST->isAllOnesValue())) { // X > -1
3391 Tmp1 = LHSHi;
3392 Tmp2 = RHSHi;
3393 break;
3394 }
3395
3396 // FIXME: This generated code sucks.
3397 ISD::CondCode LowCC;
3398 switch (cast<CondCodeSDNode>(CC)->get()) {
3399 default: assert(0 && "Unknown integer setcc!");
3400 case ISD::SETLT:
3401 case ISD::SETULT: LowCC = ISD::SETULT; break;
3402 case ISD::SETGT:
3403 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3404 case ISD::SETLE:
3405 case ISD::SETULE: LowCC = ISD::SETULE; break;
3406 case ISD::SETGE:
3407 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3408 }
3409
3410 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3411 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3412 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3413
3414 // NOTE: on targets without efficient SELECT of bools, we can always use
3415 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3416 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3417 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3418 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3419 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3420 Result, Tmp1, Tmp2));
3421 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003422 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003423 }
3424 }
3425 LHS = Tmp1;
3426 RHS = Tmp2;
3427}
3428
Chris Lattner35481892005-12-23 00:16:34 +00003429/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003430/// The resultant code need not be legal. Note that SrcOp is the input operand
3431/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003432SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3433 SDOperand SrcOp) {
3434 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003435 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003436
3437 // Emit a store to the stack slot.
3438 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003439 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003440 // Result is a load from the stack slot.
3441 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3442}
3443
Chris Lattner4352cc92006-04-04 17:23:26 +00003444SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3445 // Create a vector sized/aligned stack slot, store the value to element #0,
3446 // then load the whole vector back out.
3447 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3448 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3449 Node->getOperand(0), StackPtr,
3450 DAG.getSrcValue(NULL));
3451 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3452}
3453
3454
Chris Lattnerce872152006-03-19 06:31:19 +00003455/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3456/// support the operation, but do support the resultant packed vector type.
3457SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3458
3459 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003460 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003461 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003462 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003463 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003464 std::map<SDOperand, std::vector<unsigned> > Values;
3465 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003466 bool isConstant = true;
3467 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3468 SplatValue.getOpcode() != ISD::UNDEF)
3469 isConstant = false;
3470
Evan Cheng033e6812006-03-24 01:17:21 +00003471 for (unsigned i = 1; i < NumElems; ++i) {
3472 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003473 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003474 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003475 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003476 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003477 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003478
3479 // If this isn't a constant element or an undef, we can't use a constant
3480 // pool load.
3481 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3482 V.getOpcode() != ISD::UNDEF)
3483 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003484 }
3485
3486 if (isOnlyLowElement) {
3487 // If the low element is an undef too, then this whole things is an undef.
3488 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3489 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3490 // Otherwise, turn this into a scalar_to_vector node.
3491 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3492 Node->getOperand(0));
3493 }
3494
Chris Lattner2eb86532006-03-24 07:29:17 +00003495 // If all elements are constants, create a load from the constant pool.
3496 if (isConstant) {
3497 MVT::ValueType VT = Node->getValueType(0);
3498 const Type *OpNTy =
3499 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3500 std::vector<Constant*> CV;
3501 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3502 if (ConstantFPSDNode *V =
3503 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3504 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3505 } else if (ConstantSDNode *V =
3506 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3507 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3508 } else {
3509 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3510 CV.push_back(UndefValue::get(OpNTy));
3511 }
3512 }
3513 Constant *CP = ConstantPacked::get(CV);
3514 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3515 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3516 DAG.getSrcValue(NULL));
3517 }
3518
Chris Lattner87100e02006-03-20 01:52:29 +00003519 if (SplatValue.Val) { // Splat of one value?
3520 // Build the shuffle constant vector: <0, 0, 0, 0>
3521 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003522 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003523 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003524 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003525 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3526
3527 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003528 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003529 // Get the splatted value into the low element of a vector register.
3530 SDOperand LowValVec =
3531 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3532
3533 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3534 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3535 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3536 SplatMask);
3537 }
3538 }
3539
Evan Cheng033e6812006-03-24 01:17:21 +00003540 // If there are only two unique elements, we may be able to turn this into a
3541 // vector shuffle.
3542 if (Values.size() == 2) {
3543 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3544 MVT::ValueType MaskVT =
3545 MVT::getIntVectorWithNumElements(NumElems);
3546 std::vector<SDOperand> MaskVec(NumElems);
3547 unsigned i = 0;
3548 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3549 E = Values.end(); I != E; ++I) {
3550 for (std::vector<unsigned>::iterator II = I->second.begin(),
3551 EE = I->second.end(); II != EE; ++II)
3552 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3553 i += NumElems;
3554 }
3555 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3556
3557 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003558 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3559 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng033e6812006-03-24 01:17:21 +00003560 std::vector<SDOperand> Ops;
3561 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3562 E = Values.end(); I != E; ++I) {
3563 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3564 I->first);
3565 Ops.push_back(Op);
3566 }
3567 Ops.push_back(ShuffleMask);
3568
3569 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3570 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3571 }
3572 }
Chris Lattnerce872152006-03-19 06:31:19 +00003573
3574 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3575 // aligned object on the stack, store each element into it, then load
3576 // the result as a vector.
3577 MVT::ValueType VT = Node->getValueType(0);
3578 // Create the stack frame object.
3579 SDOperand FIPtr = CreateStackTemporary(VT);
3580
3581 // Emit a store of each element to the stack slot.
3582 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003583 unsigned TypeByteSize =
3584 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3585 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3586 // Store (in the right endianness) the elements to memory.
3587 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3588 // Ignore undef elements.
3589 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3590
Chris Lattner841c8822006-03-22 01:46:54 +00003591 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003592
3593 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3594 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3595
3596 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3597 Node->getOperand(i), Idx,
3598 DAG.getSrcValue(NULL)));
3599 }
3600
3601 SDOperand StoreChain;
3602 if (!Stores.empty()) // Not all undef elements?
3603 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3604 else
3605 StoreChain = DAG.getEntryNode();
3606
3607 // Result is a load from the stack slot.
3608 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3609}
3610
3611/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3612/// specified value type.
3613SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3614 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3615 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3616 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3617 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3618}
3619
Chris Lattner5b359c62005-04-02 04:00:59 +00003620void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3621 SDOperand Op, SDOperand Amt,
3622 SDOperand &Lo, SDOperand &Hi) {
3623 // Expand the subcomponents.
3624 SDOperand LHSL, LHSH;
3625 ExpandOp(Op, LHSL, LHSH);
3626
3627 std::vector<SDOperand> Ops;
3628 Ops.push_back(LHSL);
3629 Ops.push_back(LHSH);
3630 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003631 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003632 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003633 Hi = Lo.getValue(1);
3634}
3635
3636
Chris Lattnere34b3962005-01-19 04:19:40 +00003637/// ExpandShift - Try to find a clever way to expand this shift operation out to
3638/// smaller elements. If we can't find a way that is more efficient than a
3639/// libcall on this target, return false. Otherwise, return true with the
3640/// low-parts expanded into Lo and Hi.
3641bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3642 SDOperand &Lo, SDOperand &Hi) {
3643 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3644 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003645
Chris Lattnere34b3962005-01-19 04:19:40 +00003646 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003647 SDOperand ShAmt = LegalizeOp(Amt);
3648 MVT::ValueType ShTy = ShAmt.getValueType();
3649 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3650 unsigned NVTBits = MVT::getSizeInBits(NVT);
3651
3652 // Handle the case when Amt is an immediate. Other cases are currently broken
3653 // and are disabled.
3654 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3655 unsigned Cst = CN->getValue();
3656 // Expand the incoming operand to be shifted, so that we have its parts
3657 SDOperand InL, InH;
3658 ExpandOp(Op, InL, InH);
3659 switch(Opc) {
3660 case ISD::SHL:
3661 if (Cst > VTBits) {
3662 Lo = DAG.getConstant(0, NVT);
3663 Hi = DAG.getConstant(0, NVT);
3664 } else if (Cst > NVTBits) {
3665 Lo = DAG.getConstant(0, NVT);
3666 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003667 } else if (Cst == NVTBits) {
3668 Lo = DAG.getConstant(0, NVT);
3669 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003670 } else {
3671 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3672 Hi = DAG.getNode(ISD::OR, NVT,
3673 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3674 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3675 }
3676 return true;
3677 case ISD::SRL:
3678 if (Cst > VTBits) {
3679 Lo = DAG.getConstant(0, NVT);
3680 Hi = DAG.getConstant(0, NVT);
3681 } else if (Cst > NVTBits) {
3682 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3683 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003684 } else if (Cst == NVTBits) {
3685 Lo = InH;
3686 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003687 } else {
3688 Lo = DAG.getNode(ISD::OR, NVT,
3689 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3690 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3691 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3692 }
3693 return true;
3694 case ISD::SRA:
3695 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003696 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003697 DAG.getConstant(NVTBits-1, ShTy));
3698 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003699 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003700 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003701 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003702 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003703 } else if (Cst == NVTBits) {
3704 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003705 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003706 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003707 } else {
3708 Lo = DAG.getNode(ISD::OR, NVT,
3709 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3710 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3711 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3712 }
3713 return true;
3714 }
3715 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003716 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003717}
Chris Lattner77e77a62005-01-21 06:05:23 +00003718
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003719
Chris Lattner77e77a62005-01-21 06:05:23 +00003720// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3721// does not fit into a register, return the lo part and set the hi part to the
3722// by-reg argument. If it does fit into a single register, return the result
3723// and leave the Hi part unset.
3724SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3725 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003726 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3727 // The input chain to this libcall is the entry node of the function.
3728 // Legalizing the call will automatically add the previous call to the
3729 // dependence.
3730 SDOperand InChain = DAG.getEntryNode();
3731
Chris Lattner77e77a62005-01-21 06:05:23 +00003732 TargetLowering::ArgListTy Args;
3733 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3734 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3735 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3736 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3737 }
3738 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003739
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003740 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003741 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003742 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003743 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3744 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003745
Chris Lattner6831a812006-02-13 09:18:02 +00003746 // Legalize the call sequence, starting with the chain. This will advance
3747 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3748 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3749 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003750 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003751 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003752 default: assert(0 && "Unknown thing");
3753 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003754 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003755 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003756 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003757 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003758 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003759 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003760 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003761}
3762
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003763
Chris Lattner77e77a62005-01-21 06:05:23 +00003764/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3765/// destination type is legal.
3766SDOperand SelectionDAGLegalize::
3767ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003768 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003769 assert(getTypeAction(Source.getValueType()) == Expand &&
3770 "This is not an expansion!");
3771 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3772
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003773 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003774 assert(Source.getValueType() == MVT::i64 &&
3775 "This only works for 64-bit -> FP");
3776 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3777 // incoming integer is set. To handle this, we dynamically test to see if
3778 // it is set, and, if so, add a fudge factor.
3779 SDOperand Lo, Hi;
3780 ExpandOp(Source, Lo, Hi);
3781
Chris Lattner66de05b2005-05-13 04:45:13 +00003782 // If this is unsigned, and not supported, first perform the conversion to
3783 // signed, then adjust the result if the sign bit is set.
3784 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3785 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3786
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003787 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3788 DAG.getConstant(0, Hi.getValueType()),
3789 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003790 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3791 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3792 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003793 uint64_t FF = 0x5f800000ULL;
3794 if (TLI.isLittleEndian()) FF <<= 32;
3795 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003796
Chris Lattner5839bf22005-08-26 17:15:30 +00003797 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003798 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3799 SDOperand FudgeInReg;
3800 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003801 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3802 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003803 else {
3804 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003805 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3806 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003807 }
Chris Lattner473a9902005-09-29 06:44:39 +00003808 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003809 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003810
Chris Lattnera88a2602005-05-14 05:33:54 +00003811 // Check to see if the target has a custom way to lower this. If so, use it.
3812 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3813 default: assert(0 && "This action not implemented for this operation!");
3814 case TargetLowering::Legal:
3815 case TargetLowering::Expand:
3816 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003817 case TargetLowering::Custom: {
3818 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3819 Source), DAG);
3820 if (NV.Val)
3821 return LegalizeOp(NV);
3822 break; // The target decided this was legal after all
3823 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003824 }
3825
Chris Lattner13689e22005-05-12 07:00:44 +00003826 // Expand the source, then glue it back together for the call. We must expand
3827 // the source in case it is shared (this pass of legalize must traverse it).
3828 SDOperand SrcLo, SrcHi;
3829 ExpandOp(Source, SrcLo, SrcHi);
3830 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3831
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003832 const char *FnName = 0;
3833 if (DestTy == MVT::f32)
3834 FnName = "__floatdisf";
3835 else {
3836 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3837 FnName = "__floatdidf";
3838 }
Chris Lattner6831a812006-02-13 09:18:02 +00003839
3840 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3841 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003842 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003843}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003844
Chris Lattner22cde6a2006-01-28 08:25:58 +00003845/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3846/// INT_TO_FP operation of the specified operand when the target requests that
3847/// we expand it. At this point, we know that the result and operand types are
3848/// legal for the target.
3849SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3850 SDOperand Op0,
3851 MVT::ValueType DestVT) {
3852 if (Op0.getValueType() == MVT::i32) {
3853 // simple 32-bit [signed|unsigned] integer to float/double expansion
3854
3855 // get the stack frame index of a 8 byte buffer
3856 MachineFunction &MF = DAG.getMachineFunction();
3857 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3858 // get address of 8 byte buffer
3859 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3860 // word offset constant for Hi/Lo address computation
3861 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3862 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003863 SDOperand Hi = StackSlot;
3864 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3865 if (TLI.isLittleEndian())
3866 std::swap(Hi, Lo);
3867
Chris Lattner22cde6a2006-01-28 08:25:58 +00003868 // if signed map to unsigned space
3869 SDOperand Op0Mapped;
3870 if (isSigned) {
3871 // constant used to invert sign bit (signed to unsigned mapping)
3872 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3873 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3874 } else {
3875 Op0Mapped = Op0;
3876 }
3877 // store the lo of the constructed double - based on integer input
3878 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3879 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3880 // initial hi portion of constructed double
3881 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3882 // store the hi of the constructed double - biased exponent
3883 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3884 InitialHi, Hi, DAG.getSrcValue(NULL));
3885 // load the constructed double
3886 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3887 DAG.getSrcValue(NULL));
3888 // FP constant to bias correct the final result
3889 SDOperand Bias = DAG.getConstantFP(isSigned ?
3890 BitsToDouble(0x4330000080000000ULL)
3891 : BitsToDouble(0x4330000000000000ULL),
3892 MVT::f64);
3893 // subtract the bias
3894 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3895 // final result
3896 SDOperand Result;
3897 // handle final rounding
3898 if (DestVT == MVT::f64) {
3899 // do nothing
3900 Result = Sub;
3901 } else {
3902 // if f32 then cast to f32
3903 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3904 }
3905 return Result;
3906 }
3907 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3908 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3909
3910 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3911 DAG.getConstant(0, Op0.getValueType()),
3912 ISD::SETLT);
3913 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3914 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3915 SignSet, Four, Zero);
3916
3917 // If the sign bit of the integer is set, the large number will be treated
3918 // as a negative number. To counteract this, the dynamic code adds an
3919 // offset depending on the data type.
3920 uint64_t FF;
3921 switch (Op0.getValueType()) {
3922 default: assert(0 && "Unsupported integer type!");
3923 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3924 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3925 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3926 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3927 }
3928 if (TLI.isLittleEndian()) FF <<= 32;
3929 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3930
3931 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3932 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3933 SDOperand FudgeInReg;
3934 if (DestVT == MVT::f32)
3935 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3936 DAG.getSrcValue(NULL));
3937 else {
3938 assert(DestVT == MVT::f64 && "Unexpected conversion");
3939 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3940 DAG.getEntryNode(), CPIdx,
3941 DAG.getSrcValue(NULL), MVT::f32));
3942 }
3943
3944 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3945}
3946
3947/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3948/// *INT_TO_FP operation of the specified operand when the target requests that
3949/// we promote it. At this point, we know that the result and operand types are
3950/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3951/// operation that takes a larger input.
3952SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3953 MVT::ValueType DestVT,
3954 bool isSigned) {
3955 // First step, figure out the appropriate *INT_TO_FP operation to use.
3956 MVT::ValueType NewInTy = LegalOp.getValueType();
3957
3958 unsigned OpToUse = 0;
3959
3960 // Scan for the appropriate larger type to use.
3961 while (1) {
3962 NewInTy = (MVT::ValueType)(NewInTy+1);
3963 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3964
3965 // If the target supports SINT_TO_FP of this type, use it.
3966 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3967 default: break;
3968 case TargetLowering::Legal:
3969 if (!TLI.isTypeLegal(NewInTy))
3970 break; // Can't use this datatype.
3971 // FALL THROUGH.
3972 case TargetLowering::Custom:
3973 OpToUse = ISD::SINT_TO_FP;
3974 break;
3975 }
3976 if (OpToUse) break;
3977 if (isSigned) continue;
3978
3979 // If the target supports UINT_TO_FP of this type, use it.
3980 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3981 default: break;
3982 case TargetLowering::Legal:
3983 if (!TLI.isTypeLegal(NewInTy))
3984 break; // Can't use this datatype.
3985 // FALL THROUGH.
3986 case TargetLowering::Custom:
3987 OpToUse = ISD::UINT_TO_FP;
3988 break;
3989 }
3990 if (OpToUse) break;
3991
3992 // Otherwise, try a larger type.
3993 }
3994
3995 // Okay, we found the operation and type to use. Zero extend our input to the
3996 // desired type then run the operation on it.
3997 return DAG.getNode(OpToUse, DestVT,
3998 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3999 NewInTy, LegalOp));
4000}
4001
4002/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4003/// FP_TO_*INT operation of the specified operand when the target requests that
4004/// we promote it. At this point, we know that the result and operand types are
4005/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4006/// operation that returns a larger result.
4007SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4008 MVT::ValueType DestVT,
4009 bool isSigned) {
4010 // First step, figure out the appropriate FP_TO*INT operation to use.
4011 MVT::ValueType NewOutTy = DestVT;
4012
4013 unsigned OpToUse = 0;
4014
4015 // Scan for the appropriate larger type to use.
4016 while (1) {
4017 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4018 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4019
4020 // If the target supports FP_TO_SINT returning this type, use it.
4021 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4022 default: break;
4023 case TargetLowering::Legal:
4024 if (!TLI.isTypeLegal(NewOutTy))
4025 break; // Can't use this datatype.
4026 // FALL THROUGH.
4027 case TargetLowering::Custom:
4028 OpToUse = ISD::FP_TO_SINT;
4029 break;
4030 }
4031 if (OpToUse) break;
4032
4033 // If the target supports FP_TO_UINT of this type, use it.
4034 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4035 default: break;
4036 case TargetLowering::Legal:
4037 if (!TLI.isTypeLegal(NewOutTy))
4038 break; // Can't use this datatype.
4039 // FALL THROUGH.
4040 case TargetLowering::Custom:
4041 OpToUse = ISD::FP_TO_UINT;
4042 break;
4043 }
4044 if (OpToUse) break;
4045
4046 // Otherwise, try a larger type.
4047 }
4048
4049 // Okay, we found the operation and type to use. Truncate the result of the
4050 // extended FP_TO_*INT operation to the desired size.
4051 return DAG.getNode(ISD::TRUNCATE, DestVT,
4052 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4053}
4054
4055/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4056///
4057SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4058 MVT::ValueType VT = Op.getValueType();
4059 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4060 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4061 switch (VT) {
4062 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4063 case MVT::i16:
4064 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4065 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4066 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4067 case MVT::i32:
4068 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4069 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4070 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4071 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4072 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4073 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4074 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4075 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4076 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4077 case MVT::i64:
4078 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4079 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4080 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4081 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4082 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4083 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4084 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4085 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4086 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4087 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4088 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4089 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4090 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4091 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4092 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4093 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4094 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4095 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4096 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4097 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4098 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4099 }
4100}
4101
4102/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4103///
4104SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4105 switch (Opc) {
4106 default: assert(0 && "Cannot expand this yet!");
4107 case ISD::CTPOP: {
4108 static const uint64_t mask[6] = {
4109 0x5555555555555555ULL, 0x3333333333333333ULL,
4110 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4111 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4112 };
4113 MVT::ValueType VT = Op.getValueType();
4114 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4115 unsigned len = getSizeInBits(VT);
4116 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4117 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4118 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4119 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4120 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4121 DAG.getNode(ISD::AND, VT,
4122 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4123 }
4124 return Op;
4125 }
4126 case ISD::CTLZ: {
4127 // for now, we do this:
4128 // x = x | (x >> 1);
4129 // x = x | (x >> 2);
4130 // ...
4131 // x = x | (x >>16);
4132 // x = x | (x >>32); // for 64-bit input
4133 // return popcount(~x);
4134 //
4135 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4136 MVT::ValueType VT = Op.getValueType();
4137 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4138 unsigned len = getSizeInBits(VT);
4139 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4140 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4141 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4142 }
4143 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4144 return DAG.getNode(ISD::CTPOP, VT, Op);
4145 }
4146 case ISD::CTTZ: {
4147 // for now, we use: { return popcount(~x & (x - 1)); }
4148 // unless the target has ctlz but not ctpop, in which case we use:
4149 // { return 32 - nlz(~x & (x-1)); }
4150 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4151 MVT::ValueType VT = Op.getValueType();
4152 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4153 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4154 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4155 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4156 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4157 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4158 TLI.isOperationLegal(ISD::CTLZ, VT))
4159 return DAG.getNode(ISD::SUB, VT,
4160 DAG.getConstant(getSizeInBits(VT), VT),
4161 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4162 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4163 }
4164 }
4165}
Chris Lattnere34b3962005-01-19 04:19:40 +00004166
Chris Lattner3e928bb2005-01-07 07:47:09 +00004167/// ExpandOp - Expand the specified SDOperand into its two component pieces
4168/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4169/// LegalizeNodes map is filled in for any results that are not expanded, the
4170/// ExpandedNodes map is filled in for any results that are expanded, and the
4171/// Lo/Hi values are returned.
4172void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4173 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004174 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004175 SDNode *Node = Op.Val;
4176 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00004177 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4178 "Cannot expand FP values!");
4179 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004180 "Cannot expand to FP value or to larger int value!");
4181
Chris Lattner6fdcb252005-09-02 20:32:45 +00004182 // See if we already expanded it.
4183 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4184 = ExpandedNodes.find(Op);
4185 if (I != ExpandedNodes.end()) {
4186 Lo = I->second.first;
4187 Hi = I->second.second;
4188 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004189 }
4190
Chris Lattner3e928bb2005-01-07 07:47:09 +00004191 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004192 case ISD::CopyFromReg:
4193 assert(0 && "CopyFromReg must be legal!");
4194 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
4196 assert(0 && "Do not know how to expand this operator!");
4197 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004198 case ISD::UNDEF:
4199 Lo = DAG.getNode(ISD::UNDEF, NVT);
4200 Hi = DAG.getNode(ISD::UNDEF, NVT);
4201 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004202 case ISD::Constant: {
4203 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4204 Lo = DAG.getConstant(Cst, NVT);
4205 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4206 break;
4207 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004208 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004209 // Return the operands.
4210 Lo = Node->getOperand(0);
4211 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004212 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004213
4214 case ISD::SIGN_EXTEND_INREG:
4215 ExpandOp(Node->getOperand(0), Lo, Hi);
4216 // Sign extend the lo-part.
4217 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4218 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4219 TLI.getShiftAmountTy()));
4220 // sext_inreg the low part if needed.
4221 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4222 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004223
Nate Begemand88fc032006-01-14 03:14:10 +00004224 case ISD::BSWAP: {
4225 ExpandOp(Node->getOperand(0), Lo, Hi);
4226 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4227 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4228 Lo = TempLo;
4229 break;
4230 }
4231
Chris Lattneredb1add2005-05-11 04:51:16 +00004232 case ISD::CTPOP:
4233 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004234 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4235 DAG.getNode(ISD::CTPOP, NVT, Lo),
4236 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004237 Hi = DAG.getConstant(0, NVT);
4238 break;
4239
Chris Lattner39a8f332005-05-12 19:05:01 +00004240 case ISD::CTLZ: {
4241 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004242 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004243 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4244 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004245 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4246 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004247 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4248 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4249
4250 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4251 Hi = DAG.getConstant(0, NVT);
4252 break;
4253 }
4254
4255 case ISD::CTTZ: {
4256 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004257 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004258 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4259 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004260 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4261 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004262 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4263 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4264
4265 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4266 Hi = DAG.getConstant(0, NVT);
4267 break;
4268 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004269
Nate Begemanacc398c2006-01-25 18:21:52 +00004270 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004271 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4272 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004273 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4274 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4275
4276 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004277 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004278 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4279 if (!TLI.isLittleEndian())
4280 std::swap(Lo, Hi);
4281 break;
4282 }
4283
Chris Lattner3e928bb2005-01-07 07:47:09 +00004284 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004285 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4286 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004287 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004288
4289 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00004290 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004291 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4292 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00004293 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004294 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00004295
4296 // Build a factor node to remember that this load is independent of the
4297 // other one.
4298 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4299 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004300
Chris Lattner3e928bb2005-01-07 07:47:09 +00004301 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004302 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004303 if (!TLI.isLittleEndian())
4304 std::swap(Lo, Hi);
4305 break;
4306 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004307 case ISD::AND:
4308 case ISD::OR:
4309 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4310 SDOperand LL, LH, RL, RH;
4311 ExpandOp(Node->getOperand(0), LL, LH);
4312 ExpandOp(Node->getOperand(1), RL, RH);
4313 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4314 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4315 break;
4316 }
4317 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004318 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004319 ExpandOp(Node->getOperand(1), LL, LH);
4320 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00004321 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4322 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004323 break;
4324 }
Nate Begeman9373a812005-08-10 20:51:12 +00004325 case ISD::SELECT_CC: {
4326 SDOperand TL, TH, FL, FH;
4327 ExpandOp(Node->getOperand(2), TL, TH);
4328 ExpandOp(Node->getOperand(3), FL, FH);
4329 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4330 Node->getOperand(1), TL, FL, Node->getOperand(4));
4331 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4332 Node->getOperand(1), TH, FH, Node->getOperand(4));
4333 break;
4334 }
Nate Begeman144ff662005-10-13 17:15:37 +00004335 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004336 SDOperand Chain = Node->getOperand(0);
4337 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004338 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4339
4340 if (EVT == NVT)
4341 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4342 else
4343 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4344 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004345
4346 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004347 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004348
Nate Begeman144ff662005-10-13 17:15:37 +00004349 // The high part is obtained by SRA'ing all but one of the bits of the lo
4350 // part.
4351 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4352 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4353 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00004354 break;
4355 }
4356 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004357 SDOperand Chain = Node->getOperand(0);
4358 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004359 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4360
4361 if (EVT == NVT)
4362 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4363 else
4364 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4365 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004366
4367 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004368 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004369
Nate Begeman144ff662005-10-13 17:15:37 +00004370 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004371 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004372 break;
4373 }
4374 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004375 SDOperand Chain = Node->getOperand(0);
4376 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00004377 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4378
4379 if (EVT == NVT)
4380 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4381 else
4382 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4383 EVT);
4384
4385 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004386 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004387
4388 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004389 Hi = DAG.getNode(ISD::UNDEF, NVT);
4390 break;
4391 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004392 case ISD::ANY_EXTEND:
4393 // The low part is any extension of the input (which degenerates to a copy).
4394 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4395 // The high part is undefined.
4396 Hi = DAG.getNode(ISD::UNDEF, NVT);
4397 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004398 case ISD::SIGN_EXTEND: {
4399 // The low part is just a sign extension of the input (which degenerates to
4400 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004401 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004402
Chris Lattner3e928bb2005-01-07 07:47:09 +00004403 // The high part is obtained by SRA'ing all but one of the bits of the lo
4404 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004405 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004406 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4407 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004408 break;
4409 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004410 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004411 // The low part is just a zero extension of the input (which degenerates to
4412 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004413 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004414
Chris Lattner3e928bb2005-01-07 07:47:09 +00004415 // The high part is just a zero.
4416 Hi = DAG.getConstant(0, NVT);
4417 break;
Chris Lattner35481892005-12-23 00:16:34 +00004418
4419 case ISD::BIT_CONVERT: {
4420 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4421 Node->getOperand(0));
4422 ExpandOp(Tmp, Lo, Hi);
4423 break;
4424 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004425
Chris Lattner8137c9e2006-01-28 05:07:51 +00004426 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004427 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4428 TargetLowering::Custom &&
4429 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004430 Lo = TLI.LowerOperation(Op, DAG);
4431 assert(Lo.Val && "Node must be custom expanded!");
4432 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004433 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004434 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004435 break;
4436
Chris Lattner4e6c7462005-01-08 19:27:05 +00004437 // These operators cannot be expanded directly, emit them as calls to
4438 // library functions.
4439 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004440 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004441 SDOperand Op;
4442 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4443 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004444 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4445 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004446 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004447
Chris Lattnerf20d1832005-07-30 01:40:57 +00004448 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4449
Chris Lattner80a3e942005-07-29 00:33:32 +00004450 // Now that the custom expander is done, expand the result, which is still
4451 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004452 if (Op.Val) {
4453 ExpandOp(Op, Lo, Hi);
4454 break;
4455 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004456 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004457
Chris Lattner4e6c7462005-01-08 19:27:05 +00004458 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004459 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004460 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004461 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004462 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004463
Chris Lattner4e6c7462005-01-08 19:27:05 +00004464 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004465 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004466 SDOperand Op;
4467 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4468 case Expand: assert(0 && "cannot expand FP!");
4469 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4470 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4471 }
4472
4473 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4474
4475 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004476 if (Op.Val) {
4477 ExpandOp(Op, Lo, Hi);
4478 break;
4479 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004480 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004481
Chris Lattner4e6c7462005-01-08 19:27:05 +00004482 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004483 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004484 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004485 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004486 break;
4487
Evan Cheng05a2d562006-01-09 18:31:59 +00004488 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004489 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004490 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004491 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004492 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004493 Op = TLI.LowerOperation(Op, DAG);
4494 if (Op.Val) {
4495 // Now that the custom expander is done, expand the result, which is
4496 // still VT.
4497 ExpandOp(Op, Lo, Hi);
4498 break;
4499 }
4500 }
4501
Chris Lattnere34b3962005-01-19 04:19:40 +00004502 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004503 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004504 break;
Chris Lattner47599822005-04-02 03:38:53 +00004505
4506 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004507 TargetLowering::LegalizeAction Action =
4508 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4509 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4510 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004511 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004512 break;
4513 }
4514
Chris Lattnere34b3962005-01-19 04:19:40 +00004515 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004516 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004517 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004518 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004519
Evan Cheng05a2d562006-01-09 18:31:59 +00004520 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004521 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004522 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004523 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004524 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004525 Op = TLI.LowerOperation(Op, DAG);
4526 if (Op.Val) {
4527 // Now that the custom expander is done, expand the result, which is
4528 // still VT.
4529 ExpandOp(Op, Lo, Hi);
4530 break;
4531 }
4532 }
4533
Chris Lattnere34b3962005-01-19 04:19:40 +00004534 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004535 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004536 break;
Chris Lattner47599822005-04-02 03:38:53 +00004537
4538 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004539 TargetLowering::LegalizeAction Action =
4540 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4541 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4542 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004543 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004544 break;
4545 }
4546
Chris Lattnere34b3962005-01-19 04:19:40 +00004547 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004548 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004549 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004550 }
4551
4552 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004553 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004554 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004555 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004556 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004557 Op = TLI.LowerOperation(Op, DAG);
4558 if (Op.Val) {
4559 // Now that the custom expander is done, expand the result, which is
4560 // still VT.
4561 ExpandOp(Op, Lo, Hi);
4562 break;
4563 }
4564 }
4565
Chris Lattnere34b3962005-01-19 04:19:40 +00004566 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004567 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004568 break;
Chris Lattner47599822005-04-02 03:38:53 +00004569
4570 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004571 TargetLowering::LegalizeAction Action =
4572 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4573 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4574 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004575 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004576 break;
4577 }
4578
Chris Lattnere34b3962005-01-19 04:19:40 +00004579 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004580 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004581 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004582 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004583
Misha Brukmanedf128a2005-04-21 22:36:52 +00004584 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004585 case ISD::SUB: {
4586 // If the target wants to custom expand this, let them.
4587 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4588 TargetLowering::Custom) {
4589 Op = TLI.LowerOperation(Op, DAG);
4590 if (Op.Val) {
4591 ExpandOp(Op, Lo, Hi);
4592 break;
4593 }
4594 }
4595
4596 // Expand the subcomponents.
4597 SDOperand LHSL, LHSH, RHSL, RHSH;
4598 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4599 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004600 std::vector<MVT::ValueType> VTs;
4601 std::vector<SDOperand> LoOps, HiOps;
4602 VTs.push_back(LHSL.getValueType());
4603 VTs.push_back(MVT::Flag);
4604 LoOps.push_back(LHSL);
4605 LoOps.push_back(RHSL);
4606 HiOps.push_back(LHSH);
4607 HiOps.push_back(RHSH);
4608 if (Node->getOpcode() == ISD::ADD) {
4609 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4610 HiOps.push_back(Lo.getValue(1));
4611 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4612 } else {
4613 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4614 HiOps.push_back(Lo.getValue(1));
4615 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4616 }
Chris Lattner84f67882005-01-20 18:52:28 +00004617 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004618 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004619 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004620 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004621 SDOperand LL, LH, RL, RH;
4622 ExpandOp(Node->getOperand(0), LL, LH);
4623 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004624 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4625 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4626 // extended the sign bit of the low half through the upper half, and if so
4627 // emit a MULHS instead of the alternate sequence that is valid for any
4628 // i64 x i64 multiply.
4629 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4630 // is RH an extension of the sign bit of RL?
4631 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4632 RH.getOperand(1).getOpcode() == ISD::Constant &&
4633 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4634 // is LH an extension of the sign bit of LL?
4635 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4636 LH.getOperand(1).getOpcode() == ISD::Constant &&
4637 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4638 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4639 } else {
4640 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4641 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4642 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4643 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4644 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4645 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004646 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4647 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004648 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004649 }
4650 break;
4651 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004652 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4653 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4654 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4655 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004656 }
4657
Chris Lattner83397362005-12-21 18:02:52 +00004658 // Make sure the resultant values have been legalized themselves, unless this
4659 // is a type that requires multi-step expansion.
4660 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4661 Lo = LegalizeOp(Lo);
4662 Hi = LegalizeOp(Hi);
4663 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004664
4665 // Remember in a map if the values will be reused later.
4666 bool isNew =
4667 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4668 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004669}
4670
Chris Lattnerc7029802006-03-18 01:44:44 +00004671/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4672/// two smaller values of MVT::Vector type.
4673void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4674 SDOperand &Hi) {
4675 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4676 SDNode *Node = Op.Val;
4677 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4678 assert(NumElements > 1 && "Cannot split a single element vector!");
4679 unsigned NewNumElts = NumElements/2;
4680 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4681 SDOperand TypeNode = *(Node->op_end()-1);
4682
4683 // See if we already split it.
4684 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4685 = SplitNodes.find(Op);
4686 if (I != SplitNodes.end()) {
4687 Lo = I->second.first;
4688 Hi = I->second.second;
4689 return;
4690 }
4691
4692 switch (Node->getOpcode()) {
Chris Lattner3824e502006-04-14 06:08:35 +00004693 default: Node->dump(); assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004694 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004695 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4696 LoOps.push_back(NewNumEltsNode);
4697 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004698 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004699
4700 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4701 HiOps.push_back(NewNumEltsNode);
4702 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004703 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004704 break;
4705 }
4706 case ISD::VADD:
4707 case ISD::VSUB:
4708 case ISD::VMUL:
4709 case ISD::VSDIV:
4710 case ISD::VUDIV:
4711 case ISD::VAND:
4712 case ISD::VOR:
4713 case ISD::VXOR: {
4714 SDOperand LL, LH, RL, RH;
4715 SplitVectorOp(Node->getOperand(0), LL, LH);
4716 SplitVectorOp(Node->getOperand(1), RL, RH);
4717
4718 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4719 NewNumEltsNode, TypeNode);
4720 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4721 NewNumEltsNode, TypeNode);
4722 break;
4723 }
4724 case ISD::VLOAD: {
4725 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4726 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4727 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4728
4729 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4730 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4731 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4732 getIntPtrConstant(IncrementSize));
4733 // FIXME: This creates a bogus srcvalue!
4734 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4735
4736 // Build a factor node to remember that this load is independent of the
4737 // other one.
4738 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4739 Hi.getValue(1));
4740
4741 // Remember that we legalized the chain.
4742 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00004743 break;
4744 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004745 case ISD::VBIT_CONVERT: {
4746 // We know the result is a vector. The input may be either a vector or a
4747 // scalar value.
4748 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4749 // Lower to a store/load. FIXME: this could be improved probably.
4750 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4751
4752 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4753 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4754 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4755 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4756 SplitVectorOp(St, Lo, Hi);
4757 } else {
4758 // If the input is a vector type, we have to either scalarize it, pack it
4759 // or convert it based on whether the input vector type is legal.
4760 SDNode *InVal = Node->getOperand(0).Val;
4761 unsigned NumElems =
4762 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4763 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4764
4765 // If the input is from a single element vector, scalarize the vector,
4766 // then treat like a scalar.
4767 if (NumElems == 1) {
4768 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4769 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4770 Op.getOperand(1), Op.getOperand(2));
4771 SplitVectorOp(Scalar, Lo, Hi);
4772 } else {
4773 // Split the input vector.
4774 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4775
4776 // Convert each of the pieces now.
4777 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4778 NewNumEltsNode, TypeNode);
4779 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4780 NewNumEltsNode, TypeNode);
4781 }
4782 break;
4783 }
4784 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004785 }
4786
4787 // Remember in a map if the values will be reused later.
4788 bool isNew =
4789 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4790 assert(isNew && "Value already expanded?!?");
4791}
4792
4793
4794/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4795/// equivalent operation that returns a scalar (e.g. F32) or packed value
4796/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4797/// type for the result.
4798SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4799 MVT::ValueType NewVT) {
4800 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4801 SDNode *Node = Op.Val;
4802
4803 // See if we already packed it.
4804 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4805 if (I != PackedNodes.end()) return I->second;
4806
4807 SDOperand Result;
4808 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004809 default:
4810 Node->dump(); std::cerr << "\n";
4811 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004812 case ISD::VADD:
4813 case ISD::VSUB:
4814 case ISD::VMUL:
4815 case ISD::VSDIV:
4816 case ISD::VUDIV:
4817 case ISD::VAND:
4818 case ISD::VOR:
4819 case ISD::VXOR:
4820 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4821 NewVT,
4822 PackVectorOp(Node->getOperand(0), NewVT),
4823 PackVectorOp(Node->getOperand(1), NewVT));
4824 break;
4825 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004826 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4827 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004828
Chris Lattner4794a6b2006-03-19 00:07:49 +00004829 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004830
4831 // Remember that we legalized the chain.
4832 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4833 break;
4834 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004835 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00004836 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004837 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004838 Result = Node->getOperand(0);
4839 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004840 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00004841
4842 // If all elements of the build_vector are undefs, return an undef.
4843 bool AllUndef = true;
4844 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
4845 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
4846 AllUndef = false;
4847 break;
4848 }
4849 if (AllUndef) {
4850 Result = DAG.getNode(ISD::UNDEF, NewVT);
4851 } else {
4852 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4853 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
4854 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004855 }
4856 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004857 case ISD::VINSERT_VECTOR_ELT:
4858 if (!MVT::isVector(NewVT)) {
4859 // Returning a scalar? Must be the inserted element.
4860 Result = Node->getOperand(1);
4861 } else {
4862 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4863 PackVectorOp(Node->getOperand(0), NewVT),
4864 Node->getOperand(1), Node->getOperand(2));
4865 }
4866 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00004867 case ISD::VVECTOR_SHUFFLE:
4868 if (!MVT::isVector(NewVT)) {
4869 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4870 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4871 if (cast<ConstantSDNode>(EltNum)->getValue())
4872 Result = PackVectorOp(Node->getOperand(1), NewVT);
4873 else
4874 Result = PackVectorOp(Node->getOperand(0), NewVT);
4875 } else {
4876 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4877 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4878 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4879 Node->getOperand(2).Val->op_end()-2);
4880 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4881 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4882
4883 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4884 PackVectorOp(Node->getOperand(0), NewVT),
4885 PackVectorOp(Node->getOperand(1), NewVT), BV);
4886 }
4887 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004888 case ISD::VBIT_CONVERT:
4889 if (Op.getOperand(0).getValueType() != MVT::Vector)
4890 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4891 else {
4892 // If the input is a vector type, we have to either scalarize it, pack it
4893 // or convert it based on whether the input vector type is legal.
4894 SDNode *InVal = Node->getOperand(0).Val;
4895 unsigned NumElems =
4896 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4897 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4898
4899 // Figure out if there is a Packed type corresponding to this Vector
4900 // type. If so, convert to the packed type.
4901 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4902 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4903 // Turn this into a bit convert of the packed input.
4904 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4905 PackVectorOp(Node->getOperand(0), TVT));
4906 break;
4907 } else if (NumElems == 1) {
4908 // Turn this into a bit convert of the scalar input.
4909 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4910 PackVectorOp(Node->getOperand(0), EVT));
4911 break;
4912 } else {
4913 // FIXME: UNIMP!
4914 assert(0 && "Cast from unsupported vector type not implemented yet!");
4915 }
4916 }
Evan Chengdb3c6262006-04-10 18:54:36 +00004917 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004918 case ISD::VSELECT:
4919 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
4920 PackVectorOp(Op.getOperand(1), NewVT),
4921 PackVectorOp(Op.getOperand(2), NewVT));
4922 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004923 }
4924
4925 if (TLI.isTypeLegal(NewVT))
4926 Result = LegalizeOp(Result);
4927 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4928 assert(isNew && "Value already packed?");
4929 return Result;
4930}
4931
Chris Lattner3e928bb2005-01-07 07:47:09 +00004932
4933// SelectionDAG::Legalize - This is the entry point for the file.
4934//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004935void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00004936 if (ViewLegalizeDAGs) viewGraph();
4937
Chris Lattner3e928bb2005-01-07 07:47:09 +00004938 /// run - This is the main entry point to this class.
4939 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004940 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004941}
4942