blob: d635747d8010ab092ff5e19fb46123762656ab42 [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:
513 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000514 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000515 case ISD::TargetConstantPool:
516 case ISD::TargetGlobalAddress:
517 case ISD::TargetExternalSymbol:
518 case ISD::VALUETYPE:
519 case ISD::SRCVALUE:
520 case ISD::STRING:
521 case ISD::CONDCODE:
522 // Primitives must all be legal.
523 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
524 "This must be legal!");
525 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000526 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000527 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
528 // If this is a target node, legalize it by legalizing the operands then
529 // passing it through.
530 std::vector<SDOperand> Ops;
531 bool Changed = false;
532 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
533 Ops.push_back(LegalizeOp(Node->getOperand(i)));
534 Changed = Changed || Node->getOperand(i) != Ops.back();
535 }
536 if (Changed)
537 if (Node->getNumValues() == 1)
538 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
539 else {
540 std::vector<MVT::ValueType> VTs(Node->value_begin(),
541 Node->value_end());
542 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
543 }
544
545 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
546 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
547 return Result.getValue(Op.ResNo);
548 }
549 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000550 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
551 assert(0 && "Do not know how to legalize this operator!");
552 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000553 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000554 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000555 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000556 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
557 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000558 case TargetLowering::Custom:
559 Tmp1 = TLI.LowerOperation(Op, DAG);
560 if (Tmp1.Val) Result = Tmp1;
561 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000562 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000563 break;
564 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000565 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000566 case ISD::AssertSext:
567 case ISD::AssertZext:
568 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000569 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000570 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000571 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000572 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000573 Result = Node->getOperand(Op.ResNo);
574 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000575 case ISD::CopyFromReg:
576 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000577 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000578 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000580 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000581 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000582 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000583 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000584 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
585 } else {
586 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
587 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000588 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
589 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000590 // Since CopyFromReg produces two values, make sure to remember that we
591 // legalized both of them.
592 AddLegalizedOperand(Op.getValue(0), Result);
593 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
594 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000595 case ISD::UNDEF: {
596 MVT::ValueType VT = Op.getValueType();
597 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000598 default: assert(0 && "This action is not supported yet!");
599 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000600 if (MVT::isInteger(VT))
601 Result = DAG.getConstant(0, VT);
602 else if (MVT::isFloatingPoint(VT))
603 Result = DAG.getConstantFP(0, VT);
604 else
605 assert(0 && "Unknown value type!");
606 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000607 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000608 break;
609 }
610 break;
611 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000612
Chris Lattner48b61a72006-03-28 00:40:33 +0000613 case ISD::INTRINSIC_W_CHAIN:
614 case ISD::INTRINSIC_WO_CHAIN:
615 case ISD::INTRINSIC_VOID: {
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000616 std::vector<SDOperand> Ops;
617 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
618 Ops.push_back(LegalizeOp(Node->getOperand(i)));
619 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner10d7fa62006-03-26 09:12:51 +0000620
621 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000622 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000623 TargetLowering::Custom) {
624 Tmp3 = TLI.LowerOperation(Result, DAG);
625 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000626 }
627
628 if (Result.Val->getNumValues() == 1) break;
629
630 // Must have return value and chain result.
631 assert(Result.Val->getNumValues() == 2 &&
632 "Cannot return more than two values!");
633
634 // Since loads produce two values, make sure to remember that we
635 // legalized both of them.
636 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
637 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
638 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000639 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000640
641 case ISD::LOCATION:
642 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
643 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
644
645 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
646 case TargetLowering::Promote:
647 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000648 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000649 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000650 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
651 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
652
653 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
654 const std::string &FName =
655 cast<StringSDNode>(Node->getOperand(3))->getValue();
656 const std::string &DirName =
657 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000658 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000659
Jim Laskeye81aecb2005-12-21 20:51:37 +0000660 std::vector<SDOperand> Ops;
661 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000662 SDOperand LineOp = Node->getOperand(1);
663 SDOperand ColOp = Node->getOperand(2);
664
665 if (useDEBUG_LOC) {
666 Ops.push_back(LineOp); // line #
667 Ops.push_back(ColOp); // col #
668 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
669 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
670 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000671 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
672 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000673 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
674 Ops.push_back(DAG.getConstant(ID, MVT::i32));
675 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
676 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000677 } else {
678 Result = Tmp1; // chain
679 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000680 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000681 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000682 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000683 if (Tmp1 != Node->getOperand(0) ||
684 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000685 std::vector<SDOperand> Ops;
686 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000687 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
688 Ops.push_back(Node->getOperand(1)); // line # must be legal.
689 Ops.push_back(Node->getOperand(2)); // col # must be legal.
690 } else {
691 // Otherwise promote them.
692 Ops.push_back(PromoteOp(Node->getOperand(1)));
693 Ops.push_back(PromoteOp(Node->getOperand(2)));
694 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000695 Ops.push_back(Node->getOperand(3)); // filename must be legal.
696 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000697 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000698 }
699 break;
700 }
701 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000702
703 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000704 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000705 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000706 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000707 case TargetLowering::Legal:
708 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
709 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
710 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
711 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000712 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000713 break;
714 }
715 break;
716
717 case ISD::DEBUG_LABEL:
718 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
719 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000720 default: assert(0 && "This action is not supported yet!");
721 case TargetLowering::Legal:
722 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
723 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000724 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000725 break;
726 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000727 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000728
Chris Lattner3e928bb2005-01-07 07:47:09 +0000729 case ISD::Constant:
730 // We know we don't need to expand constants here, constants only have one
731 // value and we check that it is fine above.
732
733 // FIXME: Maybe we should handle things like targets that don't support full
734 // 32-bit immediates?
735 break;
736 case ISD::ConstantFP: {
737 // Spill FP immediates to the constant pool if the target cannot directly
738 // codegen them. Targets often have some immediate values that can be
739 // efficiently generated into an FP register without a load. We explicitly
740 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000741 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
742
743 // Check to see if this FP immediate is already legal.
744 bool isLegal = false;
745 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
746 E = TLI.legal_fpimm_end(); I != E; ++I)
747 if (CFP->isExactlyValue(*I)) {
748 isLegal = true;
749 break;
750 }
751
Chris Lattner3181a772006-01-29 06:26:56 +0000752 // If this is a legal constant, turn it into a TargetConstantFP node.
753 if (isLegal) {
754 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
755 break;
756 }
757
758 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
759 default: assert(0 && "This action is not supported yet!");
760 case TargetLowering::Custom:
761 Tmp3 = TLI.LowerOperation(Result, DAG);
762 if (Tmp3.Val) {
763 Result = Tmp3;
764 break;
765 }
766 // FALLTHROUGH
767 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000768 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 bool Extend = false;
770
Chris Lattner456a93a2006-01-28 07:39:30 +0000771 // If a FP immediate is precise when represented as a float and if the
772 // target can do an extending load from float to double, we put it into
773 // the constant pool as a float, even if it's is statically typed as a
774 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000775 MVT::ValueType VT = CFP->getValueType(0);
776 bool isDouble = VT == MVT::f64;
777 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
778 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000779 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
780 // Only do this if the target has a native EXTLOAD instruction from
781 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000782 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000783 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
784 VT = MVT::f32;
785 Extend = true;
786 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000787
Chris Lattner456a93a2006-01-28 07:39:30 +0000788 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000789 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000790 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
791 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000792 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000793 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
794 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000795 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000796 }
797 break;
798 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000799 case ISD::TokenFactor:
800 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000801 Tmp1 = LegalizeOp(Node->getOperand(0));
802 Tmp2 = LegalizeOp(Node->getOperand(1));
803 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
804 } else if (Node->getNumOperands() == 3) {
805 Tmp1 = LegalizeOp(Node->getOperand(0));
806 Tmp2 = LegalizeOp(Node->getOperand(1));
807 Tmp3 = LegalizeOp(Node->getOperand(2));
808 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000809 } else {
810 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000811 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000812 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
813 Ops.push_back(LegalizeOp(Node->getOperand(i)));
814 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000815 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000816 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000817
818 case ISD::FORMAL_ARGUMENTS:
819 // The only option for this is to custom lower it.
820 Result = TLI.LowerOperation(Result, DAG);
821 assert(Result.Val && "Target didn't custom lower ISD::FORMAL_ARGUMENTS!");
822 break;
823
Chris Lattnerb2827b02006-03-19 00:52:58 +0000824 case ISD::BUILD_VECTOR:
825 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000826 default: assert(0 && "This action is not supported yet!");
827 case TargetLowering::Custom:
828 Tmp3 = TLI.LowerOperation(Result, DAG);
829 if (Tmp3.Val) {
830 Result = Tmp3;
831 break;
832 }
833 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000834 case TargetLowering::Expand:
835 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000836 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000837 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000838 break;
839 case ISD::INSERT_VECTOR_ELT:
840 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
841 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
842 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
843 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
844
845 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
846 Node->getValueType(0))) {
847 default: assert(0 && "This action is not supported yet!");
848 case TargetLowering::Legal:
849 break;
850 case TargetLowering::Custom:
851 Tmp3 = TLI.LowerOperation(Result, DAG);
852 if (Tmp3.Val) {
853 Result = Tmp3;
854 break;
855 }
856 // FALLTHROUGH
857 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000858 // If the insert index is a constant, codegen this as a scalar_to_vector,
859 // then a shuffle that inserts it into the right position in the vector.
860 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
861 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
862 Tmp1.getValueType(), Tmp2);
863
864 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
865 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
866 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
867
868 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
869 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
870 // the RHS.
871 std::vector<SDOperand> ShufOps;
872 for (unsigned i = 0; i != NumElts; ++i) {
873 if (i != InsertPos->getValue())
874 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
875 else
876 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
877 }
878 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,ShufOps);
879
880 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
881 Tmp1, ScVec, ShufMask);
882 Result = LegalizeOp(Result);
883 break;
884 }
885
Chris Lattner2332b9f2006-03-19 01:17:20 +0000886 // If the target doesn't support this, we have to spill the input vector
887 // to a temporary stack slot, update the element, then reload it. This is
888 // badness. We could also load the value into a vector register (either
889 // with a "move to register" or "extload into register" instruction, then
890 // permute it into place, if the idx is a constant and if the idx is
891 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000892 MVT::ValueType VT = Tmp1.getValueType();
893 MVT::ValueType EltVT = Tmp2.getValueType();
894 MVT::ValueType IdxVT = Tmp3.getValueType();
895 MVT::ValueType PtrVT = TLI.getPointerTy();
896 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000897 // Store the vector.
898 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
899 Tmp1, StackPtr, DAG.getSrcValue(NULL));
900
901 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000902 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
903 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000904 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000905 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
906 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
907 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000908 // Store the scalar value.
909 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
910 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
911 // Load the updated vector.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000912 Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL));
Chris Lattner2332b9f2006-03-19 01:17:20 +0000913 break;
914 }
915 }
916 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000917 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000918 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
919 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
920 break;
921 }
922
Chris Lattnerce872152006-03-19 06:31:19 +0000923 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
924 Result = DAG.UpdateNodeOperands(Result, Tmp1);
925 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
926 Node->getValueType(0))) {
927 default: assert(0 && "This action is not supported yet!");
928 case TargetLowering::Legal:
929 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000930 case TargetLowering::Custom:
931 Tmp3 = TLI.LowerOperation(Result, DAG);
932 if (Tmp3.Val) {
933 Result = Tmp3;
934 break;
935 }
936 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +0000937 case TargetLowering::Expand:
938 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +0000939 break;
940 }
Chris Lattnerce872152006-03-19 06:31:19 +0000941 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000942 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +0000943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
944 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
945 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
946
947 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +0000948 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
949 default: assert(0 && "Unknown operation action!");
950 case TargetLowering::Legal:
951 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
952 "vector shuffle should not be created if not legal!");
953 break;
954 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +0000955 Tmp3 = TLI.LowerOperation(Result, DAG);
956 if (Tmp3.Val) {
957 Result = Tmp3;
958 break;
959 }
960 // FALLTHROUGH
961 case TargetLowering::Expand: {
962 MVT::ValueType VT = Node->getValueType(0);
963 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
964 MVT::ValueType PtrVT = TLI.getPointerTy();
965 SDOperand Mask = Node->getOperand(2);
966 unsigned NumElems = Mask.getNumOperands();
967 std::vector<SDOperand> Ops;
968 for (unsigned i = 0; i != NumElems; ++i) {
969 SDOperand Arg = Mask.getOperand(i);
970 if (Arg.getOpcode() == ISD::UNDEF) {
971 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
972 } else {
973 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
974 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
975 if (Idx < NumElems)
976 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
977 DAG.getConstant(Idx, PtrVT)));
978 else
979 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
980 DAG.getConstant(Idx - NumElems, PtrVT)));
981 }
982 }
983 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
Chris Lattner4352cc92006-04-04 17:23:26 +0000984 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +0000985 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000986 case TargetLowering::Promote: {
987 // Change base type to a different vector type.
988 MVT::ValueType OVT = Node->getValueType(0);
989 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
990
991 // Cast the two input vectors.
992 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
993 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
994
995 // Convert the shuffle mask to the right # elements.
996 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
997 assert(Tmp3.Val && "Shuffle not legal?");
998 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
999 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1000 break;
1001 }
Chris Lattner87100e02006-03-20 01:52:29 +00001002 }
1003 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001004
1005 case ISD::EXTRACT_VECTOR_ELT:
1006 Tmp1 = LegalizeOp(Node->getOperand(0));
1007 Tmp2 = LegalizeOp(Node->getOperand(1));
1008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001009
1010 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1011 Tmp1.getValueType())) {
1012 default: assert(0 && "This action is not supported yet!");
1013 case TargetLowering::Legal:
1014 break;
1015 case TargetLowering::Custom:
1016 Tmp3 = TLI.LowerOperation(Result, DAG);
1017 if (Tmp3.Val) {
1018 Result = Tmp3;
1019 break;
1020 }
1021 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001022 case TargetLowering::Expand:
1023 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001024 break;
1025 }
Chris Lattner384504c2006-03-21 20:44:12 +00001026 break;
1027
Chris Lattner15972212006-03-31 17:55:51 +00001028 case ISD::VEXTRACT_VECTOR_ELT:
1029 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001030 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001031
Chris Lattner6831a812006-02-13 09:18:02 +00001032 case ISD::CALLSEQ_START: {
1033 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1034
1035 // Recursively Legalize all of the inputs of the call end that do not lead
1036 // to this call start. This ensures that any libcalls that need be inserted
1037 // are inserted *before* the CALLSEQ_START.
1038 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1039 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
1040
1041 // Now that we legalized all of the inputs (which may have inserted
1042 // libcalls) create the new CALLSEQ_START node.
1043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1044
1045 // Merge in the last call, to ensure that this call start after the last
1046 // call ended.
1047 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1048 Tmp1 = LegalizeOp(Tmp1);
1049
1050 // Do not try to legalize the target-specific arguments (#1+).
1051 if (Tmp1 != Node->getOperand(0)) {
1052 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1053 Ops[0] = Tmp1;
1054 Result = DAG.UpdateNodeOperands(Result, Ops);
1055 }
1056
1057 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001058 AddLegalizedOperand(Op.getValue(0), Result);
1059 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1060 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1061
Chris Lattner6831a812006-02-13 09:18:02 +00001062 // Now that the callseq_start and all of the non-call nodes above this call
1063 // sequence have been legalized, legalize the call itself. During this
1064 // process, no libcalls can/will be inserted, guaranteeing that no calls
1065 // can overlap.
1066 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1067 SDOperand InCallSEQ = LastCALLSEQ_END;
1068 // Note that we are selecting this call!
1069 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1070 IsLegalizingCall = true;
1071
1072 // Legalize the call, starting from the CALLSEQ_END.
1073 LegalizeOp(LastCALLSEQ_END);
1074 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1075 return Result;
1076 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001077 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001078 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1079 // will cause this node to be legalized as well as handling libcalls right.
1080 if (LastCALLSEQ_END.Val != Node) {
1081 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1082 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1083 assert(I != LegalizedNodes.end() &&
1084 "Legalizing the call start should have legalized this node!");
1085 return I->second;
1086 }
1087
1088 // Otherwise, the call start has been legalized and everything is going
1089 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001091 // Do not try to legalize the target-specific arguments (#1+), except for
1092 // an optional flag input.
1093 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1094 if (Tmp1 != Node->getOperand(0)) {
1095 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1096 Ops[0] = Tmp1;
1097 Result = DAG.UpdateNodeOperands(Result, Ops);
1098 }
1099 } else {
1100 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1101 if (Tmp1 != Node->getOperand(0) ||
1102 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1103 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1104 Ops[0] = Tmp1;
1105 Ops.back() = Tmp2;
1106 Result = DAG.UpdateNodeOperands(Result, Ops);
1107 }
Chris Lattner6a542892006-01-24 05:48:21 +00001108 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001109 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001110 // This finishes up call legalization.
1111 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001112
1113 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1114 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1115 if (Node->getNumValues() == 2)
1116 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1117 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001118 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001119 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1120 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1121 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001122 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001123
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001124 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001125 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001126 switch (TLI.getOperationAction(Node->getOpcode(),
1127 Node->getValueType(0))) {
1128 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001129 case TargetLowering::Expand: {
1130 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1131 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1132 " not tell us which reg is the stack pointer!");
1133 SDOperand Chain = Tmp1.getOperand(0);
1134 SDOperand Size = Tmp2.getOperand(1);
1135 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1136 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1137 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001138 Tmp1 = LegalizeOp(Tmp1);
1139 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001140 break;
1141 }
1142 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001143 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1144 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001145 Tmp1 = LegalizeOp(Tmp3);
1146 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001147 }
Chris Lattner903d2782006-01-15 08:54:32 +00001148 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001149 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001150 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001151 }
Chris Lattner903d2782006-01-15 08:54:32 +00001152 // Since this op produce two values, make sure to remember that we
1153 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001154 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1155 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001156 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001157 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001158 case ISD::INLINEASM:
1159 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1160 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001161 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +00001162 Tmp2 = Tmp3 = SDOperand(0, 0);
1163 else
1164 Tmp3 = LegalizeOp(Tmp2);
1165
1166 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1167 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1168 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001169 if (Tmp3.Val) Ops.back() = Tmp3;
1170 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001171 }
1172
1173 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001174 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001175 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1176 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001177 case ISD::BR:
1178 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001179 // Ensure that libcalls are emitted before a branch.
1180 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1181 Tmp1 = LegalizeOp(Tmp1);
1182 LastCALLSEQ_END = DAG.getEntryNode();
1183
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001185 break;
1186
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001187 case ISD::BRCOND:
1188 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001189 // Ensure that libcalls are emitted before a return.
1190 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1191 Tmp1 = LegalizeOp(Tmp1);
1192 LastCALLSEQ_END = DAG.getEntryNode();
1193
Chris Lattner47e92232005-01-18 19:27:06 +00001194 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1195 case Expand: assert(0 && "It's impossible to expand bools");
1196 case Legal:
1197 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1198 break;
1199 case Promote:
1200 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1201 break;
1202 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001203
1204 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001206
1207 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1208 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001209 case TargetLowering::Legal: break;
1210 case TargetLowering::Custom:
1211 Tmp1 = TLI.LowerOperation(Result, DAG);
1212 if (Tmp1.Val) Result = Tmp1;
1213 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001214 case TargetLowering::Expand:
1215 // Expand brcond's setcc into its constituent parts and create a BR_CC
1216 // Node.
1217 if (Tmp2.getOpcode() == ISD::SETCC) {
1218 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1219 Tmp2.getOperand(0), Tmp2.getOperand(1),
1220 Node->getOperand(2));
1221 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001222 // Make sure the condition is either zero or one. It may have been
1223 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001224 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1225 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1226 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001227
Nate Begeman7cbd5252005-08-16 19:49:35 +00001228 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1229 DAG.getCondCode(ISD::SETNE), Tmp2,
1230 DAG.getConstant(0, Tmp2.getValueType()),
1231 Node->getOperand(2));
1232 }
1233 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001234 }
1235 break;
1236 case ISD::BR_CC:
1237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001238 // Ensure that libcalls are emitted before a branch.
1239 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1240 Tmp1 = LegalizeOp(Tmp1);
1241 LastCALLSEQ_END = DAG.getEntryNode();
1242
Nate Begeman750ac1b2006-02-01 07:19:44 +00001243 Tmp2 = Node->getOperand(2); // LHS
1244 Tmp3 = Node->getOperand(3); // RHS
1245 Tmp4 = Node->getOperand(1); // CC
1246
1247 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1248
1249 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1250 // the LHS is a legal SETCC itself. In this case, we need to compare
1251 // the result against zero to select between true and false values.
1252 if (Tmp3.Val == 0) {
1253 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1254 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001255 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001256
1257 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1258 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001259
Chris Lattner181b7a32005-12-17 23:46:46 +00001260 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1261 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001262 case TargetLowering::Legal: break;
1263 case TargetLowering::Custom:
1264 Tmp4 = TLI.LowerOperation(Result, DAG);
1265 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001266 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001267 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001268 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001269 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001270 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1271 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001272
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001273 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001274 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001275 Tmp3 = Result.getValue(0);
1276 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001277
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001278 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1279 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001280 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001281 case TargetLowering::Custom:
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001282 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001283 if (Tmp1.Val) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001284 Tmp3 = LegalizeOp(Tmp1);
1285 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001286 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001287 break;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001288 case TargetLowering::Promote: {
1289 // Only promote a load of vector type to another.
1290 assert(MVT::isVector(VT) && "Cannot promote this load!");
1291 // Change base type to a different vector type.
1292 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1293
1294 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, Node->getOperand(2));
1295 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1296 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1297 break;
1298 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001299 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001300 // Since loads produce two values, make sure to remember that we
1301 // legalized both of them.
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001302 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1303 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1304 return Op.ResNo ? Tmp4 : Tmp3;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001305 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001306 case ISD::EXTLOAD:
1307 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001308 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001309 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1310 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001311
Chris Lattner5f056bf2005-07-10 01:55:33 +00001312 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001313 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001314 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001315 case TargetLowering::Promote:
1316 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1318 DAG.getValueType(MVT::i8));
1319 Tmp1 = Result.getValue(0);
1320 Tmp2 = Result.getValue(1);
1321 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001322 case TargetLowering::Custom:
1323 isCustom = true;
1324 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001325 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1327 Node->getOperand(3));
1328 Tmp1 = Result.getValue(0);
1329 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001330
1331 if (isCustom) {
1332 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1333 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001334 Tmp1 = LegalizeOp(Tmp3);
1335 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001336 }
1337 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001338 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001339 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001340 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001341 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1342 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001343 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001344 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1345 Tmp2 = LegalizeOp(Load.getValue(1));
1346 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001347 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001348 assert(Node->getOpcode() != ISD::EXTLOAD &&
1349 "EXTLOAD should always be supported!");
1350 // Turn the unsupported load into an EXTLOAD followed by an explicit
1351 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001352 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1353 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001354 SDOperand ValRes;
1355 if (Node->getOpcode() == ISD::SEXTLOAD)
1356 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001357 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001358 else
1359 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001360 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1361 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1362 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001363 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001364 // Since loads produce two values, make sure to remember that we legalized
1365 // both of them.
1366 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1367 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1368 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001369 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001370 case ISD::EXTRACT_ELEMENT: {
1371 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1372 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001373 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001374 case Legal:
1375 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1376 // 1 -> Hi
1377 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1378 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1379 TLI.getShiftAmountTy()));
1380 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1381 } else {
1382 // 0 -> Lo
1383 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1384 Node->getOperand(0));
1385 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001386 break;
1387 case Expand:
1388 // Get both the low and high parts.
1389 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1390 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1391 Result = Tmp2; // 1 -> Hi
1392 else
1393 Result = Tmp1; // 0 -> Lo
1394 break;
1395 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001396 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001397 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001398
1399 case ISD::CopyToReg:
1400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001401
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001402 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001403 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001404 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001405 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001406 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001408 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001409 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001410 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001411 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001412 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1413 Tmp3);
1414 } else {
1415 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001416 }
1417
1418 // Since this produces two values, make sure to remember that we legalized
1419 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001420 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001421 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001422 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001423 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001424 break;
1425
1426 case ISD::RET:
1427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001428
1429 // Ensure that libcalls are emitted before a return.
1430 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1431 Tmp1 = LegalizeOp(Tmp1);
1432 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001433
Chris Lattner3e928bb2005-01-07 07:47:09 +00001434 switch (Node->getNumOperands()) {
1435 case 2: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001436 Tmp2 = Node->getOperand(1);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001437 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001438 case Legal:
Chris Lattnerf87324e2006-04-11 01:31:51 +00001439 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001440 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001441 case Expand:
1442 if (Tmp2.getValueType() != MVT::Vector) {
1443 SDOperand Lo, Hi;
1444 ExpandOp(Tmp2, Lo, Hi);
1445 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1446 } else {
1447 SDNode *InVal = Tmp2.Val;
1448 unsigned NumElems =
1449 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1450 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1451
1452 // Figure out if there is a Packed type corresponding to this Vector
1453 // type. If so, convert to the packed type.
1454 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1455 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1456 // Turn this into a return of the packed type.
1457 Tmp2 = PackVectorOp(Tmp2, TVT);
1458 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1459 } else if (NumElems == 1) {
1460 // Turn this into a return of the scalar type.
1461 Tmp2 = PackVectorOp(Tmp2, EVT);
1462 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001463
1464 // FIXME: Returns of gcc generic vectors smaller than a legal type
1465 // should be returned in integer registers!
1466
Chris Lattnerf87324e2006-04-11 01:31:51 +00001467 // The scalarized value type may not be legal, e.g. it might require
1468 // promotion or expansion. Relegalize the return.
1469 Result = LegalizeOp(Result);
1470 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001471 // FIXME: Returns of gcc generic vectors larger than a legal vector
1472 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001473 SDOperand Lo, Hi;
1474 SplitVectorOp(Tmp2, Lo, Hi);
1475 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1476 Result = LegalizeOp(Result);
1477 }
1478 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001479 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001480 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001481 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1483 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001484 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001485 }
1486 break;
1487 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001488 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001489 break;
1490 default: { // ret <values>
1491 std::vector<SDOperand> NewValues;
1492 NewValues.push_back(Tmp1);
1493 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1494 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1495 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001496 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001497 break;
1498 case Expand: {
1499 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001500 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1501 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001502 ExpandOp(Node->getOperand(i), Lo, Hi);
1503 NewValues.push_back(Lo);
1504 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001505 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001506 }
1507 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001508 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001509 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001510
1511 if (NewValues.size() == Node->getNumOperands())
1512 Result = DAG.UpdateNodeOperands(Result, NewValues);
1513 else
1514 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001515 break;
1516 }
1517 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001518
Chris Lattner6862dbc2006-01-29 21:02:23 +00001519 if (Result.getOpcode() == ISD::RET) {
1520 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1521 default: assert(0 && "This action is not supported yet!");
1522 case TargetLowering::Legal: break;
1523 case TargetLowering::Custom:
1524 Tmp1 = TLI.LowerOperation(Result, DAG);
1525 if (Tmp1.Val) Result = Tmp1;
1526 break;
1527 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001528 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001529 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001530 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001531 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1532 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1533
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001534 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001535 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001536 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001537 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001538 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001539 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001540 } else {
1541 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001542 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001543 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001544 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1545 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001546 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001547 }
1548
Chris Lattner3e928bb2005-01-07 07:47:09 +00001549 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1550 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001551 Tmp3 = LegalizeOp(Node->getOperand(1));
1552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1553 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001554
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001555 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001556 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1557 default: assert(0 && "This action is not supported yet!");
1558 case TargetLowering::Legal: break;
1559 case TargetLowering::Custom:
1560 Tmp1 = TLI.LowerOperation(Result, DAG);
1561 if (Tmp1.Val) Result = Tmp1;
1562 break;
Chris Lattner2efce0a2006-04-16 01:36:45 +00001563 case TargetLowering::Promote:
1564 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1565 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1566 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1567 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1568 Node->getOperand(3));
1569 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001570 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001571 break;
1572 }
1573 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001574 // Truncate the value and store the result.
1575 Tmp3 = PromoteOp(Node->getOperand(1));
1576 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001577 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001578 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001579 break;
1580
Chris Lattner3e928bb2005-01-07 07:47:09 +00001581 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001582 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001583 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001584
1585 // If this is a vector type, then we have to calculate the increment as
1586 // the product of the element size in bytes, and the number of elements
1587 // in the high half of the vector.
1588 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1589 SDNode *InVal = Node->getOperand(1).Val;
1590 unsigned NumElems =
1591 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1592 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1593
1594 // Figure out if there is a Packed type corresponding to this Vector
1595 // type. If so, convert to the packed type.
1596 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1597 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1598 // Turn this into a normal store of the packed type.
1599 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1601 Node->getOperand(3));
Chris Lattner2efce0a2006-04-16 01:36:45 +00001602 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001603 break;
1604 } else if (NumElems == 1) {
1605 // Turn this into a normal store of the scalar type.
1606 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1608 Node->getOperand(3));
Chris Lattner2ae2e982006-03-31 17:37:22 +00001609 // The scalarized value type may not be legal, e.g. it might require
1610 // promotion or expansion. Relegalize the scalar store.
1611 Result = LegalizeOp(Result);
Chris Lattnerc7029802006-03-18 01:44:44 +00001612 break;
1613 } else {
1614 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1615 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1616 }
1617 } else {
1618 ExpandOp(Node->getOperand(1), Lo, Hi);
1619 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001620
Chris Lattnerd9731af2006-03-31 18:20:46 +00001621 if (!TLI.isLittleEndian())
1622 std::swap(Lo, Hi);
1623 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001624
Chris Lattneredb1add2005-05-11 04:51:16 +00001625 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1626 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001627 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1628 getIntPtrConstant(IncrementSize));
1629 assert(isTypeLegal(Tmp2.getValueType()) &&
1630 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001631 // FIXME: This sets the srcvalue of both halves to be the same, which is
1632 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001633 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1634 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001635 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1636 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001637 }
1638 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001639 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001640 case ISD::PCMARKER:
1641 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001643 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001644 case ISD::STACKSAVE:
1645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001646 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1647 Tmp1 = Result.getValue(0);
1648 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001649
Chris Lattner140d53c2006-01-13 02:50:02 +00001650 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1651 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001652 case TargetLowering::Legal: break;
1653 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001654 Tmp3 = TLI.LowerOperation(Result, DAG);
1655 if (Tmp3.Val) {
1656 Tmp1 = LegalizeOp(Tmp3);
1657 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001658 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001659 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001660 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001661 // Expand to CopyFromReg if the target set
1662 // StackPointerRegisterToSaveRestore.
1663 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001664 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001665 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001666 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001667 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001668 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1669 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001670 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001671 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001672 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001673
1674 // Since stacksave produce two values, make sure to remember that we
1675 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001676 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1677 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1678 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001679
Chris Lattner140d53c2006-01-13 02:50:02 +00001680 case ISD::STACKRESTORE:
1681 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1682 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001683 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001684
1685 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1686 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001687 case TargetLowering::Legal: break;
1688 case TargetLowering::Custom:
1689 Tmp1 = TLI.LowerOperation(Result, DAG);
1690 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001691 break;
1692 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001693 // Expand to CopyToReg if the target set
1694 // StackPointerRegisterToSaveRestore.
1695 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1696 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1697 } else {
1698 Result = Tmp1;
1699 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001700 break;
1701 }
1702 break;
1703
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001704 case ISD::READCYCLECOUNTER:
1705 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001706 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001707
1708 // Since rdcc produce two values, make sure to remember that we legalized
1709 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001710 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001711 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001712 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001713
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001714 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001715 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1716 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1717
Chris Lattner456a93a2006-01-28 07:39:30 +00001718 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1719 "Cannot handle illegal TRUNCSTORE yet!");
1720 Tmp2 = LegalizeOp(Node->getOperand(1));
1721
1722 // The only promote case we handle is TRUNCSTORE:i1 X into
1723 // -> TRUNCSTORE:i8 (and X, 1)
1724 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1725 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1726 TargetLowering::Promote) {
1727 // Promote the bool to a mask then store.
1728 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1729 DAG.getConstant(1, Tmp2.getValueType()));
1730 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1731 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001732
Chris Lattner456a93a2006-01-28 07:39:30 +00001733 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1734 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001735 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1736 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001737 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001738
Chris Lattner456a93a2006-01-28 07:39:30 +00001739 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1740 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1741 default: assert(0 && "This action is not supported yet!");
1742 case TargetLowering::Legal: break;
1743 case TargetLowering::Custom:
1744 Tmp1 = TLI.LowerOperation(Result, DAG);
1745 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001746 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001747 }
1748 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001749 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001750 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001751 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1752 case Expand: assert(0 && "It's impossible to expand bools");
1753 case Legal:
1754 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1755 break;
1756 case Promote:
1757 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1758 break;
1759 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001760 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001761 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001762
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001763 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001764
Nate Begemanb942a3d2005-08-23 04:29:48 +00001765 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001766 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001767 case TargetLowering::Legal: break;
1768 case TargetLowering::Custom: {
1769 Tmp1 = TLI.LowerOperation(Result, DAG);
1770 if (Tmp1.Val) Result = Tmp1;
1771 break;
1772 }
Nate Begeman9373a812005-08-10 20:51:12 +00001773 case TargetLowering::Expand:
1774 if (Tmp1.getOpcode() == ISD::SETCC) {
1775 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1776 Tmp2, Tmp3,
1777 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1778 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001779 // Make sure the condition is either zero or one. It may have been
1780 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001781 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1782 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1783 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001784 Result = DAG.getSelectCC(Tmp1,
1785 DAG.getConstant(0, Tmp1.getValueType()),
1786 Tmp2, Tmp3, ISD::SETNE);
1787 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001788 break;
1789 case TargetLowering::Promote: {
1790 MVT::ValueType NVT =
1791 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1792 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001793 if (MVT::isVector(Tmp2.getValueType())) {
1794 ExtOp = ISD::BIT_CONVERT;
1795 TruncOp = ISD::BIT_CONVERT;
1796 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001797 ExtOp = ISD::ANY_EXTEND;
1798 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001799 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001800 ExtOp = ISD::FP_EXTEND;
1801 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001802 }
1803 // Promote each of the values to the new type.
1804 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1805 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1806 // Perform the larger operation, then round down.
1807 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1808 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1809 break;
1810 }
1811 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001812 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001813 case ISD::SELECT_CC: {
1814 Tmp1 = Node->getOperand(0); // LHS
1815 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001816 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1817 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001818 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001819
Nate Begeman750ac1b2006-02-01 07:19:44 +00001820 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1821
1822 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1823 // the LHS is a legal SETCC itself. In this case, we need to compare
1824 // the result against zero to select between true and false values.
1825 if (Tmp2.Val == 0) {
1826 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1827 CC = DAG.getCondCode(ISD::SETNE);
1828 }
1829 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1830
1831 // Everything is legal, see if we should expand this op or something.
1832 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1833 default: assert(0 && "This action is not supported yet!");
1834 case TargetLowering::Legal: break;
1835 case TargetLowering::Custom:
1836 Tmp1 = TLI.LowerOperation(Result, DAG);
1837 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001838 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001839 }
1840 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001841 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001842 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001843 Tmp1 = Node->getOperand(0);
1844 Tmp2 = Node->getOperand(1);
1845 Tmp3 = Node->getOperand(2);
1846 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1847
1848 // If we had to Expand the SetCC operands into a SELECT node, then it may
1849 // not always be possible to return a true LHS & RHS. In this case, just
1850 // return the value we legalized, returned in the LHS
1851 if (Tmp2.Val == 0) {
1852 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001853 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001854 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001855
Chris Lattner73e142f2006-01-30 22:43:50 +00001856 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001857 default: assert(0 && "Cannot handle this action for SETCC yet!");
1858 case TargetLowering::Custom:
1859 isCustom = true;
1860 // FALLTHROUGH.
1861 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001862 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001863 if (isCustom) {
1864 Tmp3 = TLI.LowerOperation(Result, DAG);
1865 if (Tmp3.Val) Result = Tmp3;
1866 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001867 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001868 case TargetLowering::Promote: {
1869 // First step, figure out the appropriate operation to use.
1870 // Allow SETCC to not be supported for all legal data types
1871 // Mostly this targets FP
1872 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1873 MVT::ValueType OldVT = NewInTy;
1874
1875 // Scan for the appropriate larger type to use.
1876 while (1) {
1877 NewInTy = (MVT::ValueType)(NewInTy+1);
1878
1879 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1880 "Fell off of the edge of the integer world");
1881 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1882 "Fell off of the edge of the floating point world");
1883
1884 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001885 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001886 break;
1887 }
1888 if (MVT::isInteger(NewInTy))
1889 assert(0 && "Cannot promote Legal Integer SETCC yet");
1890 else {
1891 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1892 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1893 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001894 Tmp1 = LegalizeOp(Tmp1);
1895 Tmp2 = LegalizeOp(Tmp2);
1896 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001897 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001898 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001899 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001900 case TargetLowering::Expand:
1901 // Expand a setcc node into a select_cc of the same condition, lhs, and
1902 // rhs that selects between const 1 (true) and const 0 (false).
1903 MVT::ValueType VT = Node->getValueType(0);
1904 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1905 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1906 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001907 break;
1908 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001909 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001910 case ISD::MEMSET:
1911 case ISD::MEMCPY:
1912 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001914 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1915
1916 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1917 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1918 case Expand: assert(0 && "Cannot expand a byte!");
1919 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001920 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001921 break;
1922 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001923 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001924 break;
1925 }
1926 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001927 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001928 }
Chris Lattner272455b2005-02-02 03:44:41 +00001929
1930 SDOperand Tmp4;
1931 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001932 case Expand: {
1933 // Length is too big, just take the lo-part of the length.
1934 SDOperand HiPart;
1935 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1936 break;
1937 }
Chris Lattnere5605212005-01-28 22:29:18 +00001938 case Legal:
1939 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001940 break;
1941 case Promote:
1942 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001943 break;
1944 }
1945
1946 SDOperand Tmp5;
1947 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1948 case Expand: assert(0 && "Cannot expand this yet!");
1949 case Legal:
1950 Tmp5 = LegalizeOp(Node->getOperand(4));
1951 break;
1952 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001953 Tmp5 = PromoteOp(Node->getOperand(4));
1954 break;
1955 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001956
1957 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1958 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001959 case TargetLowering::Custom:
1960 isCustom = true;
1961 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001962 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001964 if (isCustom) {
1965 Tmp1 = TLI.LowerOperation(Result, DAG);
1966 if (Tmp1.Val) Result = Tmp1;
1967 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001968 break;
1969 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001970 // Otherwise, the target does not support this operation. Lower the
1971 // operation to an explicit libcall as appropriate.
1972 MVT::ValueType IntPtr = TLI.getPointerTy();
1973 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1974 std::vector<std::pair<SDOperand, const Type*> > Args;
1975
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001976 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001977 if (Node->getOpcode() == ISD::MEMSET) {
1978 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001979 // Extend the (previously legalized) ubyte argument to be an int value
1980 // for the call.
1981 if (Tmp3.getValueType() > MVT::i32)
1982 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1983 else
1984 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001985 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1986 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1987
1988 FnName = "memset";
1989 } else if (Node->getOpcode() == ISD::MEMCPY ||
1990 Node->getOpcode() == ISD::MEMMOVE) {
1991 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1992 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1993 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1994 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1995 } else {
1996 assert(0 && "Unknown op!");
1997 }
Chris Lattner45982da2005-05-12 16:53:42 +00001998
Chris Lattnere1bd8222005-01-11 05:57:22 +00001999 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002000 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002001 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002002 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002003 break;
2004 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002005 }
2006 break;
2007 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002008
Chris Lattner5b359c62005-04-02 04:00:59 +00002009 case ISD::SHL_PARTS:
2010 case ISD::SRA_PARTS:
2011 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002012 std::vector<SDOperand> Ops;
2013 bool Changed = false;
2014 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2015 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2016 Changed |= Ops.back() != Node->getOperand(i);
2017 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002018 if (Changed)
2019 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00002020
Evan Cheng05a2d562006-01-09 18:31:59 +00002021 switch (TLI.getOperationAction(Node->getOpcode(),
2022 Node->getValueType(0))) {
2023 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002024 case TargetLowering::Legal: break;
2025 case TargetLowering::Custom:
2026 Tmp1 = TLI.LowerOperation(Result, DAG);
2027 if (Tmp1.Val) {
2028 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002029 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002030 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002031 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2032 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002033 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002034 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002035 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002036 return RetVal;
2037 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002038 break;
2039 }
2040
Chris Lattner2c8086f2005-04-02 05:00:07 +00002041 // Since these produce multiple values, make sure to remember that we
2042 // legalized all of them.
2043 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2044 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2045 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002046 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002047
2048 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002049 case ISD::ADD:
2050 case ISD::SUB:
2051 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002052 case ISD::MULHS:
2053 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002054 case ISD::UDIV:
2055 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002056 case ISD::AND:
2057 case ISD::OR:
2058 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002059 case ISD::SHL:
2060 case ISD::SRL:
2061 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002062 case ISD::FADD:
2063 case ISD::FSUB:
2064 case ISD::FMUL:
2065 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002066 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002067 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2068 case Expand: assert(0 && "Not possible");
2069 case Legal:
2070 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2071 break;
2072 case Promote:
2073 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2074 break;
2075 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002076
2077 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002078
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002079 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002080 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002081 case TargetLowering::Legal: break;
2082 case TargetLowering::Custom:
2083 Tmp1 = TLI.LowerOperation(Result, DAG);
2084 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002085 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002086 case TargetLowering::Expand: {
2087 assert(MVT::isVector(Node->getValueType(0)) &&
2088 "Cannot expand this binary operator!");
2089 // Expand the operation into a bunch of nasty scalar code.
2090 std::vector<SDOperand> Ops;
2091 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2092 MVT::ValueType PtrVT = TLI.getPointerTy();
2093 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2094 i != e; ++i) {
2095 SDOperand Idx = DAG.getConstant(i, PtrVT);
2096 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2097 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2098 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2099 }
2100 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
2101 break;
2102 }
Evan Chengcc987612006-04-12 21:20:24 +00002103 case TargetLowering::Promote: {
2104 switch (Node->getOpcode()) {
2105 default: assert(0 && "Do not know how to promote this BinOp!");
2106 case ISD::AND:
2107 case ISD::OR:
2108 case ISD::XOR: {
2109 MVT::ValueType OVT = Node->getValueType(0);
2110 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2111 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2112 // Bit convert each of the values to the new type.
2113 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2114 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2115 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2116 // Bit convert the result back the original type.
2117 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2118 break;
2119 }
2120 }
2121 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002122 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002123 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002124
2125 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2126 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2127 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2128 case Expand: assert(0 && "Not possible");
2129 case Legal:
2130 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2131 break;
2132 case Promote:
2133 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2134 break;
2135 }
2136
2137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2138
2139 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2140 default: assert(0 && "Operation not supported");
2141 case TargetLowering::Custom:
2142 Tmp1 = TLI.LowerOperation(Result, DAG);
2143 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002144 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002145 case TargetLowering::Legal: break;
2146 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00002147 // If this target supports fabs/fneg natively, do this efficiently.
2148 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2149 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2150 // Get the sign bit of the RHS.
2151 MVT::ValueType IVT =
2152 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2153 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2154 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2155 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2156 // Get the absolute value of the result.
2157 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2158 // Select between the nabs and abs value based on the sign bit of
2159 // the input.
2160 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2161 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2162 AbsVal),
2163 AbsVal);
2164 Result = LegalizeOp(Result);
2165 break;
2166 }
2167
2168 // Otherwise, do bitwise ops!
2169
2170 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00002171 const char *FnName;
2172 if (Node->getValueType(0) == MVT::f32) {
2173 FnName = "copysignf";
2174 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2175 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2176 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2177 } else {
2178 FnName = "copysign";
2179 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2180 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2181 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2182 }
2183 SDOperand Dummy;
2184 Result = ExpandLibCall(FnName, Node, Dummy);
2185 break;
2186 }
2187 break;
2188
Nate Begeman551bf3f2006-02-17 05:43:56 +00002189 case ISD::ADDC:
2190 case ISD::SUBC:
2191 Tmp1 = LegalizeOp(Node->getOperand(0));
2192 Tmp2 = LegalizeOp(Node->getOperand(1));
2193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2194 // Since this produces two values, make sure to remember that we legalized
2195 // both of them.
2196 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2197 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2198 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002199
Nate Begeman551bf3f2006-02-17 05:43:56 +00002200 case ISD::ADDE:
2201 case ISD::SUBE:
2202 Tmp1 = LegalizeOp(Node->getOperand(0));
2203 Tmp2 = LegalizeOp(Node->getOperand(1));
2204 Tmp3 = LegalizeOp(Node->getOperand(2));
2205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2206 // Since this produces two values, make sure to remember that we legalized
2207 // both of them.
2208 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2209 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2210 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002211
Nate Begeman419f8b62005-10-18 00:27:41 +00002212 case ISD::BUILD_PAIR: {
2213 MVT::ValueType PairTy = Node->getValueType(0);
2214 // TODO: handle the case where the Lo and Hi operands are not of legal type
2215 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2216 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2217 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002218 case TargetLowering::Promote:
2219 case TargetLowering::Custom:
2220 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002221 case TargetLowering::Legal:
2222 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2223 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2224 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002225 case TargetLowering::Expand:
2226 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2227 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2228 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2229 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2230 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002231 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002232 break;
2233 }
2234 break;
2235 }
2236
Nate Begemanc105e192005-04-06 00:23:54 +00002237 case ISD::UREM:
2238 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002239 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002240 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2241 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002242
Nate Begemanc105e192005-04-06 00:23:54 +00002243 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002244 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2245 case TargetLowering::Custom:
2246 isCustom = true;
2247 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002248 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002250 if (isCustom) {
2251 Tmp1 = TLI.LowerOperation(Result, DAG);
2252 if (Tmp1.Val) Result = Tmp1;
2253 }
Nate Begemanc105e192005-04-06 00:23:54 +00002254 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002255 case TargetLowering::Expand:
2256 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002257 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002258 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002259 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002260 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2261 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2262 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2263 } else {
2264 // Floating point mod -> fmod libcall.
2265 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2266 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002267 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002268 }
2269 break;
2270 }
2271 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002272 case ISD::VAARG: {
2273 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2274 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2275
Chris Lattner5c62f332006-01-28 07:42:08 +00002276 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002277 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2278 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002279 case TargetLowering::Custom:
2280 isCustom = true;
2281 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002282 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2284 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002285 Tmp1 = Result.getValue(1);
2286
2287 if (isCustom) {
2288 Tmp2 = TLI.LowerOperation(Result, DAG);
2289 if (Tmp2.Val) {
2290 Result = LegalizeOp(Tmp2);
2291 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2292 }
2293 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002294 break;
2295 case TargetLowering::Expand: {
2296 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2297 Node->getOperand(2));
2298 // Increment the pointer, VAList, to the next vaarg
2299 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2300 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2301 TLI.getPointerTy()));
2302 // Store the incremented VAList to the legalized pointer
2303 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2304 Node->getOperand(2));
2305 // Load the actual argument out of the pointer VAList
2306 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002307 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002308 Result = LegalizeOp(Result);
2309 break;
2310 }
2311 }
2312 // Since VAARG produces two values, make sure to remember that we
2313 // legalized both of them.
2314 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002315 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2316 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002317 }
2318
2319 case ISD::VACOPY:
2320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2321 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2322 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2323
2324 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2325 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002326 case TargetLowering::Custom:
2327 isCustom = true;
2328 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002329 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2331 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002332 if (isCustom) {
2333 Tmp1 = TLI.LowerOperation(Result, DAG);
2334 if (Tmp1.Val) Result = Tmp1;
2335 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002336 break;
2337 case TargetLowering::Expand:
2338 // This defaults to loading a pointer from the input and storing it to the
2339 // output, returning the chain.
2340 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2341 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2342 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002343 break;
2344 }
2345 break;
2346
2347 case ISD::VAEND:
2348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2349 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2350
2351 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2352 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002353 case TargetLowering::Custom:
2354 isCustom = true;
2355 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002356 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002357 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002358 if (isCustom) {
2359 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2360 if (Tmp1.Val) Result = Tmp1;
2361 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002362 break;
2363 case TargetLowering::Expand:
2364 Result = Tmp1; // Default to a no-op, return the chain
2365 break;
2366 }
2367 break;
2368
2369 case ISD::VASTART:
2370 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2371 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2372
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002373 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2374
Nate Begemanacc398c2006-01-25 18:21:52 +00002375 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2376 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002377 case TargetLowering::Legal: break;
2378 case TargetLowering::Custom:
2379 Tmp1 = TLI.LowerOperation(Result, DAG);
2380 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002381 break;
2382 }
2383 break;
2384
Nate Begeman35ef9132006-01-11 21:21:00 +00002385 case ISD::ROTL:
2386 case ISD::ROTR:
2387 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2388 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002389
2390 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2391 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002393 break;
2394
Nate Begemand88fc032006-01-14 03:14:10 +00002395 case ISD::BSWAP:
2396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2397 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002398 case TargetLowering::Custom:
2399 assert(0 && "Cannot custom legalize this yet!");
2400 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002401 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002402 break;
2403 case TargetLowering::Promote: {
2404 MVT::ValueType OVT = Tmp1.getValueType();
2405 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2406 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002407
Chris Lattner456a93a2006-01-28 07:39:30 +00002408 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2409 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2410 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2411 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2412 break;
2413 }
2414 case TargetLowering::Expand:
2415 Result = ExpandBSWAP(Tmp1);
2416 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002417 }
2418 break;
2419
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002420 case ISD::CTPOP:
2421 case ISD::CTTZ:
2422 case ISD::CTLZ:
2423 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2424 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002425 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002426 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002427 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002428 break;
2429 case TargetLowering::Promote: {
2430 MVT::ValueType OVT = Tmp1.getValueType();
2431 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002432
2433 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002434 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2435 // Perform the larger operation, then subtract if needed.
2436 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002437 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002438 case ISD::CTPOP:
2439 Result = Tmp1;
2440 break;
2441 case ISD::CTTZ:
2442 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002443 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2444 DAG.getConstant(getSizeInBits(NVT), NVT),
2445 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002446 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002447 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2448 break;
2449 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002450 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002451 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2452 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002453 getSizeInBits(OVT), NVT));
2454 break;
2455 }
2456 break;
2457 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002458 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002459 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002460 break;
2461 }
2462 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002463
Chris Lattner2c8086f2005-04-02 05:00:07 +00002464 // Unary operators
2465 case ISD::FABS:
2466 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002467 case ISD::FSQRT:
2468 case ISD::FSIN:
2469 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002470 Tmp1 = LegalizeOp(Node->getOperand(0));
2471 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002472 case TargetLowering::Promote:
2473 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002474 isCustom = true;
2475 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002476 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002477 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002478 if (isCustom) {
2479 Tmp1 = TLI.LowerOperation(Result, DAG);
2480 if (Tmp1.Val) Result = Tmp1;
2481 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002482 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002483 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002484 switch (Node->getOpcode()) {
2485 default: assert(0 && "Unreachable!");
2486 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002487 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2488 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002489 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002490 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002491 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002492 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2493 MVT::ValueType VT = Node->getValueType(0);
2494 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002495 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002496 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2497 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002498 break;
2499 }
2500 case ISD::FSQRT:
2501 case ISD::FSIN:
2502 case ISD::FCOS: {
2503 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002504 const char *FnName = 0;
2505 switch(Node->getOpcode()) {
2506 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2507 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2508 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2509 default: assert(0 && "Unreachable!");
2510 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002511 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002512 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002513 break;
2514 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002515 }
2516 break;
2517 }
2518 break;
Chris Lattner35481892005-12-23 00:16:34 +00002519
2520 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002521 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002522 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002523 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002524 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2525 Node->getOperand(0).getValueType())) {
2526 default: assert(0 && "Unknown operation action!");
2527 case TargetLowering::Expand:
2528 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2529 break;
2530 case TargetLowering::Legal:
2531 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002532 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002533 break;
2534 }
2535 }
2536 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002537 case ISD::VBIT_CONVERT: {
2538 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2539 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2540
2541 // The input has to be a vector type, we have to either scalarize it, pack
2542 // it, or convert it based on whether the input vector type is legal.
2543 SDNode *InVal = Node->getOperand(0).Val;
2544 unsigned NumElems =
2545 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2546 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2547
2548 // Figure out if there is a Packed type corresponding to this Vector
2549 // type. If so, convert to the packed type.
2550 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2551 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2552 // Turn this into a bit convert of the packed input.
2553 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2554 PackVectorOp(Node->getOperand(0), TVT));
2555 break;
2556 } else if (NumElems == 1) {
2557 // Turn this into a bit convert of the scalar input.
2558 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2559 PackVectorOp(Node->getOperand(0), EVT));
2560 break;
2561 } else {
2562 // FIXME: UNIMP! Store then reload
2563 assert(0 && "Cast from unsupported vector type not implemented yet!");
2564 }
2565 }
2566
Chris Lattner2c8086f2005-04-02 05:00:07 +00002567 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002568 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002569 case ISD::UINT_TO_FP: {
2570 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002571 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2572 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002573 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002574 Node->getOperand(0).getValueType())) {
2575 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002576 case TargetLowering::Custom:
2577 isCustom = true;
2578 // FALLTHROUGH
2579 case TargetLowering::Legal:
2580 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002581 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002582 if (isCustom) {
2583 Tmp1 = TLI.LowerOperation(Result, DAG);
2584 if (Tmp1.Val) Result = Tmp1;
2585 }
2586 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002587 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002588 Result = ExpandLegalINT_TO_FP(isSigned,
2589 LegalizeOp(Node->getOperand(0)),
2590 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002591 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002592 case TargetLowering::Promote:
2593 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2594 Node->getValueType(0),
2595 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002596 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002597 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002598 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002599 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002600 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2601 Node->getValueType(0), Node->getOperand(0));
2602 break;
2603 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002604 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002605 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002606 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2607 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002608 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002609 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2610 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002611 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002612 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2613 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002614 break;
2615 }
2616 break;
2617 }
2618 case ISD::TRUNCATE:
2619 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2620 case Legal:
2621 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002622 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002623 break;
2624 case Expand:
2625 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2626
2627 // Since the result is legal, we should just be able to truncate the low
2628 // part of the source.
2629 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2630 break;
2631 case Promote:
2632 Result = PromoteOp(Node->getOperand(0));
2633 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2634 break;
2635 }
2636 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002637
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002638 case ISD::FP_TO_SINT:
2639 case ISD::FP_TO_UINT:
2640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2641 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002642 Tmp1 = LegalizeOp(Node->getOperand(0));
2643
Chris Lattner1618beb2005-07-29 00:11:56 +00002644 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2645 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002646 case TargetLowering::Custom:
2647 isCustom = true;
2648 // FALLTHROUGH
2649 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002650 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002651 if (isCustom) {
2652 Tmp1 = TLI.LowerOperation(Result, DAG);
2653 if (Tmp1.Val) Result = Tmp1;
2654 }
2655 break;
2656 case TargetLowering::Promote:
2657 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2658 Node->getOpcode() == ISD::FP_TO_SINT);
2659 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002660 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002661 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2662 SDOperand True, False;
2663 MVT::ValueType VT = Node->getOperand(0).getValueType();
2664 MVT::ValueType NVT = Node->getValueType(0);
2665 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2666 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2667 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2668 Node->getOperand(0), Tmp2, ISD::SETLT);
2669 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2670 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002671 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002672 Tmp2));
2673 False = DAG.getNode(ISD::XOR, NVT, False,
2674 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002675 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2676 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002677 } else {
2678 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2679 }
2680 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002681 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002682 break;
2683 case Expand:
2684 assert(0 && "Shouldn't need to expand other operators here!");
2685 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002686 Tmp1 = PromoteOp(Node->getOperand(0));
2687 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2688 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002689 break;
2690 }
2691 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002692
Chris Lattner13c78e22005-09-02 00:18:10 +00002693 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002694 case ISD::ZERO_EXTEND:
2695 case ISD::SIGN_EXTEND:
2696 case ISD::FP_EXTEND:
2697 case ISD::FP_ROUND:
2698 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002699 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002700 case Legal:
2701 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002702 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002703 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002704 case Promote:
2705 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002706 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002707 Tmp1 = PromoteOp(Node->getOperand(0));
2708 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002709 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002710 case ISD::ZERO_EXTEND:
2711 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002712 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002713 Result = DAG.getZeroExtendInReg(Result,
2714 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002715 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002716 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002717 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002718 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002719 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002720 Result,
2721 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002722 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002723 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002724 Result = PromoteOp(Node->getOperand(0));
2725 if (Result.getValueType() != Op.getValueType())
2726 // Dynamically dead while we have only 2 FP types.
2727 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2728 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002729 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002730 Result = PromoteOp(Node->getOperand(0));
2731 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2732 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002733 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002734 }
2735 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002736 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002737 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002738 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002739 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002740
2741 // If this operation is not supported, convert it to a shl/shr or load/store
2742 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002743 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2744 default: assert(0 && "This action not supported for this op yet!");
2745 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002746 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002747 break;
2748 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002749 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002750 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002751 // NOTE: we could fall back on load/store here too for targets without
2752 // SAR. However, it is doubtful that any exist.
2753 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2754 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002755 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002756 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2757 Node->getOperand(0), ShiftCst);
2758 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2759 Result, ShiftCst);
2760 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2761 // The only way we can lower this is to turn it into a STORETRUNC,
2762 // EXTLOAD pair, targetting a temporary location (a stack slot).
2763
2764 // NOTE: there is a choice here between constantly creating new stack
2765 // slots and always reusing the same one. We currently always create
2766 // new ones, as reuse may inhibit scheduling.
2767 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2768 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2769 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2770 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002771 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002772 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2773 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2774 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002775 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002776 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002777 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2778 Result, StackSlot, DAG.getSrcValue(NULL),
2779 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002780 } else {
2781 assert(0 && "Unknown op");
2782 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002783 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002784 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002785 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002786 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002787 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002788
Chris Lattner4ddd2832006-04-08 04:13:17 +00002789 assert(Result.getValueType() == Op.getValueType() &&
2790 "Bad legalization!");
2791
Chris Lattner456a93a2006-01-28 07:39:30 +00002792 // Make sure that the generated code is itself legal.
2793 if (Result != Op)
2794 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002795
Chris Lattner45982da2005-05-12 16:53:42 +00002796 // Note that LegalizeOp may be reentered even from single-use nodes, which
2797 // means that we always must cache transformed nodes.
2798 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002799 return Result;
2800}
2801
Chris Lattner8b6fa222005-01-15 22:16:26 +00002802/// PromoteOp - Given an operation that produces a value in an invalid type,
2803/// promote it to compute the value into a larger type. The produced value will
2804/// have the correct bits for the low portion of the register, but no guarantee
2805/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002806SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2807 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002808 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002809 assert(getTypeAction(VT) == Promote &&
2810 "Caller should expand or legalize operands that are not promotable!");
2811 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2812 "Cannot promote to smaller type!");
2813
Chris Lattner03c85462005-01-15 05:21:40 +00002814 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002815 SDOperand Result;
2816 SDNode *Node = Op.Val;
2817
Chris Lattner6fdcb252005-09-02 20:32:45 +00002818 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2819 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002820
Chris Lattner03c85462005-01-15 05:21:40 +00002821 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002822 case ISD::CopyFromReg:
2823 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002824 default:
2825 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2826 assert(0 && "Do not know how to promote this operator!");
2827 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002828 case ISD::UNDEF:
2829 Result = DAG.getNode(ISD::UNDEF, NVT);
2830 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002831 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002832 if (VT != MVT::i1)
2833 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2834 else
2835 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002836 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2837 break;
2838 case ISD::ConstantFP:
2839 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2840 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2841 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002842
Chris Lattner82fbfb62005-01-18 02:59:52 +00002843 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002844 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002845 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2846 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002847 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00002848
Chris Lattner03c85462005-01-15 05:21:40 +00002849 case ISD::TRUNCATE:
2850 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2851 case Legal:
2852 Result = LegalizeOp(Node->getOperand(0));
2853 assert(Result.getValueType() >= NVT &&
2854 "This truncation doesn't make sense!");
2855 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2856 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2857 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002858 case Promote:
2859 // The truncation is not required, because we don't guarantee anything
2860 // about high bits anyway.
2861 Result = PromoteOp(Node->getOperand(0));
2862 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002863 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002864 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2865 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002866 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002867 }
2868 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002869 case ISD::SIGN_EXTEND:
2870 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002871 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002872 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2873 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2874 case Legal:
2875 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002876 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002877 break;
2878 case Promote:
2879 // Promote the reg if it's smaller.
2880 Result = PromoteOp(Node->getOperand(0));
2881 // The high bits are not guaranteed to be anything. Insert an extend.
2882 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002883 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002884 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002885 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002886 Result = DAG.getZeroExtendInReg(Result,
2887 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002888 break;
2889 }
2890 break;
Chris Lattner35481892005-12-23 00:16:34 +00002891 case ISD::BIT_CONVERT:
2892 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2893 Result = PromoteOp(Result);
2894 break;
2895
Chris Lattner8b6fa222005-01-15 22:16:26 +00002896 case ISD::FP_EXTEND:
2897 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2898 case ISD::FP_ROUND:
2899 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2900 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2901 case Promote: assert(0 && "Unreachable with 2 FP types!");
2902 case Legal:
2903 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002904 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002905 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002906 break;
2907 }
2908 break;
2909
2910 case ISD::SINT_TO_FP:
2911 case ISD::UINT_TO_FP:
2912 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2913 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002914 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002915 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002916 break;
2917
2918 case Promote:
2919 Result = PromoteOp(Node->getOperand(0));
2920 if (Node->getOpcode() == ISD::SINT_TO_FP)
2921 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002922 Result,
2923 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002924 else
Chris Lattner23993562005-04-13 02:38:47 +00002925 Result = DAG.getZeroExtendInReg(Result,
2926 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002927 // No extra round required here.
2928 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002929 break;
2930 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002931 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2932 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002933 // Round if we cannot tolerate excess precision.
2934 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002935 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2936 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002937 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002938 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002939 break;
2940
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002941 case ISD::SIGN_EXTEND_INREG:
2942 Result = PromoteOp(Node->getOperand(0));
2943 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2944 Node->getOperand(1));
2945 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002946 case ISD::FP_TO_SINT:
2947 case ISD::FP_TO_UINT:
2948 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2949 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002950 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002951 break;
2952 case Promote:
2953 // The input result is prerounded, so we don't have to do anything
2954 // special.
2955 Tmp1 = PromoteOp(Node->getOperand(0));
2956 break;
2957 case Expand:
2958 assert(0 && "not implemented");
2959 }
Nate Begemand2558e32005-08-14 01:20:53 +00002960 // If we're promoting a UINT to a larger size, check to see if the new node
2961 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2962 // we can use that instead. This allows us to generate better code for
2963 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2964 // legal, such as PowerPC.
2965 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002966 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002967 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2968 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002969 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2970 } else {
2971 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2972 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002973 break;
2974
Chris Lattner2c8086f2005-04-02 05:00:07 +00002975 case ISD::FABS:
2976 case ISD::FNEG:
2977 Tmp1 = PromoteOp(Node->getOperand(0));
2978 assert(Tmp1.getValueType() == NVT);
2979 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2980 // NOTE: we do not have to do any extra rounding here for
2981 // NoExcessFPPrecision, because we know the input will have the appropriate
2982 // precision, and these operations don't modify precision at all.
2983 break;
2984
Chris Lattnerda6ba872005-04-28 21:44:33 +00002985 case ISD::FSQRT:
2986 case ISD::FSIN:
2987 case ISD::FCOS:
2988 Tmp1 = PromoteOp(Node->getOperand(0));
2989 assert(Tmp1.getValueType() == NVT);
2990 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002991 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002992 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2993 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002994 break;
2995
Chris Lattner03c85462005-01-15 05:21:40 +00002996 case ISD::AND:
2997 case ISD::OR:
2998 case ISD::XOR:
Evan Chengcc987612006-04-12 21:20:24 +00002999 // The input may have strange things in the top bits of the registers, but
3000 // these operations don't care. They may have weird bits going out, but
3001 // that too is okay if they are integer operations.
3002 Tmp1 = PromoteOp(Node->getOperand(0));
3003 Tmp2 = PromoteOp(Node->getOperand(1));
3004 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3005 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3006 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003007 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003008 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003009 case ISD::MUL:
3010 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003011 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003012 // that too is okay if they are integer operations.
3013 Tmp1 = PromoteOp(Node->getOperand(0));
3014 Tmp2 = PromoteOp(Node->getOperand(1));
3015 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3016 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003017 break;
3018 case ISD::FADD:
3019 case ISD::FSUB:
3020 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003021 Tmp1 = PromoteOp(Node->getOperand(0));
3022 Tmp2 = PromoteOp(Node->getOperand(1));
3023 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3024 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3025
3026 // Floating point operations will give excess precision that we may not be
3027 // able to tolerate. If we DO allow excess precision, just leave it,
3028 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003029 // FIXME: Why would we need to round FP ops more than integer ones?
3030 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003031 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003032 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3033 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003034 break;
3035
Chris Lattner8b6fa222005-01-15 22:16:26 +00003036 case ISD::SDIV:
3037 case ISD::SREM:
3038 // These operators require that their input be sign extended.
3039 Tmp1 = PromoteOp(Node->getOperand(0));
3040 Tmp2 = PromoteOp(Node->getOperand(1));
3041 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003042 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3043 DAG.getValueType(VT));
3044 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3045 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003046 }
3047 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3048
3049 // Perform FP_ROUND: this is probably overly pessimistic.
3050 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003051 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3052 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003053 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003054 case ISD::FDIV:
3055 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003056 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003057 // These operators require that their input be fp extended.
3058 Tmp1 = PromoteOp(Node->getOperand(0));
3059 Tmp2 = PromoteOp(Node->getOperand(1));
3060 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3061
3062 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003063 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003064 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3065 DAG.getValueType(VT));
3066 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003067
3068 case ISD::UDIV:
3069 case ISD::UREM:
3070 // These operators require that their input be zero extended.
3071 Tmp1 = PromoteOp(Node->getOperand(0));
3072 Tmp2 = PromoteOp(Node->getOperand(1));
3073 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003074 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3075 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003076 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3077 break;
3078
3079 case ISD::SHL:
3080 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003081 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003082 break;
3083 case ISD::SRA:
3084 // The input value must be properly sign extended.
3085 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003086 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3087 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003088 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003089 break;
3090 case ISD::SRL:
3091 // The input value must be properly zero extended.
3092 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003093 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003094 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003095 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003096
3097 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003098 Tmp1 = Node->getOperand(0); // Get the chain.
3099 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003100 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3101 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3102 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3103 } else {
3104 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3105 Node->getOperand(2));
3106 // Increment the pointer, VAList, to the next vaarg
3107 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3108 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3109 TLI.getPointerTy()));
3110 // Store the incremented VAList to the legalized pointer
3111 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3112 Node->getOperand(2));
3113 // Load the actual argument out of the pointer VAList
3114 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3115 DAG.getSrcValue(0), VT);
3116 }
3117 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003118 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003119 break;
3120
Chris Lattner03c85462005-01-15 05:21:40 +00003121 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003122 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3123 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003124 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003125 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003126 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003127 case ISD::SEXTLOAD:
3128 case ISD::ZEXTLOAD:
3129 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00003130 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3131 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00003132 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003133 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003134 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003135 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003136 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003137 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3138 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003139 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003140 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003141 case ISD::SELECT_CC:
3142 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3143 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3144 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003145 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003146 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003147 case ISD::BSWAP:
3148 Tmp1 = Node->getOperand(0);
3149 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3150 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3151 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3152 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3153 TLI.getShiftAmountTy()));
3154 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003155 case ISD::CTPOP:
3156 case ISD::CTTZ:
3157 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003158 // Zero extend the argument
3159 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003160 // Perform the larger operation, then subtract if needed.
3161 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003162 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003163 case ISD::CTPOP:
3164 Result = Tmp1;
3165 break;
3166 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003167 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003168 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003169 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003170 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003171 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003172 break;
3173 case ISD::CTLZ:
3174 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003175 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3176 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003177 getSizeInBits(VT), NVT));
3178 break;
3179 }
3180 break;
Chris Lattner15972212006-03-31 17:55:51 +00003181 case ISD::VEXTRACT_VECTOR_ELT:
3182 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3183 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003184 case ISD::EXTRACT_VECTOR_ELT:
3185 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3186 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003187 }
3188
3189 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003190
3191 // Make sure the result is itself legal.
3192 Result = LegalizeOp(Result);
3193
3194 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003195 AddPromotedOperand(Op, Result);
3196 return Result;
3197}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003198
Chris Lattner15972212006-03-31 17:55:51 +00003199/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3200/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3201/// on the vector type. The return type of this matches the element type of the
3202/// vector, which may not be legal for the target.
3203SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3204 // We know that operand #0 is the Vec vector. If the index is a constant
3205 // or if the invec is a supported hardware type, we can use it. Otherwise,
3206 // lower to a store then an indexed load.
3207 SDOperand Vec = Op.getOperand(0);
3208 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3209
3210 SDNode *InVal = Vec.Val;
3211 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3212 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3213
3214 // Figure out if there is a Packed type corresponding to this Vector
3215 // type. If so, convert to the packed type.
3216 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3217 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3218 // Turn this into a packed extract_vector_elt operation.
3219 Vec = PackVectorOp(Vec, TVT);
3220 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3221 } else if (NumElems == 1) {
3222 // This must be an access of the only element. Return it.
3223 return PackVectorOp(Vec, EVT);
3224 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3225 SDOperand Lo, Hi;
3226 SplitVectorOp(Vec, Lo, Hi);
3227 if (CIdx->getValue() < NumElems/2) {
3228 Vec = Lo;
3229 } else {
3230 Vec = Hi;
3231 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3232 }
3233
3234 // It's now an extract from the appropriate high or low part. Recurse.
3235 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3236 return LowerVEXTRACT_VECTOR_ELT(Op);
3237 } else {
3238 // Variable index case for extract element.
3239 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3240 assert(0 && "unimp!");
3241 return SDOperand();
3242 }
3243}
3244
Chris Lattner4aab2f42006-04-02 05:06:04 +00003245/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3246/// memory traffic.
3247SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3248 SDOperand Vector = Op.getOperand(0);
3249 SDOperand Idx = Op.getOperand(1);
3250
3251 // If the target doesn't support this, store the value to a temporary
3252 // stack slot, then LOAD the scalar element back out.
3253 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3254 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3255 Vector, StackPtr, DAG.getSrcValue(NULL));
3256
3257 // Add the offset to the index.
3258 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3259 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3260 DAG.getConstant(EltSize, Idx.getValueType()));
3261 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3262
3263 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3264}
3265
Chris Lattner15972212006-03-31 17:55:51 +00003266
Nate Begeman750ac1b2006-02-01 07:19:44 +00003267/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3268/// with condition CC on the current target. This usually involves legalizing
3269/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3270/// there may be no choice but to create a new SetCC node to represent the
3271/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3272/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3273void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3274 SDOperand &RHS,
3275 SDOperand &CC) {
3276 SDOperand Tmp1, Tmp2, Result;
3277
3278 switch (getTypeAction(LHS.getValueType())) {
3279 case Legal:
3280 Tmp1 = LegalizeOp(LHS); // LHS
3281 Tmp2 = LegalizeOp(RHS); // RHS
3282 break;
3283 case Promote:
3284 Tmp1 = PromoteOp(LHS); // LHS
3285 Tmp2 = PromoteOp(RHS); // RHS
3286
3287 // If this is an FP compare, the operands have already been extended.
3288 if (MVT::isInteger(LHS.getValueType())) {
3289 MVT::ValueType VT = LHS.getValueType();
3290 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3291
3292 // Otherwise, we have to insert explicit sign or zero extends. Note
3293 // that we could insert sign extends for ALL conditions, but zero extend
3294 // is cheaper on many machines (an AND instead of two shifts), so prefer
3295 // it.
3296 switch (cast<CondCodeSDNode>(CC)->get()) {
3297 default: assert(0 && "Unknown integer comparison!");
3298 case ISD::SETEQ:
3299 case ISD::SETNE:
3300 case ISD::SETUGE:
3301 case ISD::SETUGT:
3302 case ISD::SETULE:
3303 case ISD::SETULT:
3304 // ALL of these operations will work if we either sign or zero extend
3305 // the operands (including the unsigned comparisons!). Zero extend is
3306 // usually a simpler/cheaper operation, so prefer it.
3307 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3308 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3309 break;
3310 case ISD::SETGE:
3311 case ISD::SETGT:
3312 case ISD::SETLT:
3313 case ISD::SETLE:
3314 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3315 DAG.getValueType(VT));
3316 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3317 DAG.getValueType(VT));
3318 break;
3319 }
3320 }
3321 break;
3322 case Expand:
3323 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3324 ExpandOp(LHS, LHSLo, LHSHi);
3325 ExpandOp(RHS, RHSLo, RHSHi);
3326 switch (cast<CondCodeSDNode>(CC)->get()) {
3327 case ISD::SETEQ:
3328 case ISD::SETNE:
3329 if (RHSLo == RHSHi)
3330 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3331 if (RHSCST->isAllOnesValue()) {
3332 // Comparison to -1.
3333 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3334 Tmp2 = RHSLo;
3335 break;
3336 }
3337
3338 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3339 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3340 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3341 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3342 break;
3343 default:
3344 // If this is a comparison of the sign bit, just look at the top part.
3345 // X > -1, x < 0
3346 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3347 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3348 CST->getValue() == 0) || // X < 0
3349 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3350 CST->isAllOnesValue())) { // X > -1
3351 Tmp1 = LHSHi;
3352 Tmp2 = RHSHi;
3353 break;
3354 }
3355
3356 // FIXME: This generated code sucks.
3357 ISD::CondCode LowCC;
3358 switch (cast<CondCodeSDNode>(CC)->get()) {
3359 default: assert(0 && "Unknown integer setcc!");
3360 case ISD::SETLT:
3361 case ISD::SETULT: LowCC = ISD::SETULT; break;
3362 case ISD::SETGT:
3363 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3364 case ISD::SETLE:
3365 case ISD::SETULE: LowCC = ISD::SETULE; break;
3366 case ISD::SETGE:
3367 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3368 }
3369
3370 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3371 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3372 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3373
3374 // NOTE: on targets without efficient SELECT of bools, we can always use
3375 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3376 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3377 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3378 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3379 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3380 Result, Tmp1, Tmp2));
3381 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003382 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003383 }
3384 }
3385 LHS = Tmp1;
3386 RHS = Tmp2;
3387}
3388
Chris Lattner35481892005-12-23 00:16:34 +00003389/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003390/// The resultant code need not be legal. Note that SrcOp is the input operand
3391/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003392SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3393 SDOperand SrcOp) {
3394 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003395 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003396
3397 // Emit a store to the stack slot.
3398 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003399 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003400 // Result is a load from the stack slot.
3401 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3402}
3403
Chris Lattner4352cc92006-04-04 17:23:26 +00003404SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3405 // Create a vector sized/aligned stack slot, store the value to element #0,
3406 // then load the whole vector back out.
3407 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3408 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3409 Node->getOperand(0), StackPtr,
3410 DAG.getSrcValue(NULL));
3411 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3412}
3413
3414
Chris Lattnerce872152006-03-19 06:31:19 +00003415/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3416/// support the operation, but do support the resultant packed vector type.
3417SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3418
3419 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003420 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003421 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003422 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003423 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003424 std::map<SDOperand, std::vector<unsigned> > Values;
3425 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003426 bool isConstant = true;
3427 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3428 SplatValue.getOpcode() != ISD::UNDEF)
3429 isConstant = false;
3430
Evan Cheng033e6812006-03-24 01:17:21 +00003431 for (unsigned i = 1; i < NumElems; ++i) {
3432 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003433 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003434 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003435 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003436 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003437 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003438
3439 // If this isn't a constant element or an undef, we can't use a constant
3440 // pool load.
3441 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3442 V.getOpcode() != ISD::UNDEF)
3443 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003444 }
3445
3446 if (isOnlyLowElement) {
3447 // If the low element is an undef too, then this whole things is an undef.
3448 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3449 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3450 // Otherwise, turn this into a scalar_to_vector node.
3451 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3452 Node->getOperand(0));
3453 }
3454
Chris Lattner2eb86532006-03-24 07:29:17 +00003455 // If all elements are constants, create a load from the constant pool.
3456 if (isConstant) {
3457 MVT::ValueType VT = Node->getValueType(0);
3458 const Type *OpNTy =
3459 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3460 std::vector<Constant*> CV;
3461 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3462 if (ConstantFPSDNode *V =
3463 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3464 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3465 } else if (ConstantSDNode *V =
3466 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3467 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3468 } else {
3469 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3470 CV.push_back(UndefValue::get(OpNTy));
3471 }
3472 }
3473 Constant *CP = ConstantPacked::get(CV);
3474 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3475 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3476 DAG.getSrcValue(NULL));
3477 }
3478
Chris Lattner87100e02006-03-20 01:52:29 +00003479 if (SplatValue.Val) { // Splat of one value?
3480 // Build the shuffle constant vector: <0, 0, 0, 0>
3481 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003482 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003483 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003484 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003485 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3486
3487 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003488 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003489 // Get the splatted value into the low element of a vector register.
3490 SDOperand LowValVec =
3491 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3492
3493 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3494 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3495 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3496 SplatMask);
3497 }
3498 }
3499
Evan Cheng033e6812006-03-24 01:17:21 +00003500 // If there are only two unique elements, we may be able to turn this into a
3501 // vector shuffle.
3502 if (Values.size() == 2) {
3503 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3504 MVT::ValueType MaskVT =
3505 MVT::getIntVectorWithNumElements(NumElems);
3506 std::vector<SDOperand> MaskVec(NumElems);
3507 unsigned i = 0;
3508 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3509 E = Values.end(); I != E; ++I) {
3510 for (std::vector<unsigned>::iterator II = I->second.begin(),
3511 EE = I->second.end(); II != EE; ++II)
3512 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3513 i += NumElems;
3514 }
3515 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3516
3517 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003518 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3519 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng033e6812006-03-24 01:17:21 +00003520 std::vector<SDOperand> Ops;
3521 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3522 E = Values.end(); I != E; ++I) {
3523 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3524 I->first);
3525 Ops.push_back(Op);
3526 }
3527 Ops.push_back(ShuffleMask);
3528
3529 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3530 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3531 }
3532 }
Chris Lattnerce872152006-03-19 06:31:19 +00003533
3534 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3535 // aligned object on the stack, store each element into it, then load
3536 // the result as a vector.
3537 MVT::ValueType VT = Node->getValueType(0);
3538 // Create the stack frame object.
3539 SDOperand FIPtr = CreateStackTemporary(VT);
3540
3541 // Emit a store of each element to the stack slot.
3542 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003543 unsigned TypeByteSize =
3544 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3545 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3546 // Store (in the right endianness) the elements to memory.
3547 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3548 // Ignore undef elements.
3549 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3550
Chris Lattner841c8822006-03-22 01:46:54 +00003551 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003552
3553 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3554 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3555
3556 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3557 Node->getOperand(i), Idx,
3558 DAG.getSrcValue(NULL)));
3559 }
3560
3561 SDOperand StoreChain;
3562 if (!Stores.empty()) // Not all undef elements?
3563 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3564 else
3565 StoreChain = DAG.getEntryNode();
3566
3567 // Result is a load from the stack slot.
3568 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3569}
3570
3571/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3572/// specified value type.
3573SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3574 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3575 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3576 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3577 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3578}
3579
Chris Lattner5b359c62005-04-02 04:00:59 +00003580void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3581 SDOperand Op, SDOperand Amt,
3582 SDOperand &Lo, SDOperand &Hi) {
3583 // Expand the subcomponents.
3584 SDOperand LHSL, LHSH;
3585 ExpandOp(Op, LHSL, LHSH);
3586
3587 std::vector<SDOperand> Ops;
3588 Ops.push_back(LHSL);
3589 Ops.push_back(LHSH);
3590 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003591 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003592 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003593 Hi = Lo.getValue(1);
3594}
3595
3596
Chris Lattnere34b3962005-01-19 04:19:40 +00003597/// ExpandShift - Try to find a clever way to expand this shift operation out to
3598/// smaller elements. If we can't find a way that is more efficient than a
3599/// libcall on this target, return false. Otherwise, return true with the
3600/// low-parts expanded into Lo and Hi.
3601bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3602 SDOperand &Lo, SDOperand &Hi) {
3603 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3604 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003605
Chris Lattnere34b3962005-01-19 04:19:40 +00003606 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003607 SDOperand ShAmt = LegalizeOp(Amt);
3608 MVT::ValueType ShTy = ShAmt.getValueType();
3609 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3610 unsigned NVTBits = MVT::getSizeInBits(NVT);
3611
3612 // Handle the case when Amt is an immediate. Other cases are currently broken
3613 // and are disabled.
3614 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3615 unsigned Cst = CN->getValue();
3616 // Expand the incoming operand to be shifted, so that we have its parts
3617 SDOperand InL, InH;
3618 ExpandOp(Op, InL, InH);
3619 switch(Opc) {
3620 case ISD::SHL:
3621 if (Cst > VTBits) {
3622 Lo = DAG.getConstant(0, NVT);
3623 Hi = DAG.getConstant(0, NVT);
3624 } else if (Cst > NVTBits) {
3625 Lo = DAG.getConstant(0, NVT);
3626 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003627 } else if (Cst == NVTBits) {
3628 Lo = DAG.getConstant(0, NVT);
3629 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003630 } else {
3631 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3632 Hi = DAG.getNode(ISD::OR, NVT,
3633 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3634 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3635 }
3636 return true;
3637 case ISD::SRL:
3638 if (Cst > VTBits) {
3639 Lo = DAG.getConstant(0, NVT);
3640 Hi = DAG.getConstant(0, NVT);
3641 } else if (Cst > NVTBits) {
3642 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3643 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003644 } else if (Cst == NVTBits) {
3645 Lo = InH;
3646 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003647 } else {
3648 Lo = DAG.getNode(ISD::OR, NVT,
3649 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3650 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3651 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3652 }
3653 return true;
3654 case ISD::SRA:
3655 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003656 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003657 DAG.getConstant(NVTBits-1, ShTy));
3658 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003659 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003660 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003661 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003662 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003663 } else if (Cst == NVTBits) {
3664 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003665 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003666 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003667 } else {
3668 Lo = DAG.getNode(ISD::OR, NVT,
3669 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3670 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3671 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3672 }
3673 return true;
3674 }
3675 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003676 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003677}
Chris Lattner77e77a62005-01-21 06:05:23 +00003678
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003679
Chris Lattner77e77a62005-01-21 06:05:23 +00003680// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3681// does not fit into a register, return the lo part and set the hi part to the
3682// by-reg argument. If it does fit into a single register, return the result
3683// and leave the Hi part unset.
3684SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3685 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003686 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3687 // The input chain to this libcall is the entry node of the function.
3688 // Legalizing the call will automatically add the previous call to the
3689 // dependence.
3690 SDOperand InChain = DAG.getEntryNode();
3691
Chris Lattner77e77a62005-01-21 06:05:23 +00003692 TargetLowering::ArgListTy Args;
3693 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3694 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3695 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3696 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3697 }
3698 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003699
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003700 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003701 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003702 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003703 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3704 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003705
Chris Lattner6831a812006-02-13 09:18:02 +00003706 // Legalize the call sequence, starting with the chain. This will advance
3707 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3708 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3709 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003710 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003711 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003712 default: assert(0 && "Unknown thing");
3713 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003714 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003715 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003716 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003717 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003718 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003719 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003720 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003721}
3722
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003723
Chris Lattner77e77a62005-01-21 06:05:23 +00003724/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3725/// destination type is legal.
3726SDOperand SelectionDAGLegalize::
3727ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003728 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003729 assert(getTypeAction(Source.getValueType()) == Expand &&
3730 "This is not an expansion!");
3731 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3732
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003733 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003734 assert(Source.getValueType() == MVT::i64 &&
3735 "This only works for 64-bit -> FP");
3736 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3737 // incoming integer is set. To handle this, we dynamically test to see if
3738 // it is set, and, if so, add a fudge factor.
3739 SDOperand Lo, Hi;
3740 ExpandOp(Source, Lo, Hi);
3741
Chris Lattner66de05b2005-05-13 04:45:13 +00003742 // If this is unsigned, and not supported, first perform the conversion to
3743 // signed, then adjust the result if the sign bit is set.
3744 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3745 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3746
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003747 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3748 DAG.getConstant(0, Hi.getValueType()),
3749 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003750 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3751 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3752 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003753 uint64_t FF = 0x5f800000ULL;
3754 if (TLI.isLittleEndian()) FF <<= 32;
3755 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003756
Chris Lattner5839bf22005-08-26 17:15:30 +00003757 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003758 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3759 SDOperand FudgeInReg;
3760 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003761 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3762 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003763 else {
3764 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003765 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3766 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003767 }
Chris Lattner473a9902005-09-29 06:44:39 +00003768 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003769 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003770
Chris Lattnera88a2602005-05-14 05:33:54 +00003771 // Check to see if the target has a custom way to lower this. If so, use it.
3772 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3773 default: assert(0 && "This action not implemented for this operation!");
3774 case TargetLowering::Legal:
3775 case TargetLowering::Expand:
3776 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003777 case TargetLowering::Custom: {
3778 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3779 Source), DAG);
3780 if (NV.Val)
3781 return LegalizeOp(NV);
3782 break; // The target decided this was legal after all
3783 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003784 }
3785
Chris Lattner13689e22005-05-12 07:00:44 +00003786 // Expand the source, then glue it back together for the call. We must expand
3787 // the source in case it is shared (this pass of legalize must traverse it).
3788 SDOperand SrcLo, SrcHi;
3789 ExpandOp(Source, SrcLo, SrcHi);
3790 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3791
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003792 const char *FnName = 0;
3793 if (DestTy == MVT::f32)
3794 FnName = "__floatdisf";
3795 else {
3796 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3797 FnName = "__floatdidf";
3798 }
Chris Lattner6831a812006-02-13 09:18:02 +00003799
3800 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3801 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003802 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003803}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003804
Chris Lattner22cde6a2006-01-28 08:25:58 +00003805/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3806/// INT_TO_FP operation of the specified operand when the target requests that
3807/// we expand it. At this point, we know that the result and operand types are
3808/// legal for the target.
3809SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3810 SDOperand Op0,
3811 MVT::ValueType DestVT) {
3812 if (Op0.getValueType() == MVT::i32) {
3813 // simple 32-bit [signed|unsigned] integer to float/double expansion
3814
3815 // get the stack frame index of a 8 byte buffer
3816 MachineFunction &MF = DAG.getMachineFunction();
3817 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3818 // get address of 8 byte buffer
3819 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3820 // word offset constant for Hi/Lo address computation
3821 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3822 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003823 SDOperand Hi = StackSlot;
3824 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3825 if (TLI.isLittleEndian())
3826 std::swap(Hi, Lo);
3827
Chris Lattner22cde6a2006-01-28 08:25:58 +00003828 // if signed map to unsigned space
3829 SDOperand Op0Mapped;
3830 if (isSigned) {
3831 // constant used to invert sign bit (signed to unsigned mapping)
3832 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3833 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3834 } else {
3835 Op0Mapped = Op0;
3836 }
3837 // store the lo of the constructed double - based on integer input
3838 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3839 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3840 // initial hi portion of constructed double
3841 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3842 // store the hi of the constructed double - biased exponent
3843 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3844 InitialHi, Hi, DAG.getSrcValue(NULL));
3845 // load the constructed double
3846 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3847 DAG.getSrcValue(NULL));
3848 // FP constant to bias correct the final result
3849 SDOperand Bias = DAG.getConstantFP(isSigned ?
3850 BitsToDouble(0x4330000080000000ULL)
3851 : BitsToDouble(0x4330000000000000ULL),
3852 MVT::f64);
3853 // subtract the bias
3854 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3855 // final result
3856 SDOperand Result;
3857 // handle final rounding
3858 if (DestVT == MVT::f64) {
3859 // do nothing
3860 Result = Sub;
3861 } else {
3862 // if f32 then cast to f32
3863 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3864 }
3865 return Result;
3866 }
3867 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3868 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3869
3870 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3871 DAG.getConstant(0, Op0.getValueType()),
3872 ISD::SETLT);
3873 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3874 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3875 SignSet, Four, Zero);
3876
3877 // If the sign bit of the integer is set, the large number will be treated
3878 // as a negative number. To counteract this, the dynamic code adds an
3879 // offset depending on the data type.
3880 uint64_t FF;
3881 switch (Op0.getValueType()) {
3882 default: assert(0 && "Unsupported integer type!");
3883 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3884 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3885 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3886 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3887 }
3888 if (TLI.isLittleEndian()) FF <<= 32;
3889 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3890
3891 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3892 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3893 SDOperand FudgeInReg;
3894 if (DestVT == MVT::f32)
3895 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3896 DAG.getSrcValue(NULL));
3897 else {
3898 assert(DestVT == MVT::f64 && "Unexpected conversion");
3899 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3900 DAG.getEntryNode(), CPIdx,
3901 DAG.getSrcValue(NULL), MVT::f32));
3902 }
3903
3904 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3905}
3906
3907/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3908/// *INT_TO_FP operation of the specified operand when the target requests that
3909/// we promote it. At this point, we know that the result and operand types are
3910/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3911/// operation that takes a larger input.
3912SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3913 MVT::ValueType DestVT,
3914 bool isSigned) {
3915 // First step, figure out the appropriate *INT_TO_FP operation to use.
3916 MVT::ValueType NewInTy = LegalOp.getValueType();
3917
3918 unsigned OpToUse = 0;
3919
3920 // Scan for the appropriate larger type to use.
3921 while (1) {
3922 NewInTy = (MVT::ValueType)(NewInTy+1);
3923 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3924
3925 // If the target supports SINT_TO_FP of this type, use it.
3926 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3927 default: break;
3928 case TargetLowering::Legal:
3929 if (!TLI.isTypeLegal(NewInTy))
3930 break; // Can't use this datatype.
3931 // FALL THROUGH.
3932 case TargetLowering::Custom:
3933 OpToUse = ISD::SINT_TO_FP;
3934 break;
3935 }
3936 if (OpToUse) break;
3937 if (isSigned) continue;
3938
3939 // If the target supports UINT_TO_FP of this type, use it.
3940 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3941 default: break;
3942 case TargetLowering::Legal:
3943 if (!TLI.isTypeLegal(NewInTy))
3944 break; // Can't use this datatype.
3945 // FALL THROUGH.
3946 case TargetLowering::Custom:
3947 OpToUse = ISD::UINT_TO_FP;
3948 break;
3949 }
3950 if (OpToUse) break;
3951
3952 // Otherwise, try a larger type.
3953 }
3954
3955 // Okay, we found the operation and type to use. Zero extend our input to the
3956 // desired type then run the operation on it.
3957 return DAG.getNode(OpToUse, DestVT,
3958 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3959 NewInTy, LegalOp));
3960}
3961
3962/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3963/// FP_TO_*INT operation of the specified operand when the target requests that
3964/// we promote it. At this point, we know that the result and operand types are
3965/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3966/// operation that returns a larger result.
3967SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3968 MVT::ValueType DestVT,
3969 bool isSigned) {
3970 // First step, figure out the appropriate FP_TO*INT operation to use.
3971 MVT::ValueType NewOutTy = DestVT;
3972
3973 unsigned OpToUse = 0;
3974
3975 // Scan for the appropriate larger type to use.
3976 while (1) {
3977 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3978 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3979
3980 // If the target supports FP_TO_SINT returning this type, use it.
3981 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3982 default: break;
3983 case TargetLowering::Legal:
3984 if (!TLI.isTypeLegal(NewOutTy))
3985 break; // Can't use this datatype.
3986 // FALL THROUGH.
3987 case TargetLowering::Custom:
3988 OpToUse = ISD::FP_TO_SINT;
3989 break;
3990 }
3991 if (OpToUse) break;
3992
3993 // If the target supports FP_TO_UINT of this type, use it.
3994 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3995 default: break;
3996 case TargetLowering::Legal:
3997 if (!TLI.isTypeLegal(NewOutTy))
3998 break; // Can't use this datatype.
3999 // FALL THROUGH.
4000 case TargetLowering::Custom:
4001 OpToUse = ISD::FP_TO_UINT;
4002 break;
4003 }
4004 if (OpToUse) break;
4005
4006 // Otherwise, try a larger type.
4007 }
4008
4009 // Okay, we found the operation and type to use. Truncate the result of the
4010 // extended FP_TO_*INT operation to the desired size.
4011 return DAG.getNode(ISD::TRUNCATE, DestVT,
4012 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4013}
4014
4015/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4016///
4017SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4018 MVT::ValueType VT = Op.getValueType();
4019 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4020 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4021 switch (VT) {
4022 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4023 case MVT::i16:
4024 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4025 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4026 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4027 case MVT::i32:
4028 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4029 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4030 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4031 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4032 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4033 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4034 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4035 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4036 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4037 case MVT::i64:
4038 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4039 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4040 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4041 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4042 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4043 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4044 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4045 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4046 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4047 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4048 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4049 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4050 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4051 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4052 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4053 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4054 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4055 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4056 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4057 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4058 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4059 }
4060}
4061
4062/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4063///
4064SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4065 switch (Opc) {
4066 default: assert(0 && "Cannot expand this yet!");
4067 case ISD::CTPOP: {
4068 static const uint64_t mask[6] = {
4069 0x5555555555555555ULL, 0x3333333333333333ULL,
4070 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4071 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4072 };
4073 MVT::ValueType VT = Op.getValueType();
4074 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4075 unsigned len = getSizeInBits(VT);
4076 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4077 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4078 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4079 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4080 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4081 DAG.getNode(ISD::AND, VT,
4082 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4083 }
4084 return Op;
4085 }
4086 case ISD::CTLZ: {
4087 // for now, we do this:
4088 // x = x | (x >> 1);
4089 // x = x | (x >> 2);
4090 // ...
4091 // x = x | (x >>16);
4092 // x = x | (x >>32); // for 64-bit input
4093 // return popcount(~x);
4094 //
4095 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4096 MVT::ValueType VT = Op.getValueType();
4097 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4098 unsigned len = getSizeInBits(VT);
4099 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4100 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4101 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4102 }
4103 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4104 return DAG.getNode(ISD::CTPOP, VT, Op);
4105 }
4106 case ISD::CTTZ: {
4107 // for now, we use: { return popcount(~x & (x - 1)); }
4108 // unless the target has ctlz but not ctpop, in which case we use:
4109 // { return 32 - nlz(~x & (x-1)); }
4110 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4111 MVT::ValueType VT = Op.getValueType();
4112 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4113 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4114 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4115 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4116 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4117 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4118 TLI.isOperationLegal(ISD::CTLZ, VT))
4119 return DAG.getNode(ISD::SUB, VT,
4120 DAG.getConstant(getSizeInBits(VT), VT),
4121 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4122 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4123 }
4124 }
4125}
Chris Lattnere34b3962005-01-19 04:19:40 +00004126
Chris Lattner3e928bb2005-01-07 07:47:09 +00004127/// ExpandOp - Expand the specified SDOperand into its two component pieces
4128/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4129/// LegalizeNodes map is filled in for any results that are not expanded, the
4130/// ExpandedNodes map is filled in for any results that are expanded, and the
4131/// Lo/Hi values are returned.
4132void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4133 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004134 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004135 SDNode *Node = Op.Val;
4136 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00004137 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4138 "Cannot expand FP values!");
4139 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004140 "Cannot expand to FP value or to larger int value!");
4141
Chris Lattner6fdcb252005-09-02 20:32:45 +00004142 // See if we already expanded it.
4143 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4144 = ExpandedNodes.find(Op);
4145 if (I != ExpandedNodes.end()) {
4146 Lo = I->second.first;
4147 Hi = I->second.second;
4148 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004149 }
4150
Chris Lattner3e928bb2005-01-07 07:47:09 +00004151 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004152 case ISD::CopyFromReg:
4153 assert(0 && "CopyFromReg must be legal!");
4154 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004155 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
4156 assert(0 && "Do not know how to expand this operator!");
4157 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004158 case ISD::UNDEF:
4159 Lo = DAG.getNode(ISD::UNDEF, NVT);
4160 Hi = DAG.getNode(ISD::UNDEF, NVT);
4161 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004162 case ISD::Constant: {
4163 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4164 Lo = DAG.getConstant(Cst, NVT);
4165 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4166 break;
4167 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004168 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004169 // Return the operands.
4170 Lo = Node->getOperand(0);
4171 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004172 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004173
4174 case ISD::SIGN_EXTEND_INREG:
4175 ExpandOp(Node->getOperand(0), Lo, Hi);
4176 // Sign extend the lo-part.
4177 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4178 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4179 TLI.getShiftAmountTy()));
4180 // sext_inreg the low part if needed.
4181 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4182 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004183
Nate Begemand88fc032006-01-14 03:14:10 +00004184 case ISD::BSWAP: {
4185 ExpandOp(Node->getOperand(0), Lo, Hi);
4186 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4187 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4188 Lo = TempLo;
4189 break;
4190 }
4191
Chris Lattneredb1add2005-05-11 04:51:16 +00004192 case ISD::CTPOP:
4193 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004194 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4195 DAG.getNode(ISD::CTPOP, NVT, Lo),
4196 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004197 Hi = DAG.getConstant(0, NVT);
4198 break;
4199
Chris Lattner39a8f332005-05-12 19:05:01 +00004200 case ISD::CTLZ: {
4201 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004202 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004203 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4204 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004205 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4206 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004207 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4208 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4209
4210 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4211 Hi = DAG.getConstant(0, NVT);
4212 break;
4213 }
4214
4215 case ISD::CTTZ: {
4216 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004217 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004218 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4219 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004220 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4221 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004222 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4223 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4224
4225 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4226 Hi = DAG.getConstant(0, NVT);
4227 break;
4228 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004229
Nate Begemanacc398c2006-01-25 18:21:52 +00004230 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004231 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4232 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004233 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4234 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4235
4236 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004237 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004238 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4239 if (!TLI.isLittleEndian())
4240 std::swap(Lo, Hi);
4241 break;
4242 }
4243
Chris Lattner3e928bb2005-01-07 07:47:09 +00004244 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004245 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4246 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004247 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004248
4249 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00004250 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004251 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4252 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00004253 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004254 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00004255
4256 // Build a factor node to remember that this load is independent of the
4257 // other one.
4258 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4259 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004260
Chris Lattner3e928bb2005-01-07 07:47:09 +00004261 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004262 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004263 if (!TLI.isLittleEndian())
4264 std::swap(Lo, Hi);
4265 break;
4266 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004267 case ISD::AND:
4268 case ISD::OR:
4269 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4270 SDOperand LL, LH, RL, RH;
4271 ExpandOp(Node->getOperand(0), LL, LH);
4272 ExpandOp(Node->getOperand(1), RL, RH);
4273 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4274 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4275 break;
4276 }
4277 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004278 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004279 ExpandOp(Node->getOperand(1), LL, LH);
4280 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00004281 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4282 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004283 break;
4284 }
Nate Begeman9373a812005-08-10 20:51:12 +00004285 case ISD::SELECT_CC: {
4286 SDOperand TL, TH, FL, FH;
4287 ExpandOp(Node->getOperand(2), TL, TH);
4288 ExpandOp(Node->getOperand(3), FL, FH);
4289 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4290 Node->getOperand(1), TL, FL, Node->getOperand(4));
4291 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4292 Node->getOperand(1), TH, FH, Node->getOperand(4));
4293 break;
4294 }
Nate Begeman144ff662005-10-13 17:15:37 +00004295 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004296 SDOperand Chain = Node->getOperand(0);
4297 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004298 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4299
4300 if (EVT == NVT)
4301 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4302 else
4303 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4304 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004305
4306 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004307 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004308
Nate Begeman144ff662005-10-13 17:15:37 +00004309 // The high part is obtained by SRA'ing all but one of the bits of the lo
4310 // part.
4311 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4312 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4313 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00004314 break;
4315 }
4316 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004317 SDOperand Chain = Node->getOperand(0);
4318 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004319 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4320
4321 if (EVT == NVT)
4322 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4323 else
4324 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4325 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004326
4327 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004328 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004329
Nate Begeman144ff662005-10-13 17:15:37 +00004330 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004331 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004332 break;
4333 }
4334 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004335 SDOperand Chain = Node->getOperand(0);
4336 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00004337 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4338
4339 if (EVT == NVT)
4340 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4341 else
4342 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4343 EVT);
4344
4345 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004346 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004347
4348 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004349 Hi = DAG.getNode(ISD::UNDEF, NVT);
4350 break;
4351 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004352 case ISD::ANY_EXTEND:
4353 // The low part is any extension of the input (which degenerates to a copy).
4354 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4355 // The high part is undefined.
4356 Hi = DAG.getNode(ISD::UNDEF, NVT);
4357 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004358 case ISD::SIGN_EXTEND: {
4359 // The low part is just a sign extension of the input (which degenerates to
4360 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004361 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004362
Chris Lattner3e928bb2005-01-07 07:47:09 +00004363 // The high part is obtained by SRA'ing all but one of the bits of the lo
4364 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004365 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004366 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4367 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004368 break;
4369 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004370 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004371 // The low part is just a zero extension of the input (which degenerates to
4372 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004373 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004374
Chris Lattner3e928bb2005-01-07 07:47:09 +00004375 // The high part is just a zero.
4376 Hi = DAG.getConstant(0, NVT);
4377 break;
Chris Lattner35481892005-12-23 00:16:34 +00004378
4379 case ISD::BIT_CONVERT: {
4380 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4381 Node->getOperand(0));
4382 ExpandOp(Tmp, Lo, Hi);
4383 break;
4384 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004385
Chris Lattner8137c9e2006-01-28 05:07:51 +00004386 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004387 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4388 TargetLowering::Custom &&
4389 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004390 Lo = TLI.LowerOperation(Op, DAG);
4391 assert(Lo.Val && "Node must be custom expanded!");
4392 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004393 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004394 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004395 break;
4396
Chris Lattner4e6c7462005-01-08 19:27:05 +00004397 // These operators cannot be expanded directly, emit them as calls to
4398 // library functions.
4399 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004400 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004401 SDOperand Op;
4402 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4403 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004404 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4405 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004406 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004407
Chris Lattnerf20d1832005-07-30 01:40:57 +00004408 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4409
Chris Lattner80a3e942005-07-29 00:33:32 +00004410 // Now that the custom expander is done, expand the result, which is still
4411 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004412 if (Op.Val) {
4413 ExpandOp(Op, Lo, Hi);
4414 break;
4415 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004416 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004417
Chris Lattner4e6c7462005-01-08 19:27:05 +00004418 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004419 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004420 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004421 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004422 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004423
Chris Lattner4e6c7462005-01-08 19:27:05 +00004424 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004425 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004426 SDOperand Op;
4427 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4428 case Expand: assert(0 && "cannot expand FP!");
4429 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4430 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4431 }
4432
4433 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4434
4435 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004436 if (Op.Val) {
4437 ExpandOp(Op, Lo, Hi);
4438 break;
4439 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004440 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004441
Chris Lattner4e6c7462005-01-08 19:27:05 +00004442 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004443 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004444 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004445 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004446 break;
4447
Evan Cheng05a2d562006-01-09 18:31:59 +00004448 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004449 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004450 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004451 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004452 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004453 Op = TLI.LowerOperation(Op, DAG);
4454 if (Op.Val) {
4455 // Now that the custom expander is done, expand the result, which is
4456 // still VT.
4457 ExpandOp(Op, Lo, Hi);
4458 break;
4459 }
4460 }
4461
Chris Lattnere34b3962005-01-19 04:19:40 +00004462 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004463 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004464 break;
Chris Lattner47599822005-04-02 03:38:53 +00004465
4466 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004467 TargetLowering::LegalizeAction Action =
4468 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4469 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4470 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004471 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004472 break;
4473 }
4474
Chris Lattnere34b3962005-01-19 04:19:40 +00004475 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004476 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004477 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004478 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004479
Evan Cheng05a2d562006-01-09 18:31:59 +00004480 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004481 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004482 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004483 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004484 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004485 Op = TLI.LowerOperation(Op, DAG);
4486 if (Op.Val) {
4487 // Now that the custom expander is done, expand the result, which is
4488 // still VT.
4489 ExpandOp(Op, Lo, Hi);
4490 break;
4491 }
4492 }
4493
Chris Lattnere34b3962005-01-19 04:19:40 +00004494 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004495 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004496 break;
Chris Lattner47599822005-04-02 03:38:53 +00004497
4498 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004499 TargetLowering::LegalizeAction Action =
4500 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4501 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4502 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004503 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004504 break;
4505 }
4506
Chris Lattnere34b3962005-01-19 04:19:40 +00004507 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004508 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004509 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004510 }
4511
4512 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004513 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004514 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004515 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004516 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004517 Op = TLI.LowerOperation(Op, DAG);
4518 if (Op.Val) {
4519 // Now that the custom expander is done, expand the result, which is
4520 // still VT.
4521 ExpandOp(Op, Lo, Hi);
4522 break;
4523 }
4524 }
4525
Chris Lattnere34b3962005-01-19 04:19:40 +00004526 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004527 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004528 break;
Chris Lattner47599822005-04-02 03:38:53 +00004529
4530 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004531 TargetLowering::LegalizeAction Action =
4532 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4533 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4534 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004535 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004536 break;
4537 }
4538
Chris Lattnere34b3962005-01-19 04:19:40 +00004539 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004540 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004541 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004542 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004543
Misha Brukmanedf128a2005-04-21 22:36:52 +00004544 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004545 case ISD::SUB: {
4546 // If the target wants to custom expand this, let them.
4547 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4548 TargetLowering::Custom) {
4549 Op = TLI.LowerOperation(Op, DAG);
4550 if (Op.Val) {
4551 ExpandOp(Op, Lo, Hi);
4552 break;
4553 }
4554 }
4555
4556 // Expand the subcomponents.
4557 SDOperand LHSL, LHSH, RHSL, RHSH;
4558 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4559 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004560 std::vector<MVT::ValueType> VTs;
4561 std::vector<SDOperand> LoOps, HiOps;
4562 VTs.push_back(LHSL.getValueType());
4563 VTs.push_back(MVT::Flag);
4564 LoOps.push_back(LHSL);
4565 LoOps.push_back(RHSL);
4566 HiOps.push_back(LHSH);
4567 HiOps.push_back(RHSH);
4568 if (Node->getOpcode() == ISD::ADD) {
4569 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4570 HiOps.push_back(Lo.getValue(1));
4571 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4572 } else {
4573 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4574 HiOps.push_back(Lo.getValue(1));
4575 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4576 }
Chris Lattner84f67882005-01-20 18:52:28 +00004577 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004578 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004579 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004580 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004581 SDOperand LL, LH, RL, RH;
4582 ExpandOp(Node->getOperand(0), LL, LH);
4583 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004584 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4585 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4586 // extended the sign bit of the low half through the upper half, and if so
4587 // emit a MULHS instead of the alternate sequence that is valid for any
4588 // i64 x i64 multiply.
4589 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4590 // is RH an extension of the sign bit of RL?
4591 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4592 RH.getOperand(1).getOpcode() == ISD::Constant &&
4593 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4594 // is LH an extension of the sign bit of LL?
4595 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4596 LH.getOperand(1).getOpcode() == ISD::Constant &&
4597 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4598 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4599 } else {
4600 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4601 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4602 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4603 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4604 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4605 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004606 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4607 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004608 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004609 }
4610 break;
4611 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004612 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4613 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4614 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4615 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004616 }
4617
Chris Lattner83397362005-12-21 18:02:52 +00004618 // Make sure the resultant values have been legalized themselves, unless this
4619 // is a type that requires multi-step expansion.
4620 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4621 Lo = LegalizeOp(Lo);
4622 Hi = LegalizeOp(Hi);
4623 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004624
4625 // Remember in a map if the values will be reused later.
4626 bool isNew =
4627 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4628 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004629}
4630
Chris Lattnerc7029802006-03-18 01:44:44 +00004631/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4632/// two smaller values of MVT::Vector type.
4633void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4634 SDOperand &Hi) {
4635 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4636 SDNode *Node = Op.Val;
4637 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4638 assert(NumElements > 1 && "Cannot split a single element vector!");
4639 unsigned NewNumElts = NumElements/2;
4640 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4641 SDOperand TypeNode = *(Node->op_end()-1);
4642
4643 // See if we already split it.
4644 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4645 = SplitNodes.find(Op);
4646 if (I != SplitNodes.end()) {
4647 Lo = I->second.first;
4648 Hi = I->second.second;
4649 return;
4650 }
4651
4652 switch (Node->getOpcode()) {
Chris Lattner3824e502006-04-14 06:08:35 +00004653 default: Node->dump(); assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004654 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004655 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4656 LoOps.push_back(NewNumEltsNode);
4657 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004658 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004659
4660 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4661 HiOps.push_back(NewNumEltsNode);
4662 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004663 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004664 break;
4665 }
4666 case ISD::VADD:
4667 case ISD::VSUB:
4668 case ISD::VMUL:
4669 case ISD::VSDIV:
4670 case ISD::VUDIV:
4671 case ISD::VAND:
4672 case ISD::VOR:
4673 case ISD::VXOR: {
4674 SDOperand LL, LH, RL, RH;
4675 SplitVectorOp(Node->getOperand(0), LL, LH);
4676 SplitVectorOp(Node->getOperand(1), RL, RH);
4677
4678 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4679 NewNumEltsNode, TypeNode);
4680 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4681 NewNumEltsNode, TypeNode);
4682 break;
4683 }
4684 case ISD::VLOAD: {
4685 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4686 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4687 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4688
4689 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4690 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4691 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4692 getIntPtrConstant(IncrementSize));
4693 // FIXME: This creates a bogus srcvalue!
4694 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4695
4696 // Build a factor node to remember that this load is independent of the
4697 // other one.
4698 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4699 Hi.getValue(1));
4700
4701 // Remember that we legalized the chain.
4702 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00004703 break;
4704 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004705 case ISD::VBIT_CONVERT: {
4706 // We know the result is a vector. The input may be either a vector or a
4707 // scalar value.
4708 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4709 // Lower to a store/load. FIXME: this could be improved probably.
4710 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4711
4712 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4713 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4714 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4715 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4716 SplitVectorOp(St, Lo, Hi);
4717 } else {
4718 // If the input is a vector type, we have to either scalarize it, pack it
4719 // or convert it based on whether the input vector type is legal.
4720 SDNode *InVal = Node->getOperand(0).Val;
4721 unsigned NumElems =
4722 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4723 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4724
4725 // If the input is from a single element vector, scalarize the vector,
4726 // then treat like a scalar.
4727 if (NumElems == 1) {
4728 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4729 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4730 Op.getOperand(1), Op.getOperand(2));
4731 SplitVectorOp(Scalar, Lo, Hi);
4732 } else {
4733 // Split the input vector.
4734 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4735
4736 // Convert each of the pieces now.
4737 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4738 NewNumEltsNode, TypeNode);
4739 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4740 NewNumEltsNode, TypeNode);
4741 }
4742 break;
4743 }
4744 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004745 }
4746
4747 // Remember in a map if the values will be reused later.
4748 bool isNew =
4749 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4750 assert(isNew && "Value already expanded?!?");
4751}
4752
4753
4754/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4755/// equivalent operation that returns a scalar (e.g. F32) or packed value
4756/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4757/// type for the result.
4758SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4759 MVT::ValueType NewVT) {
4760 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4761 SDNode *Node = Op.Val;
4762
4763 // See if we already packed it.
4764 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4765 if (I != PackedNodes.end()) return I->second;
4766
4767 SDOperand Result;
4768 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004769 default:
4770 Node->dump(); std::cerr << "\n";
4771 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004772 case ISD::VADD:
4773 case ISD::VSUB:
4774 case ISD::VMUL:
4775 case ISD::VSDIV:
4776 case ISD::VUDIV:
4777 case ISD::VAND:
4778 case ISD::VOR:
4779 case ISD::VXOR:
4780 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4781 NewVT,
4782 PackVectorOp(Node->getOperand(0), NewVT),
4783 PackVectorOp(Node->getOperand(1), NewVT));
4784 break;
4785 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004786 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4787 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004788
Chris Lattner4794a6b2006-03-19 00:07:49 +00004789 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004790
4791 // Remember that we legalized the chain.
4792 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4793 break;
4794 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004795 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00004796 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004797 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004798 Result = Node->getOperand(0);
4799 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004800 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00004801
4802 // If all elements of the build_vector are undefs, return an undef.
4803 bool AllUndef = true;
4804 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
4805 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
4806 AllUndef = false;
4807 break;
4808 }
4809 if (AllUndef) {
4810 Result = DAG.getNode(ISD::UNDEF, NewVT);
4811 } else {
4812 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4813 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
4814 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004815 }
4816 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004817 case ISD::VINSERT_VECTOR_ELT:
4818 if (!MVT::isVector(NewVT)) {
4819 // Returning a scalar? Must be the inserted element.
4820 Result = Node->getOperand(1);
4821 } else {
4822 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4823 PackVectorOp(Node->getOperand(0), NewVT),
4824 Node->getOperand(1), Node->getOperand(2));
4825 }
4826 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00004827 case ISD::VVECTOR_SHUFFLE:
4828 if (!MVT::isVector(NewVT)) {
4829 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4830 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4831 if (cast<ConstantSDNode>(EltNum)->getValue())
4832 Result = PackVectorOp(Node->getOperand(1), NewVT);
4833 else
4834 Result = PackVectorOp(Node->getOperand(0), NewVT);
4835 } else {
4836 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4837 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4838 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4839 Node->getOperand(2).Val->op_end()-2);
4840 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4841 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4842
4843 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4844 PackVectorOp(Node->getOperand(0), NewVT),
4845 PackVectorOp(Node->getOperand(1), NewVT), BV);
4846 }
4847 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004848 case ISD::VBIT_CONVERT:
4849 if (Op.getOperand(0).getValueType() != MVT::Vector)
4850 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4851 else {
4852 // If the input is a vector type, we have to either scalarize it, pack it
4853 // or convert it based on whether the input vector type is legal.
4854 SDNode *InVal = Node->getOperand(0).Val;
4855 unsigned NumElems =
4856 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4857 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4858
4859 // Figure out if there is a Packed type corresponding to this Vector
4860 // type. If so, convert to the packed type.
4861 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4862 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4863 // Turn this into a bit convert of the packed input.
4864 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4865 PackVectorOp(Node->getOperand(0), TVT));
4866 break;
4867 } else if (NumElems == 1) {
4868 // Turn this into a bit convert of the scalar input.
4869 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4870 PackVectorOp(Node->getOperand(0), EVT));
4871 break;
4872 } else {
4873 // FIXME: UNIMP!
4874 assert(0 && "Cast from unsupported vector type not implemented yet!");
4875 }
4876 }
Evan Chengdb3c6262006-04-10 18:54:36 +00004877 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004878 case ISD::VSELECT:
4879 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
4880 PackVectorOp(Op.getOperand(1), NewVT),
4881 PackVectorOp(Op.getOperand(2), NewVT));
4882 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004883 }
4884
4885 if (TLI.isTypeLegal(NewVT))
4886 Result = LegalizeOp(Result);
4887 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4888 assert(isNew && "Value already packed?");
4889 return Result;
4890}
4891
Chris Lattner3e928bb2005-01-07 07:47:09 +00004892
4893// SelectionDAG::Legalize - This is the entry point for the file.
4894//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004895void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00004896 if (ViewLegalizeDAGs) viewGraph();
4897
Chris Lattner3e928bb2005-01-07 07:47:09 +00004898 /// run - This is the main entry point to this class.
4899 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004900 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004901}
4902